Druid是Java语言中最好的数据库连接池,并且能够提供强大的监控和扩展功能。
业界把 Druid 和 HikariCP 做对比后,虽说 HikariCP 的性能比 Druid 高,但是因为 Druid 包括很多维度的统计和分析功能,所以这也是大家都选择使用它的原因。
下面来说明如何在 spring Boot 中配置使用Druid
1、添加Maven依赖 (或jar包)
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.15</version>
</dependency>
2、配置数据源相关信息
#datasource
spring.datasource.url=jdbc:mysql://192.168.10.20:3306/test?useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true
spring.datasource.username = root
spring.datasource.password = password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#pool Initialization
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
#overtime
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
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat,wall,log4j
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
#spring.datasource.useGlobalDataSourceStat=tru
#server
server.port=8081
3、配置监控统计功能
package com.cl.config;
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 com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
/**
* druid配置 相当于在web.xml中
* @author cl
*/
@Configuration
public class WebXmlConfig {
/**
* 注册一个StatViewServlet 相当于在web.xml中声明了一个servlet
*/
@Bean
public ServletRegistrationBean druidStatViewServlet() {
// org.springframework.boot.context.embedded.ServletRegistrationBean提供类的进行注册.
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),
"/druid/*");
// 添加初始化参数:initParams
// 白名单:
servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
// IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not
// permitted to view this page.
servletRegistrationBean.addInitParameter("deny", "192.168.1.73");
// 登录查看信息的账号密码.
servletRegistrationBean.addInitParameter("loginUsername", "admin");
servletRegistrationBean.addInitParameter("loginPassword", "admin");
// 是否能够重置数据.
servletRegistrationBean.addInitParameter("resetEnable", "false");// 禁用HTML页面上的“Reset
// All”功能
return servletRegistrationBean;
}
/**
* 注册一个:filterRegistrationBean 相当于在web.xml中声明了一个Filter
*/
@Bean
public FilterRegistrationBean druidStatFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
filterRegistrationBean.setName("druidWebStatFilter");
// 添加过滤规则.
filterRegistrationBean.addUrlPatterns("/*");
// 添加忽略的格式信息.
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
}
4、Application.java
dao层的实现,省略~
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import com.alibaba.druid.pool.DruidDataSource;
@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan(basePackages = { "com.cl" }) // 指定spring管理的bean所在的包
@MapperScan("com.cl.dao") // 指定mybatis的mapper接口所在的包
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
// 创建数据源
@Bean
@ConfigurationProperties(prefix = "spring.datasource") // 指定数据源的前缀,在application.properties文件中指定
public DruidDataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
return dataSource;
}
// 创建SqlSessionFactory
@Bean
public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));
return sqlSessionFactoryBean.getObject();
}
// 创建事物管理器
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
}
4、测试
帐号:admin
密码: admin
5、更多使用和常见问题
详见:https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
本文只是简单的对如何使用Druid做了说明,其实官方包括各种很细致的说明,建议大家还是多看看官方说明以官方为准,国人开发的说明自然也都是中文,通俗易懂。
官方资料直达地址:
Druid 首页
https://github.com/alibaba/druid/wiki/%E9%A6%96%E9%A1%B5
Druid 常见问题
https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
Druid 发布版
https://github.com/alibaba/druid/releases
Druid 源码
https://github.com/alibaba/druid