SpringBoot整合junit與Mybatis流程詳解
SpringBoot整合junit
回顧 Spring 整合 junit
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class UserServiceTest {
@Autowired
private BookService bookService;
@Test
public void testSave(){
bookService.save();
}
}使用 @RunWith 注解指定運(yùn)行器,使用 @ContextConfiguration 注解來指定配置類或者配置文件。而 SpringBoot 整合 junit 特別簡單,分為以下三步完成
- 在測(cè)試類上添加
SpringBootTest注解 - 使用
@Autowired注入要測(cè)試的資源 - 定義測(cè)試方法進(jìn)行測(cè)試
環(huán)境準(zhǔn)備
創(chuàng)建一個(gè)名為 springboot_07_test 的 SpringBoot 工程,工程目錄結(jié)構(gòu)如下

在 com.itheima.service 下創(chuàng)建 BookService 接口,內(nèi)容如下
public interface BookService {
public void save();
}
在 com.itheima.service.impl 包寫創(chuàng)建一個(gè) BookServiceImpl 類,使其實(shí)現(xiàn) BookService 接口,內(nèi)容如下
@Service
public class BookServiceImpl implements BookService {
@Override
public void save() {
System.out.println("book service is running ...");
}
}編寫測(cè)試類
在 test/java 下創(chuàng)建 com.itheima 包,在該包下創(chuàng)建測(cè)試類,將 BookService 注入到該測(cè)試類中
@SpringBootTest
class Springboot07TestApplicationTests {
@Autowired
private BookService bookService;
@Test
public void save() {
bookService.save();
}
}注意:這里的引導(dǎo)類所在包必須是測(cè)試類所在包及其子包。
我們的測(cè)試類會(huì)自動(dòng)加載引導(dǎo)類來初始化Spring的環(huán)境。如果不在一個(gè)包里那么就找不著引導(dǎo)類了。
例如:
- 引導(dǎo)類所在包是
com.itheima - 測(cè)試類所在包是
com.itheima
如果不滿足這個(gè)要求的話,就需要在使用 @SpringBootTest 注解時(shí),使用 classes 屬性指定引導(dǎo)類的字節(jié)碼對(duì)象。如 @SpringBootTest(classes = Springboot07TestApplication.class)
在這里我們認(rèn)識(shí)了一個(gè)新的注解:

原來我們?cè)赟pring整合Junit的時(shí)候還要在類上面配上加載的配置文件(@ContextConfiguration(classes = SpringConfig.class))。我們?cè)谑褂肧pringBoot整合Junit的時(shí)候雖然沒有明著寫,但其實(shí)也加載了。
我們的引導(dǎo)類其實(shí)啟到了配置類的作用,它會(huì)把他所在的包及其子包全部掃描一遍,所以說我們寫的@Service才能加載成bean:

SpringBoot整合mybatis
回顧Spring整合Mybatis
Spring 整合 Mybatis 需要定義很多配置類
SpringConfig 配置類
- 導(dǎo)入
JdbcConfig配置類 - 導(dǎo)入
MybatisConfig配置類
@Configuration
@ComponentScan("com.itheima")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MyBatisConfig.class})
public class SpringConfig {
}JdbcConfig 配置類
定義數(shù)據(jù)源(加載properties配置項(xiàng):driver、url、username、password)
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String userName;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource getDataSource(){
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(userName);
ds.setPassword(password);
return ds;
}
}MybatisConfig 配置類
- 定義
SqlSessionFactoryBean - 定義映射配置
@Bean
public MapperScannerConfigurer getMapperScannerConfigurer(){
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.itheima.dao");
return msc;
}
@Bean
public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource){
SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
ssfb.setTypeAliasesPackage("com.itheima.domain");
ssfb.setDataSource(dataSource);
return ssfb;
}SpringBoot整合mybatis
創(chuàng)建模塊
創(chuàng)建新模塊,選擇 Spring Initializr,并配置模塊相關(guān)基礎(chǔ)信息

