1.添加依赖,在maven repository中搜索
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.21</version> </dependency>
2.配置application.yml
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
maxActive: 10
maxWait: 3000
maxIdle: 8
minIdle: 3
3.整合druid数据源
@Configuration public class DruidConfig { @Bean public DataSource druid(){ return new DruidDataSource(); } //配置druid的监控 //1、配置一个管理后台的servlet @Bean public ServletRegistrationBean statViewServlet(){ ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(),"druid/*"); Map<String,String> initParames = new HashMap<>(); initParames.put("loginUsername","admin"); initParames.put("loginPassword","123456"); initParames.put("allow","localhost"); bean.setInitParameters(initParames); return bean; } //2、配置一个web监控的filter @Bean public FilterRegistrationBean webStatFilter(){ FilterRegistrationBean bean = new FilterRegistrationBean(); bean.setFilter(new WebStatFilter()); Map<String,String> initParams = new HashMap<>(); initParams.put("exclusion","*.js,*.css,/druid/*"); bean.setInitParameters(initParams); bean.setUrlPatterns(Arrays.asList("/*")); return bean; } }