今天就跟大家聊聊有關(guān)使用springboot怎么對(duì)druid連接池進(jìn)行整合,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
堅(jiān)守“ 做人真誠(chéng) · 做事靠譜 · 口碑至上 · 高效敬業(yè) ”的價(jià)值觀,專業(yè)網(wǎng)站建設(shè)服務(wù)10余年為成都食品包裝袋小微創(chuàng)業(yè)公司專業(yè)提供企業(yè)網(wǎng)站建設(shè)營(yíng)銷網(wǎng)站建設(shè)商城網(wǎng)站建設(shè)手機(jī)網(wǎng)站建設(shè)小程序網(wǎng)站建設(shè)網(wǎng)站改版,從內(nèi)容策劃、視覺(jué)設(shè)計(jì)、底層架構(gòu)、網(wǎng)頁(yè)布局、功能開(kāi)發(fā)迭代于一體的高端網(wǎng)站建設(shè)服務(wù)。使用springboot默認(rèn)的連接池
導(dǎo)入springboot data-jdbc依賴
org.springframework.boot spring-boot-starter-data-jdbc
配置文件配置連接池
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依賴
com.alibaba druid 1.0.18
配置文件配置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/*"); MapinitParams = 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 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 依賴
log4j log4j 1.2.17
看完上述內(nèi)容,你們對(duì)使用springboot怎么對(duì)druid連接池進(jìn)行整合有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。