溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

springboot-mybatis怎么使用junit4 單元測試單獨啟動mybatis

發(fā)布時間:2021-07-09 09:23:11 來源:億速云 閱讀:323 作者:chen 欄目:大數(shù)據(jù)

本篇內容主要講解“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)    //這個是啟用自己配置的數(shù)據(jù)元,不加則采用虛擬數(shù)據(jù)源
@Rollback(false)    //這個是默認是回滾,不會commit入數(shù)據(jù)庫,改成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入?yún)⒅?
    @Options(useGeneratedKeys = true , keyProperty = "id")    //只能再自增id中用,返回插入的id,放入entity入?yún)⒅?
    int insert(OtpStatisticsEntity entity);

到此,相信大家對“springboot-mybatis怎么使用junit4 單元測試單獨啟動mybatis”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI