springboot整合druid連接池的步驟
使用springboot默認的連接池
導入springboot data-jdbc依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
配置文件配置連接池
spring: datasource: username: root password: 5201314 url: jdbc:mysql:///jqmb?serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver
springboot默認的連接池
@Autowired
DataSource dataSource;
@Test
void contextLoads() {
System.out.println(dataSource.getClass());
System.out.println("____________________________________");
}

使用連接池druid
導入druid依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
配置文件配置druid的屬性
spring: datasource: username: root password: 5201314 url: jdbc:mysql:///jqmb?serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

配置類中對druid屬性進行綁定
@Configuration
public class DataSource_Druid_Configure {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DruidDataSource getDataSour(){
return new DruidDataSource();
}

配置Druid的監(jiān)控后臺
//配置Druid的監(jiān)控
//1、配置一個管理后臺的Servlet
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map<String,String> initParams = new HashMap<>();
initParams.put("loginUsername","admin");//登錄用戶名
initParams.put("loginPassword","123456");//登錄密碼
initParams.put("allow","");//默認就是允許所有訪問
initParams.put("deny","192.168.15.21");//拒絕訪問
bean.setInitParameters(initParams);
return bean;
}
//2、配置一個web監(jiān)控的filter
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
Map<String,String> initParams = new HashMap<>();
initParams.put("exclusions","*.js,*.css,/druid/*");
bean.setInitParameters(initParams);
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
訪問http://localhost:8090/druid/login.html

如果sql監(jiān)控失效需要導入log4j 依賴
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
以上就是springboot整合druid連接池的步驟的詳細內(nèi)容,更多關于springboot整合druid連接池的資料請關注腳本之家其它相關文章!
- SpringBoot中Druid連接池與多數(shù)據(jù)源切換的方法
- SpringBoot整合mybatis使用Druid做連接池的方式
- Springboot中加入druid連接池
- springboot2.0配置連接池(hikari、druid)的方法
- SpringBoot整合Druid實現(xiàn)數(shù)據(jù)庫連接池和監(jiān)控
- springboot項目整合druid數(shù)據(jù)庫連接池的實現(xiàn)
- springboot集成druid連接池配置的方法
- SpringBoot使用 druid 連接池來優(yōu)化分頁語句
- SpringBoot整合Druid數(shù)據(jù)庫連接池的方法
- 解決Spring Boot中Druid連接池“discard long time none received connection“警告
相關文章
Spring Cloud Gateway 服務網(wǎng)關快速實現(xiàn)解析
這篇文章主要介紹了Spring Cloud Gateway 服務網(wǎng)關快速實現(xiàn)解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08
Java中ResultSetMetaData 元數(shù)據(jù)的具體使用
本文主要介紹了Java中ResultSetMetaData 元數(shù)據(jù)的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-04-04
Springboot啟動報錯Input length = 2的問題解決
最近使用Springboot啟動報錯,報錯內(nèi)容java.nio.charset.MalformedInputException: Input length = 2,下面就來介紹一下解決方法,感興趣的可以了解一下2024-08-08
SpringMVC中的@ControllerAdvice使用場景詳解
這篇文章主要介紹了SpringMVC中的@ControllerAdvice使用場景詳解,在Spring?MVC進行調(diào)用的過程中,會有很多的特殊的需求,比如全局異常,分頁信息和分頁搜索條件,請求時帶來返回時還得回顯頁面,需要的朋友可以參考下2024-01-01

