欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot整合Druid数据连接池

程序员文章站 2022-04-24 12:39:42
SpringBoot整合Druid数据连接池 Druid是什么? Druid是Alibaba开源的的数据库连接池。Druid能够提供强大的监控和扩展功能。 在哪里下载druid maven*仓库: 怎么获取Druid的源码 Druid是一个开源项目,源码托管在github上,源代码仓库地址是 。同 ......

springboot整合druid数据连接池

druid是什么?

druid是alibaba开源的的数据库连接池。druid能够提供强大的监控和扩展功能。

在哪里下载druid

maven*仓库:

怎么获取druid的源码

druid是一个开源项目,源码托管在github上,源代码仓库地址是 。同时每次druid发布正式版本和快照的时候,都会把源码打包,你可以从上面的下载地址中找到相关版本的源码

怎么配置maven

<dependency>
    <groupid>com.alibaba</groupid>
    <artifactid>druid</artifactid>
    <!-- 可通过*仓库获取最新的版本号,这里用我项目中的版本号 -->
    <version>1.0.11</version>
</dependency>

开始配置

  • 首先是 application.properties
# 数据库配置
spring.datasource.driver-class-name=com.mysql.jdbc.driver
# 注:此处必须用 druid 的数据源
spring.datasource.type=com.alibaba.druid.pool.druiddatasource
spring.datasource.name=test
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.initialsize=5
spring.datasource.minidle=5
spring.datasource.maxactive=10
spring.datasource.maxwait=60000
spring.datasource.timebetweenevictionrunsmillis=3600000
spring.datasource.minevictableidletimemillis=3600000
spring.datasource.validationquery=select 'x'
spring.datasource.testwhileidle=true
spring.datasource.testonborrow=true
spring.datasource.testonreturn=false
spring.datasource.poolpreparedstatements=true
spring.datasource.maxpoolpreparedstatementperconnectionsize=20
spring.datasource.filters=stat,wall,slf4j
spring.datasource.connectionproperties=druid.stat.mergesql=true;druid.stat.slowsqlmillis=5000
spring.datasource.useglobaldatasourcestat=true

# druid 登录信息
druid.config.user-name=root
druid.config.password=root
druid.config.url-mapping=/druid/*
  • 封装数据库配置到 java bean
@configuration
@configurationproperties(prefix = "spring.datasource")
public class dbproperties {
    private string url;
    private string username;
    private string password;
    private string driverclassname;
    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;

    public string geturl() {
        return url;
    }

    public void seturl(string url) {
        this.url = url;
    }

    public string getusername() {
        return username;
    }

    public void setusername(string username) {
        this.username = username;
    }

    public string getpassword() {
        return password;
    }

    public void setpassword(string password) {
        this.password = password;
    }

    public string getdriverclassname() {
        return driverclassname;
    }

    public void setdriverclassname(string driverclassname) {
        this.driverclassname = driverclassname;
    }

    public int getinitialsize() {
        return initialsize;
    }

    public void setinitialsize(int initialsize) {
        this.initialsize = initialsize;
    }

    public int getminidle() {
        return minidle;
    }

    public void setminidle(int minidle) {
        this.minidle = minidle;
    }

    public int getmaxactive() {
        return maxactive;
    }

    public void setmaxactive(int maxactive) {
        this.maxactive = maxactive;
    }

    public int getmaxwait() {
        return maxwait;
    }

    public void setmaxwait(int maxwait) {
        this.maxwait = maxwait;
    }

    public int gettimebetweenevictionrunsmillis() {
        return timebetweenevictionrunsmillis;
    }

    public void settimebetweenevictionrunsmillis(int timebetweenevictionrunsmillis) {
        this.timebetweenevictionrunsmillis = timebetweenevictionrunsmillis;
    }

    public int getminevictableidletimemillis() {
        return minevictableidletimemillis;
    }

    public void setminevictableidletimemillis(int minevictableidletimemillis) {
        this.minevictableidletimemillis = minevictableidletimemillis;
    }

    public string getvalidationquery() {
        return validationquery;
    }

    public void setvalidationquery(string validationquery) {
        this.validationquery = validationquery;
    }

    public boolean istestwhileidle() {
        return testwhileidle;
    }

    public void settestwhileidle(boolean testwhileidle) {
        this.testwhileidle = testwhileidle;
    }

    public boolean istestonborrow() {
        return testonborrow;
    }

    public void settestonborrow(boolean testonborrow) {
        this.testonborrow = testonborrow;
    }

    public boolean istestonreturn() {
        return testonreturn;
    }

    public void settestonreturn(boolean testonreturn) {
        this.testonreturn = testonreturn;
    }

    public boolean ispoolpreparedstatements() {
        return poolpreparedstatements;
    }

    public void setpoolpreparedstatements(boolean poolpreparedstatements) {
        this.poolpreparedstatements = poolpreparedstatements;
    }

    public int getmaxpoolpreparedstatementperconnectionsize() {
        return maxpoolpreparedstatementperconnectionsize;
    }

    public void setmaxpoolpreparedstatementperconnectionsize(int maxpoolpreparedstatementperconnectionsize) {
        this.maxpoolpreparedstatementperconnectionsize = maxpoolpreparedstatementperconnectionsize;
    }

    public string getfilters() {
        return filters;
    }

    public void setfilters(string filters) {
        this.filters = filters;
    }

    public string getconnectionproperties() {
        return connectionproperties;
    }

    public void setconnectionproperties(string connectionproperties) {
        this.connectionproperties = connectionproperties;
    }

    public boolean isuseglobaldatasourcestat() {
        return useglobaldatasourcestat;
    }

    public void setuseglobaldatasourcestat(boolean useglobaldatasourcestat) {
        this.useglobaldatasourcestat = useglobaldatasourcestat;
    }

}
@configuration
@configurationproperties(prefix = "druid.config")
public class druidproperties {

    private string username;

    private string password;

    private string urlmapping;

    public string getusername() {
        return username;
    }

    public void setusername(string username) {
        this.username = username;
    }

    public string getpassword() {
        return password;
    }

    public void setpassword(string password) {
        this.password = password;
    }

    public string geturlmapping() {
        return urlmapping;
    }

    public void seturlmapping(string urlmapping) {
        this.urlmapping = urlmapping;
    }
}
  • 配置数据源
/**
 * 持久层相关配置
 */
