SpringBoot整合Druid數(shù)據(jù)源的方法實(shí)現(xiàn)
SprintBoot 默認(rèn)使用的是 HikariDataSource數(shù)據(jù)源,這次整合一個(gè)第三方的數(shù)據(jù)源 Druid ,它是阿里開(kāi)發(fā)的一款開(kāi)源的數(shù)據(jù)源,被很多人認(rèn)為是Java語(yǔ)言中最好的數(shù)據(jù)庫(kù)連接池,因?yàn)?Druid 能夠提供強(qiáng)大的一整套監(jiān)控和擴(kuò)展功能。
默認(rèn)情況下,sprintboot使用hikaridatasource數(shù)據(jù)源。這一次,集成了第三方數(shù)據(jù)源Druid。它是阿里巴巴開(kāi)發(fā)的開(kāi)源數(shù)據(jù)源,許多人認(rèn)為它是Java語(yǔ)言中最好的數(shù)據(jù)庫(kù)連接池,因?yàn)镈ruid可以提供一組強(qiáng)大的監(jiān)控和擴(kuò)展功能。
1、在創(chuàng)建SpringBoot項(xiàng)目的時(shí)候,在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ù)庫(kù)連接信息
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、在運(yùn)行測(cè)試方法,查看數(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、運(yùn)行測(cè)試方法

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

