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
配置類中對druid屬性進行綁定
@Configurationpublic 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','');//默認(rèn)就是允許所有訪問 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)控失效需要導(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連接池的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. JSP的Cookie在登錄中的使用2. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實現(xiàn)代碼3. HTML5 Canvas繪制圖形從入門到精通4. 使用Spry輕松將XML數(shù)據(jù)顯示到HTML頁的方法5. 利用CSS3新特性創(chuàng)建透明邊框三角6. ASP基礎(chǔ)知識VBScript基本元素講解7. 詳解CSS偽元素的妙用單標(biāo)簽之美8. XHTML 1.0:標(biāo)記新的開端9. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究10. XML入門的常見問題(四)
