1、导入依赖
1 <!--druib数据源--> 2 <dependency> 3 <groupId>com.alibaba</groupId> 4 <artifactId>druid</artifactId> 5 <version>1.1.21</version> 6 </dependency> 7 8 <dependency> 9 <groupId>org.springframework.boot</groupId> 10 <artifactId>spring-boot-starter-jdbc</artifactId> 11 </dependency> 12 13 <!--日志--> 14 <dependency> 15 <groupId>log4j</groupId> 16 <artifactId>log4j</artifactId> 17 <version>1.2.17</version> 18 </dependency> 19 <!--JDBC连接驱动--> 20 <dependency> 21 <groupId>mysql</groupId> 22 <artifactId>mysql-connector-java</artifactId> 23 <scope>runtime</scope> 24 </dependency>
2、配置数据源
1 spring: 2 datasource: 3 username: xxxx 4 password: xxxx 5 url: jdbc:mysql://xxxx:3306/yidai?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true 6 driver-class-name: com.mysql.cj.jdbc.Driver 7 type: com.alibaba.druid.pool.DruidDataSource 8 9 initialSize: 5 10 minIdle: 5 11 maxActive: 20 12 maxWait: 60000 13 timeBetweenEvictionRunsMillis: 60000 14 minEvictableIdleTimeMillis: 300000 15 validationQuery: SELECT 1 FROM DUAL 16 testWhileIdle: true 17 testOnBorrow: false 18 testOnReturn: false 19 poolPreparedStatements: true 20 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 21 filters: stat,wall,log4j 22 maxPoolPreparedStatementPerConnectionSize: 20 23 useGlobalDataSourceStat: true 24 connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
3、DruibConifg配置类
package com.example.config; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.servlet.ServletRegistration; import javax.sql.DataSource; import java.util.Arrays; import java.util.HashMap; import java.util.Map; /** * @author : ppx * @date : 20:50 2021/1/31 */ @Configuration public class DruidConfig { @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource druib(){ return new DruidDataSource(); } @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", "");// 默认就是允许所有访问 initParams.put("deny", "10.18.172.124"); bean.setInitParameters(initParams); return bean; } @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; } }
4、页面访问
http://localhost:8080/druid/