MyBatis-Plus 如何单元测试的实现
作者:liujianko 时间:2022-03-02 19:45:08
最近项目中使用了 MyBatis-Plus,点击看官方文档。
使用一个新的框架,首先是验证框架的使用。
使用 MyBatis-Plus,首先就验证一下能否成功操作(CRUD)数据库。
如何通过不用启动项目,然后可以测试 MyBatis-Plus 查询数据。
所以首要想到的是单元测试 @Test
第一步
通过 MyBatis-Plus 的代码生成工具生成数据库表对应的文件
MyBatis-Plus 对于单表操作,有一个内置的 mapper 接口方法,service 的接口我暂时没使用并没验证过。
使用过 MyBatis 的应该都知道,在 service 层使用 mapper.java 来操作数据库,并且 mapper.xml 里面是有对应的查询入口。
-- service
public class EntityServiceImp{
@Autowired
private EntityMapper mapper;
public void test(){
// 服务层调用 mapper.java 中的 selectEntityList 方法
mapper.selectEntityList(map);
}
}
-- mapper.java
public interface EntityMapper {
// mapper.xml 有一个id='selectEntityList' 的 select 块
List<entity> selectEntityList(Map<String, Object> map);
}
--mapper.xml
<mapper namespace="com.example.mapper.EntityMapper" >
<resultMap id="BaseResultMap" type="com.example.pojo.Entity" ></resultMap >
<select id="selectEntityList" resultMap="BaseResultMap" parameterType="map" >
select * from entity where .....
</select>
<mapper>
然而使用 MyBatis-Plus,对于单表操作,不需要像 MyBatis 这么麻烦,可通过调用内置一些单表的接口方法。
第二步
在 src/test/java 下面创建测试用例
@RunWith(SpringRunner.class)
@SpringBootTest
public class DbTest {
@Autowired
private LogYjxxMapper logYjxxMapper;
@Test
public void test2() {
// selectList 是内置的方法,logYjxxMapper中并不需要自己定义 selectList 这么一个方法
// selectList括号里的参数是条件构造器,可参看官方文档
List<LogYjxx> yjxxLoglist = logYjxxMapper.selectList(new QueryWrapper<LogYjxx>()
.eq("lx", YjxxConstant.LX_SF)
.and(i -> i.in("zt", 2,3).or().isNull("zt"))
);
for (LogYjxx logYjxx : yjxxLoglist) {
System.out.println(logYjxx);
}
}
}
重点: 类上方的两个注解(@RunWith(SpringRunner.class) @SpringBootTest)很重要,不要漏了。
来源:https://blog.csdn.net/godbrian/article/details/89554718
标签:MyBatis-Plus,单元测试
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
c#实现输出本月的月历
2023-07-18 19:41:33
命令行编译java文件方式
2023-01-18 18:35:47
![](https://img.aspxhome.com/file/2023/2/60252_0s.png)
springboot+springmvc+mybatis项目整合
2023-07-16 03:13:57
![](https://img.aspxhome.com/file/2023/1/61251_0s.png)
详解Java中对象池的介绍与使用
2023-07-25 13:13:48
浅谈Java(SpringBoot)基于zookeeper的分布式锁实现
2023-11-16 08:14:56
Java中的functor实现
2023-11-26 08:30:50
关于SpringBoot使用Redis空指针的问题(不能成功注入的问题)
2023-09-04 01:30:03
![](https://img.aspxhome.com/file/2023/2/61302_0s.png)
apollo更改配置刷新@ConfigurationProperties配置类
2022-01-09 22:28:22
![](https://img.aspxhome.com/file/2023/6/62666_0s.png)
基于Java GUI 事件处理方式
2023-11-25 13:08:55
![](https://img.aspxhome.com/file/2023/9/60079_0s.png)
深入理解java虚拟机的故障处理工具
2023-11-20 06:41:58
JDBC连接MySQL并实现模糊查询
2021-07-06 01:42:28
Springboot项目全局异常统一处理案例代码
2021-08-26 10:51:19
基于RxJava实现酷炫启动页
2023-09-26 21:50:47
![](https://img.aspxhome.com/file/2023/7/86007_0s.gif)
java模拟TCP通信实现客户端上传文件到服务器端
2023-11-26 10:14:49
Java 用反射设置对象的属性值实例详解
2023-11-26 03:56:51
详解MyBatis-Puls中saveBatch批量添加慢的问题
2021-10-14 02:36:27
![](https://img.aspxhome.com/file/2023/9/64369_0s.png)
java 动态 代理的方法总结
2023-08-25 04:00:33
Spring的连接数据库以及JDBC模板(实例讲解)
2023-03-21 05:56:36
![](https://img.aspxhome.com/file/2023/1/63101_0s.png)
java使用字符画一个海绵宝宝
2023-09-08 09:45:19
![](https://img.aspxhome.com/file/2023/3/58453_0s.jpg)
Java并发线程池实例分析讲解
2022-08-05 20:25:40
![](https://img.aspxhome.com/file/2023/2/61382_0s.png)