参考:https://blog.csdn.net/saytime/article/details/78963121
有很多类似的博客,大概过了一遍整合一下,配置基本一样,自己记录一下。
1.pom.xml
添加jpa或jdbc依赖,默认引入tomcat-jdbc连接池,这里可以排除该依赖;其余依次添加mysql、druid依赖。
<!-- jpa -->
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
-->
<!-- jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<!-- 排除tomcat-jdbc依赖 -->
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
2.application.yml
配置连接池,直接在主配置文件中,也可以自定义配置文件。
spring:
datasource:
# type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/datasource
username: root
password: root
# druid config
# 初始化大小,最小,最大
initialSize: 5
minIdle: 5
maxActive: 20
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
# 校验SQL,Oracle配置 spring.datasource.validationQuery=SELECT 1 FROM DUAL,如果不配validationQuery项,则下面三项配置无用
validationQuery: SELECT 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打开PSCache,并且指定每个连接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
useGlobalDataSourceStat: true
3.DataSourceProperties
@Component
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceProperties {
private String driverClassName;
private String url;
private String username;
private String password;
private int initialSize;
private int minIdle;
private int maxActive;
private int maxWait;
private int timeBetweenEvictionRunsMillis;
private int minEvictableIdleTimeMillis;
private String validationQuery;
private boolean testWhileIdle;
private boolean testOnBorrow;
private boolean testOnReturn;
private boolean poolPreparedStatements;
private int maxPoolPreparedStatementPerConnectionSize;
private String filters;
private String connectionProperties;
private boolean useGlobalDataSourceStat;
get、set略
}
4.自定义DataSource
自定义使用Druid数据源,不会再创建默认的数据源。
@Configuration
public class DruidSpringConfig {
@Autowired
private DataSourceProperties dataSourceProperties;
@Bean
public DataSource dataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setDriverClassName(dataSourceProperties.getDriverClassName());
datasource.setUrl(dataSourceProperties.getUrl());
datasource.setUsername(dataSourceProperties.getUsername());
datasource.setPassword(dataSourceProperties.getPassword());
// druid configuration
datasource.setInitialSize(dataSourceProperties.getInitialSize());
datasource.setMinIdle(dataSourceProperties.getMinIdle());
datasource.setMaxActive(dataSourceProperties.getMaxActive());
datasource.setMaxWait(dataSourceProperties.getMaxWait());
datasource.setTimeBetweenEvictionRunsMillis(dataSourceProperties.getTimeBetweenEvictionRunsMillis());
datasource.setMinEvictableIdleTimeMillis(dataSourceProperties.getMinEvictableIdleTimeMillis());
datasource.setValidationQuery(dataSourceProperties.getValidationQuery());
datasource.setTestWhileIdle(dataSourceProperties.isTestWhileIdle());
datasource.setTestOnBorrow(dataSourceProperties.isTestOnBorrow());
datasource.setTestOnReturn(dataSourceProperties.isTestOnReturn());
datasource.setPoolPreparedStatements(dataSourceProperties.isPoolPreparedStatements());
datasource.setMaxPoolPreparedStatementPerConnectionSize(dataSourceProperties.getMaxPoolPreparedStatementPerConnectionSize());
datasource.setUseGlobalDataSourceStat(dataSourceProperties.isUseGlobalDataSourceStat());
try {
datasource.setFilters(dataSourceProperties.getFilters());
} catch (SQLException e) {
System.err.println("druid configuration initialization filter: " + e);
}
datasource.setConnectionProperties(dataSourceProperties.getConnectionProperties());
return datasource;
}
}
5.监控
5.1配置过滤器
import com.alibaba.druid.support.http.WebStatFilter;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
@WebFilter(filterName = "druidWebStatFilter", urlPatterns = "/*",
initParams = {
@WebInitParam(name = "exclusions", value = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略资源
})
public class DruidFilter extends WebStatFilter {
}
5.2配置Servlet
import com.alibaba.druid.support.http.StatViewServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
/**
* @author Mo Qing Gen
*/
@WebServlet(urlPatterns = "/druid/*",
initParams = {
// @WebInitParam(name="allow",value="192.168.16.110,127.0.0.1"),// IP白名单 (没有配置或者为空,则允许所有访问)
// @WebInitParam(name="deny",value="192.168.16.111"),// IP黑名单 (存在共同时,deny优先于allow)
@WebInitParam(name = "loginUsername", value = "admin"),// 用户名
@WebInitParam(name = "loginPassword", value = "123456"),// 密码
@WebInitParam(name = "resetEnable", value = "false")// 禁用HTML页面上的“Reset All”功能
})
public class DruidServlet extends StatViewServlet {
}
5.3主配置文件类
添加@ServletComponentScan注解,扫描5.1、5.2配置的过滤器和Servlet,否则不起作用。
@ServletComponentScan("com.momo.springbootdatasourcedruid.config.servlet")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
6.测试
ip:8080/项目名/druid/login.html,账号密码同DruidServlet配置