跳转到主内容
趣航编程网 - 趣学编程,启航技术之路!

MyBatis-Plus 如何单元测试的实现

最近项目中使用了 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 selectEntityList(Map map);}--mapper.xml

然而使用 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 yjxxLoglist = logYjxxMapper.selectList(new QueryWrapper().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)很重要,不要漏了。

好了,通过以上两步,就可以很顺利的验证自己的 sql 了。

到此这篇关于MyBatis-Plus 如何单元测试的实现的文章就介绍到这了,更多相关MyBatis-Plus 单元测试内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:基于Springboot+Mybatis对数据访问层进行单元测试的方式分享Springboot Mybatis-Plus数据库单元测试实战(三种方式)对dbunit进行mybatis DAO层Excel单元测试(必看篇) mybatis单元测试过程(无需启动容器)

相关文章