SpringBoot整合第三方技術(shù)的詳細(xì)步驟
SpringBoot整合第三方技術(shù)
一、整合Junit
新建一個(gè)SpringBoot項(xiàng)目
使用@SpringBootTest標(biāo)簽在test測(cè)試包內(nèi)整合Junit
@SpringBootTest class Springboot03JunitApplicationTests { @Autowired private BookService bookService; @Test void contextLoads() { bookService.save(); } }
- 名稱:@SpringBootTest
- 類型:測(cè)試類注解
- 位置:測(cè)試類定義上方
- 作用:設(shè)置Junnit加載的SpringBoot啟動(dòng)類
注意:整合的Junit測(cè)試類需要和Java包中的配置文件類放在同一目錄下,否則需要指定配置java文件的class
@SpringBootTest(classes = Springboot03JunitApplication.class) class Springboot03JunitApplicationTests { @Autowired private BookService bookService; @Test void contextLoads() { bookService.save(); } }
二、整合Mybatis
創(chuàng)建新模塊的時(shí)候選擇需要的技術(shù)集
之后就可以看到mybatis相應(yīng)的坐標(biāo)已經(jīng)導(dǎo)入完成
接著設(shè)置數(shù)據(jù)源
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test username: root password: 123456
定義數(shù)據(jù)層接口與映射配置
public interface UserDao { @Select("select * from test.sys_role;") public List<Role> getAll(); }
測(cè)試類中注入dao接口,測(cè)試功能
@SpringBootTest class Springboot04MybatisApplicationTests { @Autowired private UserDao userDao; @Test void contextLoads() { List<Role> roleList = userDao.getAll(); System.out.println(roleList); } }
注意:
- 數(shù)據(jù)庫(kù)SQL映射需要添加@Mapper被容器識(shí)別到
- 數(shù)據(jù)庫(kù)連接相關(guān)信息轉(zhuǎn)換成配置
- SpringBoot版本低于2.4.3(不含),Mysql驅(qū)動(dòng)版本大于8.0時(shí),需要在url連接串中配置時(shí)區(qū),或在MySQL數(shù)據(jù)庫(kù)端配置時(shí)區(qū)解決此問(wèn)題
jdbc:mysql://localhost:3306/test?serverTimezone=UTC
三、整合Mybatis-Plus
Mybatis-Plus與Mybati 區(qū)別
- 導(dǎo)入坐標(biāo)不同
- 數(shù)據(jù)層實(shí)現(xiàn)簡(jiǎn)化
注意:由于SpringBoot中未收錄MyBatis-Plus的坐標(biāo)版本,需要指定對(duì)應(yīng)的Version
SpringBoot沒(méi)有整合Mybatis-Plus,所以需要我們手動(dòng)添加SpringBoot整合MyBatis-Plus的坐標(biāo),可以通過(guò)mvnrepository獲取
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3</version> </dependency>
定義數(shù)據(jù)層接口與映射配置,繼承BaseMapper
@Mapper public interface UserDao extends BaseMapper<Role> { }
在yml配置文件配置數(shù)據(jù)庫(kù)前綴
#設(shè)置mp相關(guān)配置 mybatis-plus: global-config: db-config: table-prefix: sys_
測(cè)試
@SpringBootTest class Springboot05MybatisPlusApplicationTests { @Autowired private UserDao userDao; @Test void contextLoads() { Role role = userDao.selectById(1); System.out.println(role); } }
四、整合Druid
同樣的,Druid也需要自己手工整合
Maven導(dǎo)入依賴
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.6</version> </dependency>
在yml配置文件指定數(shù)據(jù)源
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
或者
spring: datasource: druid: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC username: root password: root
五、總結(jié)
整合第三方技術(shù)的步驟:
- 導(dǎo)入對(duì)應(yīng)的starter
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
username: root
password: root
到此這篇關(guān)于SpringBoot整合第三方技術(shù)的文章就介紹到這了,更多相關(guān)SpringBoot整合第三方內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot為啥不用配置啟動(dòng)類的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot為啥不用配置啟動(dòng)類的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Java連接Oracle數(shù)據(jù)庫(kù)并查詢
這篇文章主要介紹了Java連接Oracle數(shù)據(jù)庫(kù)并查詢的相關(guān)資料,需要的朋友可以參考下2017-04-04java如何根據(jù)IP獲取當(dāng)前區(qū)域天氣信息詳解
根據(jù)IP自動(dòng)獲取當(dāng)?shù)氐奶鞖忸A(yù)報(bào)信息這個(gè)功能大家應(yīng)該都遇到過(guò),天氣預(yù)報(bào)信息用途非常廣泛,篇文章主要給大家介紹了關(guān)于java如何根據(jù)IP獲取當(dāng)前區(qū)域天氣信息的相關(guān)資料,需要的朋友可以參考下2021-08-08Spring動(dòng)態(tài)配置計(jì)時(shí)器觸發(fā)時(shí)間的實(shí)例代碼
這篇文章主要介紹了Spring動(dòng)態(tài)配置計(jì)時(shí)器觸發(fā)時(shí)間的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06基于Java判斷網(wǎng)絡(luò)是否正常代碼實(shí)例
這篇文章主要介紹了基于Java判斷網(wǎng)絡(luò)是否正常代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Maven之遠(yuǎn)程倉(cāng)庫(kù)的配置詳解
這篇文章主要介紹了Maven之遠(yuǎn)程倉(cāng)庫(kù)的配置詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09