@configuration
public class sessionfactoryconfig {

    private static final logger logger = loggerfactory.getlogger(sessionfactoryconfig.class);

    private final dbproperties db;

    @autowired
    public sessionfactoryconfig(dbproperties db) {
        this.db = db;
    }

    /**
     * 创建数据源
     */
    @bean
    // 如果访问 druid 时数据源页面显示 (*) property for user to setup 可用下面的 bean 注入方式
    // @bean(destroymethod = "close", initmethod = "init")
    @primary
    public datasource datasource() {
        final druiddatasource datasource = new druiddatasource();
        datasource.seturl(db.geturl());
        datasource.setusername(db.getusername());
        datasource.setpassword(db.getpassword());
        datasource.setdriverclassname(db.getdriverclassname());
        datasource.setinitialsize(db.getinitialsize());
        datasource.setminidle(db.getminidle());
        datasource.setmaxactive(db.getmaxactive());
        datasource.setmaxwait(db.getmaxwait()); datasource.settimebetweenevictionrunsmillis(db.gettimebetweenevictionrunsmillis());
        datasource.setminevictableidletimemillis(db.getminevictableidletimemillis());
        datasource.setvalidationquery(db.getvalidationquery());
        datasource.settestonreturn(db.istestonreturn());
        datasource.setpoolpreparedstatements(db.ispoolpreparedstatements());
        datasource.setmaxpoolpreparedstatementperconnectionsize(db.getmaxpoolpreparedstatementperconnectionsize());
        datasource.setuseglobaldatasourcestat(db.isuseglobaldatasourcestat());
        try {
            datasource.setfilters(db.getfilters());
        } catch (sqlexception e) {
            logger.info("druid configuration initialization filter: " + e);
        }
        datasource.setconnectionproperties(db.getconnectionproperties());
        return datasource;
    }
}
  • druid连接池配置
/**
 * druid连接池配置
 */
@configuration
public class druidconfiguration {

    private static final logger logger = loggerfactory.getlogger(druidconfiguration.class);

    private final druidproperties druidproperties;

    @autowired
    public druidconfiguration(druidproperties dp) {
        this.druidproperties = dp;
    }

    /**
     * 访问路径及用户名、密码
     */
    @bean
    public servletregistrationbean druidconfig() {
        logger.info("init druid servlet configuration ");
        final servletregistrationbean srb = new servletregistrationbean();
        srb.setservlet(new statviewservlet());
        logger.info("druid config info --> {}", druidproperties);
        srb.addurlmappings(druidproperties.geturlmapping());
        srb.addinitparameter("loginusername", druidproperties.getusername());
        srb.addinitparameter("loginpassword", druidproperties.getpassword());
        return srb;
    }

    /**
     * 配置过滤器
     */
    @bean
    public filterregistrationbean filterregistrationbean() {
        final filterregistrationbean filterregistrationbean = new filterregistrationbean();
        filterregistrationbean.setfilter(new webstatfilter());
        filterregistrationbean.addurlpatterns("/*");
        filterregistrationbean.addinitparameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterregistrationbean;
    }
}
  • 委托druid进行spring 监控
/**
 * 委托druid进行spring 监控
 */
@configuration
public class springmonitoringconfig {

    @bean(name = "druid-stat-interceptor")
    public druidstatinterceptor druidstatinterceptor() {
        return new druidstatinterceptor();
    }

    @bean(name = "druid-stat-pointcut")
    // 非单例
    @scope("prototype")
    public jdkregexpmethodpointcut druidstatpointcut() {
        final jdkregexpmethodpointcut pointcut = new jdkregexpmethodpointcut();
        pointcut.setpatterns("com.ajm.punch_the_clock.controller.*", "com.ajm.punch_the_clock.mapper.*");
        return pointcut;
    }

    @bean
    public defaultpointcutadvisor druidstatadvisor(@qualifier("druid-stat-interceptor") final druidstatinterceptor druidstatinterceptor,
                                                   @qualifier("druid-stat-pointcut") final jdkregexpmethodpointcut druidstatpointcut) {
        final defaultpointcutadvisor defaultpointadvisor = new defaultpointcutadvisor();
        defaultpointadvisor.setpointcut(druidstatpointcut);
        defaultpointadvisor.setadvice(druidstatinterceptor);
        return defaultpointadvisor;
    }

}
  • 项目启动,访问 http://127.0.0.1:端口号/druid

SpringBoot整合Druid数据连接池
SpringBoot整合Druid数据连接池

如果出现以上页面,恭喜你。整合 ok :-)