SpringBoot导入Druid连接池,dbType not support

druid是由阿里开源,据说是各方面性能比较好的一个数据库连接池,具有很强大的连接监控功能。

在Springboot项目中使用Druid:

maven导入druid依赖:

   
        com.alibaba
        druid
        1.1.10
    

druid的使用需要做很多的数据库连接参数配置,关于参数配置可参考Druid项目github地址

SpringBoot导入Druid连接池,dbType not support_第1张图片

在application.properties文件中配置:

#mybatis数据库连接
spring.datasource.url=jdbc:mysql://数据库地址
spring.datasource.username=数据库连接用户名
spring.datasource.password=数据库连接密码
#数据库连接
#druid  datasouce database settings begin
spring.druid.datasource.type=mysql
spring.druid.datasource.driverClassName=com.mysql.jdbc.Driver
# 下面为连接池的补充设置,应用到上面所有数据源中
spring.druid.datasource.druidLoginName=Druid登录用户名
spring.druid.datasource.druidLoginPwd=Druid登录密码
# 初始化大小,最小,最大
spring.druid.datasource.initialSize=5
spring.druid.datasource.minIdle=5
spring.druid.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.druid.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.druid.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.druid.datasource.minEvictableIdleTimeMillis=300000
spring.druid.datasource.validationQuery=SELECT 1 FROM DUAL
spring.druid.datasource.testWhileIdle=true
spring.druid.datasource.testOnBorrow=false
spring.druid.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.druid.datasource.poolPreparedStatements=true
spring.druid.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.druid.datasource.filters=stat,wall,config
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.druid.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
spring.druid.datasource.useGlobalDataSourceStat=true

要注意数据库type的值,不然再启动的时候会出现:

dbType not support : com.alibaba.druid.pool.DruidDataSource...

然后代码配置:

import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
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 org.springframework.context.annotation.Primary;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

/**
 * Druid 连接配置
 */
@Configuration
public class DruidDataSourceConfig {

    @Value("${spring.datasource.url}")
    private String dbUrl;

    @Value("${spring.druid.datasource.type}")
    private String dbType;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.druid.datasource.driverClassName}")
    private String driverClassName;

    @Value("${spring.druid.datasource.initialSize}")
    private int initialSize;

    @Value("${spring.druid.datasource.minIdle}")
    private int minIdle;

    @Value("${spring.druid.datasource.maxActive}")
    private int maxActive;

    @Value("${spring.druid.datasource.maxWait}")
    private int maxWait;

    @Value("${spring.druid.datasource.timeBetweenEvictionRunsMillis}")
    private int timeBetweenEvictionRunsMillis;

    @Value("${spring.druid.datasource.minEvictableIdleTimeMillis}")
    private int minEvictableIdleTimeMillis;

    @Value("${spring.druid.datasource.validationQuery}")
    private String validationQuery;

    @Value("${spring.druid.datasource.testWhileIdle}")
    private boolean testWhileIdle;

    @Value("${spring.druid.datasource.testOnBorrow}")
    private boolean testOnBorrow;

    @Value("${spring.druid.datasource.testOnReturn}")
    private boolean testOnReturn;

    @Value("${spring.druid.datasource.poolPreparedStatements}")
    private boolean poolPreparedStatements;

    @Value("${spring.druid.datasource.filters}")
    private String filters;

    @Value("${spring.druid.datasource.connectionProperties}")
    private String connectionProperties;

    @Value("${spring.druid.datasource.useGlobalDataSourceStat}")
    private boolean useGlobalDataSourceStat;

    @Value("${spring.druid.datasource.druidLoginName}")
    private String druidLoginName;

    @Value("${spring.druid.datasource.druidLoginPwd}")
    private String druidPassword;

    @Bean(name = "dataSource", destroyMethod = "close", initMethod = "init")
    @Primary
    public DataSource dataSource() {
        DruidDataSource datasource = new DruidDataSource();
        try {
            datasource.setUrl(this.dbUrl);
            datasource.setDbType(dbType);
            datasource.setUsername(username);
            datasource.setPassword(password);
            datasource.setDriverClassName(driverClassName);
            datasource.setInitialSize(initialSize);
            datasource.setMinIdle(minIdle);
            datasource.setMaxActive(maxActive);
            datasource.setMaxWait(maxWait);
            datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
            datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
            datasource.setValidationQuery(validationQuery);
            datasource.setTestWhileIdle(testWhileIdle);
            datasource.setTestOnBorrow(testOnBorrow);
            datasource.setTestOnReturn(testOnReturn);
            datasource.setPoolPreparedStatements(poolPreparedStatements);
            datasource.setFilters(filters);
            datasource.setConnectionProperties(connectionProperties);
            datasource.setUseGlobalDataSourceStat(useGlobalDataSourceStat);
        } catch (SQLException e) {
           e.printStackTrace();
        }
        return datasource;
    }

    /////////  下面是druid 监控访问的设置  /////////////////
    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");  //url 匹配
        reg.addInitParameter("allow", ""); // IP白名单 (没有配置或者为空,则允许所有访问)
        reg.addInitParameter("deny", ""); //IP黑名单 (存在共同时,deny优先于allow)
        reg.addInitParameter("loginUsername", this.druidLoginName);//登录名
        reg.addInitParameter("loginPassword", this.druidPassword);//登录密码
        reg.addInitParameter("resetEnable", "false"); // 禁用HTML页面上的“Reset All”功能
        return reg;
    }

    @Bean(name = "druidWebStatFilter")
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); //忽略资源
        filterRegistrationBean.addInitParameter("profileEnable", "true");
        filterRegistrationBean.addInitParameter("principalCookieName", "USER_COOKIE");
        filterRegistrationBean.addInitParameter("principalSessionName", "USER_SESSION");
        return filterRegistrationBean;
    }
}

关于参数的解释已经很详细了

启动项目,浏览器访问http://localhost:8080/druid,会自动跳转登录界面,会上面配置的用户名密码登录即可。
SpringBoot导入Druid连接池,dbType not support_第2张图片
登录成功:
SpringBoot导入Druid连接池,dbType not support_第3张图片

你可能感兴趣的:(SpringBoot)