SpringBoot 整合MyBatis、Junit5的實(shí)踐過程
一、前言
自從使用SpringBoot后,前言部分不需要再過多贅述Maven的坐標(biāo)了,但是由于我們需要集成外部的工具(框架),所以可能還是需要引入坐標(biāo),有些在創(chuàng)建模塊的時(shí)候可以使用圖形化界面勾選,有些依舊只能在pom.xml中手動(dòng)添加,本文需要的坐標(biāo)如下:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>3.0.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>二、整合Junit
正常創(chuàng)建一個(gè)SpringBoot的模塊,這里先模擬一個(gè)Service類,在里面設(shè)計(jì)一個(gè)add方法:
@Service
public class UserService {
public void add(){
System.out.println("add...");
}
}編寫測試類如下:
/**
* userService的測試類
*/
//junit5
@ExtendWith(SpringExtension.class)
//junit4
//@RunWith(SpringRunner.class)
//@SpringBootTest(classes = SpringBootTestApplication.class)
@SpringBootTest//當(dāng)測試類的包名和java中主包相同 或 測試類包名屬于主包的子包,可以不用寫(classes = SpringBootTestApplication.class)
class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void testAdd(){
userService.add();
}
}注意:這里我演示的是Junit5的測試注解,如果需要使用Junit4,則需要導(dǎo)入Junit4的包,4和5的注解略有不同。

效果如下(不必關(guān)注警告,這是在提示未來版本可能會取消掉某個(gè)功能):

三、整合MyBatis
1.注解開發(fā)
首先先創(chuàng)建一個(gè)表,用于模擬各種操作(這里我們只演示查詢操作)

同時(shí)需要一個(gè)User類來封裝數(shù)據(jù):
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}然后我們需要在yml配置文件中配置數(shù)據(jù)源:
# datasource
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///springboot
password: 123456
username: root這里我們直接創(chuàng)建一個(gè)映射接口,使用注解開發(fā),其中@Mapper表示掃描這個(gè)映射接口,自動(dòng)生成這個(gè)接口的實(shí)現(xiàn)類,里面我們只寫一個(gè)查詢方法。
@Mapper
public interface UserMapper {
@Select("select * from t_user")
public List<User> findAll();
}創(chuàng)建測試類:
@ExtendWith(SpringExtension.class)
@SpringBootTest
class SpringBootMybatisApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void testFindAll1() {
List<User> list = userMapper.findAll();
System.out.println(list);
}
}效果如下:

2.xml配置
雖然注解相對方便,但是在多表操作的大項(xiàng)目中,使用xml配置映射文件會讓可讀性更高,并且許多老項(xiàng)目一直沿用的xml配置,所以依舊需要了解xml的編寫方式。
首先是接口,我們這里重新寫一個(gè):
@Mapper
public interface UserXmlMapper {
public List<User> findAll();
}然后就是對應(yīng)的xml映射文件:
<mapper namespace="com.yds.springbootmybatis.mapper.UserXmlMapper">
<select id="findAll" resultType="user">
select * from t_user
</select>
</mapper>同時(shí),既然已經(jīng)使用了xml配置,就必須配置核心配置文件,這里我們直接寫在yml配置文件中,也就是說,我們在yml配置文件中配置了兩個(gè)東西,一個(gè)是數(shù)據(jù)源,一個(gè)是核心配置文件(在核心配置文件中取了別名):
#mybatis mybatis: mapper-locations: classpath:mapper/*Mapper.xml #mapper的映射文件路徑 type-aliases-package: com.yds.springbootmybatis.domain #vconfig-location: #指定mybatis的核心配置文件的
同時(shí)重寫測試類:
@ExtendWith(SpringExtension.class)
@SpringBootTest
class SpringBootMybatisApplicationTests {
@Autowired
private UserMapper userMapper;
@Autowired
private UserXmlMapper userXmlMapper;
@Test
public void testFindAll1() {
List<User> list = userMapper.findAll();
System.out.println(list);
}
@Test
public void testFindAll2() {
List<User> list = userXmlMapper.findAll();
System.out.println(list);
}
}效果如下:

到此這篇關(guān)于SpringBoot 整合MyBatis、Junit5的文章就介紹到這了,更多相關(guān)SpringBoot 整合MyBatis、Junit5內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot整合MyBatis和SpringBoot整合MyBatis-Plus教程
- SpringBoot整合mybatisPlus實(shí)現(xiàn)批量插入并獲取ID詳解
- SpringBoot整合Mybatis-plus關(guān)鍵詞模糊查詢結(jié)果為空
- SpringBoot + Mybatis Plus 整合 Redis的詳細(xì)步驟
- 全網(wǎng)最新springboot整合mybatis-plus的過程
- SpringBoot3整合Mybatis完整版實(shí)例
- Springboot整合mybatis-plus使用pageHelper進(jìn)行分頁(使用步驟)
- Springboot整合Mybatis和SQLite的詳細(xì)過程
- SpringBoot+JUnit5+MockMvc+Mockito單元測試的實(shí)現(xiàn)
相關(guān)文章
springsecurity6配置自定義路徑身份認(rèn)證的實(shí)現(xiàn)
本文主要介紹了springsecurity6配置自定義路徑身份認(rèn)證的實(shí)現(xiàn),通過使用自定義的AuthorizationManager和MyService,可以實(shí)現(xiàn)更靈活的訪問控制,感興趣的可以了解一下2025-03-03
詳解ConcurrentHashMap如何保證線程安全及底層實(shí)現(xiàn)原理
這篇文章主要為大家介紹了ConcurrentHashMap如何保證線程安全及底層實(shí)現(xiàn)原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
java 實(shí)現(xiàn)回調(diào)代碼實(shí)例
本文主要介紹Java的回調(diào)機(jī)制,并附實(shí)例代碼以供大家參考學(xué)習(xí),有需要的小伙伴可以看下2016-07-07
Java文件處理之使用XWPFDocument導(dǎo)出Word文檔
最近因項(xiàng)目開發(fā)的需要,整理了一份用JAVA導(dǎo)出WORD文檔,下面這篇文章主要給大家介紹了關(guān)于Java文件處理之使用XWPFDocument導(dǎo)出Word文檔的相關(guān)資料,需要的朋友可以參考下2023-12-12
詳解Java的call by value和call by reference
在本篇文章里小編給大家總結(jié)了關(guān)于Java的call by value和call by reference的相關(guān)用法和知識點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-03-03
java開發(fā)Dubbo負(fù)載均衡與集群容錯(cuò)示例詳解
這篇文章主要為大家介紹了java開發(fā)Dubbo負(fù)載均衡與集群容錯(cuò)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11

