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
注解指定運行器,使用 @ContextConfiguration
注解來指定配置類或者配置文件。而 SpringBoot
整合 junit
特別簡單,分為以下三步完成
- 在測試類上添加
SpringBootTest
注解 - 使用
@Autowired
注入要測試的資源 - 定義測試方法進行測試
環(huán)境準備
創(chuàng)建一個名為 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)建一個 BookServiceImpl
類,使其實現(xiàn) BookService
接口,內(nèi)容如下
@Service public class BookServiceImpl implements BookService { @Override public void save() { System.out.println("book service is running ..."); } }
編寫測試類
在 test/java
下創(chuàng)建 com.itheima
包,在該包下創(chuàng)建測試類,將 BookService
注入到該測試類中
@SpringBootTest class Springboot07TestApplicationTests { @Autowired private BookService bookService; @Test public void save() { bookService.save(); } }
注意:這里的引導(dǎo)類所在包必須是測試類所在包及其子包。
我們的測試類會自動加載引導(dǎo)類來初始化Spring的環(huán)境。如果不在一個包里那么就找不著引導(dǎo)類了。
例如:
- 引導(dǎo)類所在包是
com.itheima
- 測試類所在包是
com.itheima
如果不滿足這個要求的話,就需要在使用 @SpringBootTest
注解時,使用 classes
屬性指定引導(dǎo)類的字節(jié)碼對象。如 @SpringBootTest(classes = Springboot07TestApplication.class)
在這里我們認識了一個新的注解:
原來我們在Spring整合Junit的時候還要在類上面配上加載的配置文件(@ContextConfiguration(classes = SpringConfig.class))。我們在使用SpringBoot整合Junit的時候雖然沒有明著寫,但其實也加載了。
我們的引導(dǎo)類其實啟到了配置類的作用,它會把他所在的包及其子包全部掃描一遍,所以說我們寫的@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配置項: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ǔ)信息
選擇當前模塊需要使用的技術(shù)集(MyBatis、MySQL)
定義實體類
在 com.itheima.domain
包下定義實體類 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); }
定義測試類
在 test/java
下定義包 com.itheima
,在該包下測試類,內(nèi)容如下
@SpringBootTest class Springboot08MybatisApplicationTests { @Autowired private BookDao bookDao; @Test void testGetById() { Book book = bookDao.getById(1); System.out.println(book); } }
編寫配置
我們代碼中并沒有指定連接哪兒個數(shù)據(jù)庫,用戶名是什么,密碼是什么。所以這部分需要在 SpringBoot
的配置文件中進行配合。
在 application.yml
配置文件中配置如下內(nèi)容
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db
username: root
password: root
測試
運行測試方法,我們會看到如下錯誤信息
錯誤信息顯示在 Spring
容器中沒有 BookDao
類型的 bean
。為什么會出現(xiàn)這種情況呢?
原因是 Mybatis
會掃描接口并創(chuàng)建接口的代碼對象交給 Spring
管理,但是現(xiàn)在并沒有告訴 Mybatis
哪個是 dao
接口。而我們要解決這個問題需要在BookDao
接口上使用 @Mapper
,BookDao
接口改進為
@Mapper public interface BookDao { @Select("select * from tbl_book where id = #{id}") public Book getById(Integer id); }
注意:
SpringBoot
版本低于2.4.3(不含),Mysql驅(qū)動版本大于8.0時,需要在url連接串中配置時區(qū) jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
,或在MySQL數(shù)據(jù)庫端配置時區(qū)解決此問題
使用Druid數(shù)據(jù)源
現(xiàn)在我們并沒有指定數(shù)據(jù)源,SpringBoot
有默認的數(shù)據(jù)源,我們也可以指定使用 Druid
數(shù)據(jù)源,按照以下步驟實現(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)容可以改進為
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot Hazelcast Caching 使用和配置詳解
這篇文章主要介紹了Spring Boot Hazelcast Caching 使用和配置詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09Java中的HttpServletRequest接口詳細解讀
這篇文章主要介紹了Java中的HttpServletRequest接口詳細解讀,是一個接口,全限定名稱為Jakarta.Serclet.http.HttpServletRequest2023-11-11
HttpServletRequest接口是Servlet規(guī)范的一員,需要的朋友可以參考下