選擇當(dāng)前模塊需要使用的技術(shù)集(MyBatis、MySQL)

定義實(shí)體類
在 com.itheima.domain 包下定義實(shí)體類 Book,內(nèi)容如下
public class Book {
private Integer id;
private String name;
private String type;
private String description;
//setter and getter
//toString
}定義dao接口
在 com.itheima.dao 包下定義 BookDao 接口,內(nèi)容如下
public interface BookDao {
@Select("select * from tbl_book where id = #{id}")
public Book getById(Integer id);
}
定義測(cè)試類
在 test/java 下定義包 com.itheima ,在該包下測(cè)試類,內(nèi)容如下
@SpringBootTest
class Springboot08MybatisApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void testGetById() {
Book book = bookDao.getById(1);
System.out.println(book);
}
}編寫配置
我們代碼中并沒有指定連接哪兒個(gè)數(shù)據(jù)庫,用戶名是什么,密碼是什么。所以這部分需要在 SpringBoot 的配置文件中進(jìn)行配合。
在 application.yml 配置文件中配置如下內(nèi)容
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db
username: root
password: root
測(cè)試
運(yùn)行測(cè)試方法,我們會(huì)看到如下錯(cuò)誤信息

錯(cuò)誤信息顯示在 Spring 容器中沒有 BookDao 類型的 bean。為什么會(huì)出現(xiàn)這種情況呢?
原因是 Mybatis 會(huì)掃描接口并創(chuàng)建接口的代碼對(duì)象交給 Spring 管理,但是現(xiàn)在并沒有告訴 Mybatis 哪個(gè)是 dao 接口。而我們要解決這個(gè)問題需要在BookDao 接口上使用 @Mapper ,BookDao 接口改進(jìn)為
@Mapper
public interface BookDao {
@Select("select * from tbl_book where id = #{id}")
public Book getById(Integer id);
}
注意:
SpringBoot 版本低于2.4.3(不含),Mysql驅(qū)動(dòng)版本大于8.0時(shí),需要在url連接串中配置時(shí)區(qū) jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC,或在MySQL數(shù)據(jù)庫端配置時(shí)區(qū)解決此問題
使用Druid數(shù)據(jù)源
現(xiàn)在我們并沒有指定數(shù)據(jù)源,SpringBoot 有默認(rèn)的數(shù)據(jù)源,我們也可以指定使用 Druid 數(shù)據(jù)源,按照以下步驟實(shí)現(xiàn)
導(dǎo)入 Druid 依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
在 application.yml 配置文件配置
可以通過 spring.datasource.type 來配置使用什么數(shù)據(jù)源。配置文件內(nèi)容可以改進(jìn)為
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
到此這篇關(guān)于SpringBoot整合junit與Mybatis流程詳解的文章就介紹到這了,更多相關(guān)SpringBoot整合junit 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot Thymeleaf字符串對(duì)象實(shí)例解析
這篇文章主要介紹了Springboot Thymeleaf字符串對(duì)象實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2007-09-09
Springboot錯(cuò)誤頁面和錯(cuò)誤信息定制操作
這篇文章主要介紹了Springboot錯(cuò)誤頁面和錯(cuò)誤信息定制操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Spring Boot非Web項(xiàng)目運(yùn)行配置的方法教程
這篇文章主要介紹了Spring Boot非Web項(xiàng)目運(yùn)行配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Spring Boot Hazelcast Caching 使用和配置詳解
這篇文章主要介紹了Spring Boot Hazelcast Caching 使用和配置詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09
Java中的HttpServletRequest接口詳細(xì)解讀
這篇文章主要介紹了Java中的HttpServletRequest接口詳細(xì)解讀,是一個(gè)接口,全限定名稱為Jakarta.Serclet.http.HttpServletRequest2023-11-11
HttpServletRequest接口是Servlet規(guī)范的一員,需要的朋友可以參考下

