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)就是允許所有訪問 initParams.put('deny','192.168.15.21');//拒絕訪問 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; }
訪問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. asp讀取xml文件和記數(shù)2. Xml簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理3. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?4. JSP的Cookie在登錄中的使用5. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)6. ASP中實(shí)現(xiàn)字符部位類似.NET里String對(duì)象的PadLeft和PadRight函數(shù)7. asp批量添加修改刪除操作示例代碼8. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法9. asp知識(shí)整理筆記4(問答模式)10. 得到XML文檔大小的方法
