springboot整合druid連接池的步驟
使用springboot默認(rèn)的連接池
導(dǎo)入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默認(rèn)的連接池
@Autowired DataSource dataSource; @Test void contextLoads() { System.out.println(dataSource.getClass()); System.out.println("____________________________________"); }
使用連接池druid
導(dǎo)入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
配置類中對(duì)druid屬性進(jìn)行綁定
@Configuration public class DataSource_Druid_Configure { @ConfigurationProperties(prefix = "spring.datasource") @Bean public DruidDataSource getDataSour(){ return new DruidDataSource(); }
配置Druid的監(jiān)控后臺(tái)
//配置Druid的監(jiān)控 //1、配置一個(gè)管理后臺(tái)的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","");//默認(rèn)就是允許所有訪問(wèn) initParams.put("deny","192.168.15.21");//拒絕訪問(wèn) bean.setInitParameters(initParams); return bean; } //2、配置一個(gè)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; }
訪問(wèn)http://localhost:8090/druid/login.html
如果sql監(jiān)控失效需要導(dǎo)入log4j 依賴
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
以上就是springboot整合druid連接池的步驟的詳細(xì)內(nèi)容,更多關(guān)于springboot整合druid連接池的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- SpringBoot中Druid連接池與多數(shù)據(jù)源切換的方法
- SpringBoot整合mybatis使用Druid做連接池的方式
- Springboot中加入druid連接池
- springboot2.0配置連接池(hikari、druid)的方法
- SpringBoot整合Druid實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接池和監(jiān)控
- springboot項(xiàng)目整合druid數(shù)據(jù)庫(kù)連接池的實(shí)現(xiàn)
- springboot集成druid連接池配置的方法
- SpringBoot使用 druid 連接池來(lái)優(yōu)化分頁(yè)語(yǔ)句
- SpringBoot整合Druid數(shù)據(jù)庫(kù)連接池的方法
- 解決Spring Boot中Druid連接池“discard long time none received connection“警告
相關(guān)文章
SpringBoot實(shí)現(xiàn)異步任務(wù)的項(xiàng)目實(shí)踐
本文將使用SpringBoot 去實(shí)現(xiàn)異步之間的調(diào)用,提高系統(tǒng)的并發(fā)性能、用戶體驗(yàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10Spring Cloud Gateway 服務(wù)網(wǎng)關(guān)快速實(shí)現(xiàn)解析
這篇文章主要介紹了Spring Cloud Gateway 服務(wù)網(wǎng)關(guān)快速實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08Java中ResultSetMetaData 元數(shù)據(jù)的具體使用
本文主要介紹了Java中ResultSetMetaData 元數(shù)據(jù)的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04Springboot如何使用Map將錯(cuò)誤提示輸出到頁(yè)面
這篇文章主要介紹了Springboot如何使用Map將錯(cuò)誤提示輸出到頁(yè)面,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08Springboot啟動(dòng)報(bào)錯(cuò)Input length = 2的問(wèn)題解決
最近使用Springboot啟動(dòng)報(bào)錯(cuò),報(bào)錯(cuò)內(nèi)容java.nio.charset.MalformedInputException: Input length = 2,下面就來(lái)介紹一下解決方法,感興趣的可以了解一下2024-08-08SpringMVC中的@ControllerAdvice使用場(chǎng)景詳解
這篇文章主要介紹了SpringMVC中的@ControllerAdvice使用場(chǎng)景詳解,在Spring?MVC進(jìn)行調(diào)用的過(guò)程中,會(huì)有很多的特殊的需求,比如全局異常,分頁(yè)信息和分頁(yè)搜索條件,請(qǐng)求時(shí)帶來(lái)返回時(shí)還得回顯頁(yè)面,需要的朋友可以參考下2024-01-01