1 <dependency> 2 <groupId>com.alibaba</groupId> 3 <artifactId>druid</artifactId> 4 <version>1.1.21</version> 5 </dependency>
druid坐标
spring: datasource: username: root password: 2652779 url: jdbc:mysql://localhost:3306/shiro?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource #Spring Boot 默认是不注入这些属性值的,需要自己绑定 #druid 数据源专有配置 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:监控统计、log4j:日志记录、wall:防御sql注入 #如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority #则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
druid 配置:
1 @Configuration 2 public class DruidConfig { 3 4 @ConfigurationProperties(prefix = "spring.datasource") 5 @Bean 6 public DataSource druidDataSource() { 7 return new DruidDataSource(); 8 } 9 10 //后台监控 11 @Bean 12 public ServletRegistrationBean statViewServlet() { 13 14 ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*"); 15 16 //后台需要有人登录的登录密码 17 HashMap<String, String> initParameters = new HashMap<>(); 18 19 //增加配置 20 initParameters.put("loginUsername", "admin"); //登录的key 是固定的 loginUsername loginPassword 21 initParameters.put("loginPassword", "123456"); 22 23 //允许谁可以访问 24 initParameters.put("allow", ""); 25 26 //禁止谁可以访问 27 // initParameters.put("kuangsheng", "192.168.11.123"); 28 29 bean.setInitParameters(initParameters); 30 return bean; 31 } 32 33 //filter 34 @Bean 35 public FilterRegistrationBean webStatFilter() { 36 FilterRegistrationBean bean = new FilterRegistrationBean(); 37 38 bean.setFilter(new WebStatFilter()); 39 40 41 HashMap<String, String> initParment = new HashMap<>(); 42 //过滤请求 43 initParment.put("exclusions", "*.js,*.css,/druid/*"); 44 45 return bean; 46 } 47 48 49 }