SpringBoot整合Druid數(shù)據(jù)源的方法實現(xiàn)
SprintBoot 默認(rèn)使用的是 HikariDataSource數(shù)據(jù)源,這次整合一個第三方的數(shù)據(jù)源 Druid ,它是阿里開發(fā)的一款開源的數(shù)據(jù)源,被很多人認(rèn)為是Java語言中最好的數(shù)據(jù)庫連接池,因為 Druid 能夠提供強大的一整套監(jiān)控和擴(kuò)展功能。
默認(rèn)情況下,sprintboot使用hikaridatasource數(shù)據(jù)源。這一次,集成了第三方數(shù)據(jù)源Druid。它是阿里巴巴開發(fā)的開源數(shù)據(jù)源,許多人認(rèn)為它是Java語言中最好的數(shù)據(jù)庫連接池,因為Druid可以提供一組強大的監(jiān)控和擴(kuò)展功能。
1、在創(chuàng)建SpringBoot項目的時候,在pom.xml maven中添加依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>注意:druid 依賴 log4j 的日志jar包,但是 SpringBoot 默認(rèn)使用的是 slf4j+logback,所以導(dǎo)入log4j的jar包即可。
2、在 application.yml(或aproperties)中添加相應(yīng)的配置:
#
server:
port: 80
# 數(shù)據(jù)庫連接信息
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT
driver-class-name: com.mysql.cj.jdbc.Driver # com.mysql.jdbc.Driver
# 使用 Druid 數(shù)據(jù)源
type: com.alibaba.druid.pool.DruidDataSource3、 log4j.properties 配置文件:
log4j.rootLogger = debug,stdout, D log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.Threshold = INFO log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p %m%n log4j.appender.D = org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File = ./log4j.log log4j.appender.D.Append = true log4j.appender.D.Threshold = DEBUG log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern=%d %p %m%n
4、在運行測試方法,查看數(shù)據(jù)源
public class SpringbootdemoApplicationTests {
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private DataSource dataSource;
@Test
public void contextLoads() throws SQLException {
System.out.println("dataSource==" + dataSource.getClass());
Connection con = dataSource.getConnection();
System.out.println("con==" + con);
List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from user");
System.out.println(maps);
}
}5、運行測試方法

Druid 數(shù)據(jù)源整合完成。
到此這篇關(guān)于SpringBoot整合Druid數(shù)據(jù)源的方法實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot整合Druid數(shù)據(jù)源內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot項目多數(shù)據(jù)源及mybatis 駝峰失效的問題解決方法
- SpringBoot詳解如何進(jìn)行整合Druid數(shù)據(jù)源
- Springboot集成mybatis實現(xiàn)多數(shù)據(jù)源配置詳解流程
- SpringBoot超詳細(xì)講解多數(shù)據(jù)源集成
- SpringBoot多數(shù)據(jù)源的兩種實現(xiàn)方式實例
- SpringBoot多數(shù)據(jù)源切換實現(xiàn)代碼(Mybaitis)
- 使用SpringBoot配置多數(shù)據(jù)源的經(jīng)驗分享
- 親手教你SpringBoot中的多數(shù)據(jù)源集成問題
- SpringBoot內(nèi)置數(shù)據(jù)源的持久化與解決方案
相關(guān)文章
Java TimeoutException:服務(wù)調(diào)用超時異常的正確解決方案
在現(xiàn)代軟件開發(fā)中,服務(wù)間通信是構(gòu)建分布式系統(tǒng)的基礎(chǔ),然而,網(wǎng)絡(luò)延遲、服務(wù)負(fù)載、資源競爭等因素都可能導(dǎo)致服務(wù)調(diào)用超時,TimeoutException是Java中表示服務(wù)調(diào)用超時的常見異常之一,本文將探討TimeoutException的成因及解決方案,需要的朋友可以參考下2024-12-12
springboot 整合EhCache實現(xiàn)單服務(wù)緩存的操作方法
這篇文章主要介紹了springboot 整合EhCache實現(xiàn)單服務(wù)緩存的操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07
聊聊SpringCloud和SpringCloudAlibaba的區(qū)別
這篇文章主要介紹了SpringCloud和SpringCloudAlibaba的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
解決redisTemplate向redis中插入String類型數(shù)據(jù)時出現(xiàn)亂碼問題
這篇文章主要介紹了解決redisTemplate向redis中插入String類型數(shù)據(jù)時出現(xiàn)亂碼問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
JAVA(SpringBoot)集成Jasypt進(jìn)行加密、解密功能
Jasypt是一個Java庫,專門用于簡化加密和解密操作,提供多種加密算法支持,集成到SpringBoot等框架中,通過使用Jasypt,可以有效保護(hù)配置文件中的敏感信息,如數(shù)據(jù)庫密碼等,避免被未授權(quán)訪問,Jasypt還支持自定義加密器,提高擴(kuò)展性和安全性,適用于各種需要加密保護(hù)應(yīng)用場景2024-09-09
springboot跨域如何設(shè)置SameSite的實現(xiàn)
這篇文章主要介紹了springboot跨域如何設(shè)置SameSite的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
詳解 問題:HttpServlet cannot be resolved to a type
這篇文章主要介紹了詳解 問題:HttpServlet cannot be resolved to a type的相關(guān)資料,需要的朋友可以參考下2017-03-03
SpringCloud解決feign調(diào)用token丟失問題解決辦法
在feign調(diào)用中可能會遇到如下問題:同步調(diào)用中,token丟失,這種可以通過創(chuàng)建一個攔截器,將token做透傳來解決,異步調(diào)用中,token丟失,這種就無法直接透傳了,因為子線程并沒有token,這種需要先將token從父線程傳遞到子線程,再進(jìn)行透傳2024-05-05

