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)行綁定
@Configurationpublic 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)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 基于 Python 實(shí)踐感知器分類算法2. Python如何批量生成和調(diào)用變量3. Python 中如何使用 virtualenv 管理虛擬環(huán)境4. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車5. 通過(guò)CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效6. Python獲取B站粉絲數(shù)的示例代碼7. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無(wú)效問(wèn)題8. ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖9. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)10. python利用opencv實(shí)現(xiàn)顏色檢測(cè)
