我基于注解创建了myBatis
Map器,如以下 * 更改代码 * 示例所示:
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface MyTableMapper {
@Insert("INSERT INTO MY_TABLE(" +
"Column_1, " +
"Column_2 " +
") VALUES (" +
"#{myTableModel.columnOne}, " +
"#{myTableModel.columnTwo} " +
")")
int insert(MyTableModel myTableModel);
@Select("SELECT * FROM MY_TABLE")
@Results(value = {
@Result(id = true, property = "columnOne", column = "Column_1"),
@Result(property = "columnTwo", column = "Column_2")
})
List<MyTableModel> findAll();
@ResultType(Integer.class)
@Select("SELECT COUNT(*) FROM MY_TABLE " +
"WHERE Column_1 = #{columnOne} AND Column_2 = #{columnTwo}")
int countByColumnOneAndColumnTwo(@Param("columnOne") String columnOne, @Param("columnTwo") String columnTwo);
@Update("UPDATE MY_TABLE SET " +
"Column_3 = #{myTableModel.columnThree}, "
"Column_4 = #{myTableModel.columnFour} " +
"WHERE Column_1 = #{myTableModel.columnOne} AND Column_2 = #{myTableModel.columnTwo}")
int updateByColumnOneAndColumnTwo(@Param("myTableModel") MyTableModel myTableModel);
@Delete("DELETE FROM MY_TABLE " +
"WHERE Column_1 = #{myTableModel.columnOne} AND Column_2 = #{myTableModel.columnTwo}")
void deleteByColumnOneAndColumnTwo(@Param("myTableModel") MyTableModel myTableModel);
}
注意事项!我正在使用Spring Cloud Config
和Hashicorp Vault
。
spring:
datasource:
url: jdbc:sqlserver://172.xx.yy.zzz:1433;databaseName=myDatabase;encrypt=true;trustServerCertificate=true;
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
testWhileIdle: true
testOnBorrow: true
hikari:
connection-test-query: SELECT 1
....
- 某些
yaml
配置文件 *
#Hashicorp Vault
spring.cloud.vault:
scheme: ${VAULT_SCHEME:http}
host: ${VAULT_HOST:localhost}
port: ${VAULT_PORT:8xyz0}
...
application-name: my-microservice-name
#Config Server
spring.cloud.config:
...
uri: ${SPRING_CLOUD_CONFIG_URI:http://localhost:8xx8}
凭据存储在保管库中(* 要创建SqlSession,XML文件中的其他内容不是选项*)
现在我想测试我的Map器。我使用@MybatisTest* 部分找到了这个documentation *。
我的测试Spock
文件!
import org.springframework.beans.factory.annotation.Autowired
import spock.lang.Specification
class MyTableMapperSpec extends Specification {
@Autowired
private MyTableMapper myTableMapper
private MyTableModel myTableModel
void setup() {
def myTableModelId = MyTableModelId.builder()
//.someFields
.build()
myTableModel = MyTableModel.builder()
//.someFields
.build()
myTableMapper.deleteByColumnOneAndColumnTwo(myTableModel)
myTableMapper.insert(myTableModel)
}
def "FindAll"() {
when:
List<MyTableModel> listMyTableModel = myTableMapper.findAll()
then:
!listMyTableModel.isEmpty()
}
void cleanup() {
//myTableMapper.deleteByColumnOneAndColumnTwo(myTableMapper)
}
}
当我这样试探这句台词时:myTableMapper.deleteByColumnOneAndColumnTwo(myTableModel)
我注意到我的**myTableMapper
**是null
!也许@Autowired
注解不适合我的代码!
如何解决此问题(获取Map器的实现)?
2条答案
按热度按时间lmvvr0a81#
文件包括:
build.gradle
的但是,
@MybatisTest
将失败,因为您没有dataSource
、sqlSessionFactory
或其他!尝试使用:
@SpringBootTest
或
bvpmtnay2#
那么,您实际上需要用
@MybatisTest
来注解Spock规范。除了spock-core
依赖项之外,您还需要spock-spring
依赖项。