千家信息网

springboot-mybatis怎么使用junit4 单元测试单独启动mybatis

发表于:2025-02-05 作者:千家信息网编辑
千家信息网最后更新 2025年02月05日,本篇内容主要讲解"springboot-mybatis怎么使用junit4 单元测试单独启动mybatis",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"s
千家信息网最后更新 2025年02月05日springboot-mybatis怎么使用junit4 单元测试单独启动mybatis

本篇内容主要讲解"springboot-mybatis怎么使用junit4 单元测试单独启动mybatis",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"springboot-mybatis怎么使用junit4 单元测试单独启动mybatis"吧!

1. 首先再pom加入mybatis test 的jar包

    org.mybatis.spring.boot    mybatis-spring-boot-starter-test    1.3.2    test

2. 增加单元测试类

//@SpringbootTest@MybatisTest    //缓存mybatsitest注解@RunWith(SpringJUnit4ClassRunner.class)@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)    //这个是启用自己配置的数据元,不加则采用虚拟数据源@Rollback(false)    //这个是默认是回滚,不会commit入数据库,改成false 则commitpublic class MapperTest {    @Resource    Mapper mapper;    @Test    public void testInsert(){        Entity entity = new Entity();        entity.setAct("res");        int insert = mapper.insert(entity);        System.out.println(insert);        System.out.println(entity.getId());    }

3. mybatis 注解式sql编写

@Insert({"insert into otp_statistics ( ACT ) VALUES ( #{act} ) "})// @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", resultType = Integer.class, before = false)    //返回插入的id,放入entity入参中    @Options(useGeneratedKeys = true , keyProperty = "id")    //只能再自增id中用,返回插入的id,放入entity入参中    int insert(OtpStatisticsEntity entity);

到此,相信大家对"springboot-mybatis怎么使用junit4 单元测试单独启动mybatis"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0