目录
1.项目结构
2.代码
3.集成效果
源码下载:https://download.csdn.net/download/adam_zs/28993988https://download.csdn.net/download/adam_zs/28993988
注意:application.yaml文件中配置数据源和druid的参数都要放在spring.datasource下边,要不然启动项目会报错!
spring: datasource: username: root password: 123456 url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/sakila?serverTimezone=UTC&useUnicode=true@characterEncoding=utf-8 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
package com.wangzs.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.Servlet; import javax.sql.DataSource; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @Configuration public class DruidConfig { @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource druidDataSource() { return new DruidDataSource(); } @Bean public ServletRegistrationBean druidServletRegistrationBean() { ServletRegistrationBean<Servlet> servletRegistrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*"); Map<String, String> initParams = new HashMap<>(); initParams.put("loginUsername", "admin"); initParams.put("loginPassword", "123456"); // 后台允许谁可以访问 // initParams.put("allow", "localhost"):表示只有本机可以访问 // initParams.put("allow", ""):为空或者为null时,表示允许所有访问 initParams.put("allow", ""); // deny:Druid 后台拒绝谁访问 // initParams.put("msb", "192.168.1.20");表示禁止此ip访问 servletRegistrationBean.setInitParameters(initParams); return servletRegistrationBean; } // 配置 Druid 监控 之 web 监控的 filter // WebStatFilter:用于配置Web和Druid数据源之间的管理关联监控统计 @Bean public FilterRegistrationBean webStatFilter() { FilterRegistrationBean bean = new FilterRegistrationBean(); bean.setFilter(new WebStatFilter()); // exclusions:设置哪些请求进行过滤排除掉,从而不进行统计 Map<String, String> initParams = new HashMap<>(); initParams.put("exclusions", "*.js,*.css,/druid/*"); bean.setInitParameters(initParams); // "/*" 表示过滤所有请求 bean.setUrlPatterns(Arrays.asList("/*")); return bean; } }
package com.wangzs; import javax.sql.DataSource; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jdbc.core.JdbcTemplate; import com.alibaba.druid.pool.DruidDataSource; @SpringBootTest class StudySpringBootJdbcApplicationTests { @Autowired private DruidDataSource druidDataSource; @Autowired private JdbcTemplate jdbcTemplate; @Test public void test_jdbcTemplate() { DataSource dataSource = jdbcTemplate.getDataSource(); System.out.println("==test_jdbcTemplate==" + dataSource); } @Test public void test_druid() { System.out.println("==druidDataSource.getInitialSize()==" + druidDataSource.getInitialSize()); System.out.println("==druidDataSource.getMaxActive()==" + druidDataSource.getMaxActive()); } }