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

发布时间:2021-07-09 09:23:11 作者:chen
来源:亿速云 阅读:303

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

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

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter-test</artifactId>
    <version>1.3.2</version>
    <scope>test</scope>
</dependency>

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

推荐阅读:
  1. springboot-mybatis的使用和配置教程
  2. spring-boot2.0 单元测试JUnit4

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

junit spring spring boot

上一篇:iOS如何实现懒加载

下一篇:iOS如何实现加载本地HTML,css,js

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》