1.pom.xml 引入DruidDataSource,log4j
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2.application.yaml 指定数据源类型为DruidDataSource 以及相关配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
filters: wall,stat,slf4j
filter:
#sql监测
stat:
enabled: true
#sql合并
merge-sql: true
slow-sql-millis: 0
logback:
#日志记录
enabled: true
3.编写config类
@Configuration
public class DuriDataSourceConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource duridDataSource(){
return new DruidDataSource();
}
//后台监控
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean<StatViewServlet> bean=new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
Map<String, String> initParameters=new HashMap<>();
initParameters.put("loginUsername","admin");
initParameters.put("loginPassword","123456");
initParameters.put("allow","");
bean.setInitParameters(initParameters);
return bean;
}
}
4.访问url http://localhost:8083/druid/login.html