DRUID是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0、DBCP、PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB连接池(据说是目前最好的连接池,不知道速度有没有BoneCP快)。
1.引入依赖
<!-- data -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- data -->
2.写properties配置文件(文中用的是pg数据库,建议尽量将数据库配置信息写入配置文件,这样方便后期修改数据库配置,而不用重新编译)
########### data base properties ###########
spring.datasource.url=jdbc:postgresql://数据库地址:5432/数据库名称
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
pring.datasource.testOnReturn=false
spring.druid.monitor.username=admin
spring.druid.monitor.password=123456
spring.druid.monitor.servlet.url=/druid/*
spring.druid.monitor.initparameter=false
#spring.druid.monitor.filters=mergeStat,wall,log4j
3. datasource配置类
@Configuration
@EnableConfigurationProperties(DataSourceProperties.class)//明确指定需要用哪个实体类来装载配置信息
public class DruidConfig {
@Value("${spring.druid.monitor.username}")
String userName;
@Value("${spring.druid.monitor.password}")
String password;
@Value("${spring.druid.monitor.servlet.url}")
String url;
@Value("${spring.druid.monitor.initparameter}")
String initparameter;
@Value("${spring.druid.monitor.filters}")
String filters;
@Bean
@ConfigurationProperties("spring.datasource.*")
public DruidDataSource dataSource(DataSourceProperties properties) {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(properties.determineDriverClassName());
dataSource.setUrl(properties.determineUrl());
dataSource.setUsername(properties.determineUsername());
dataSource.setPassword(properties.determinePassword());
DatabaseDriver databaseDriver = DatabaseDriver.fromJdbcUrl(properties.determineUrl());
String validationQuery = databaseDriver.getValidationQuery();
if (validationQuery != null) {
dataSource.setTestOnBorrow(true);
dataSource.setValidationQuery(validationQuery);
}
try {
// 开启Druid的监控统计功能,mergeStat代替stat表示sql合并,wall表示防御SQL注入攻击
dataSource.setFilters(filters);
} catch (SQLException e) {
e.printStackTrace();
}
return dataSource;
}
@Bean
public ServletRegistrationBean DruidStatViewServlet(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(
new StatViewServlet(),url);
servletRegistrationBean.addInitParameter("loginUsername",userName);
servletRegistrationBean.addInitParameter("loginPassword",password);
servletRegistrationBean.addInitParameter("resetEnable",initparameter);
return servletRegistrationBean;
}
}