重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

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

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

创新新互联,凭借十多年的网站制作、成都网站制作经验,本着真心·诚心服务的企业理念服务于成都中小企业设计网站有1000+案例。做网站建设,选成都创新互联公司

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 则commit
public 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”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


网页名称:springboot-mybatis怎么使用junit4单元测试单独启动mybatis
分享URL:http://cqcxhl.com/article/pgpecd.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP