怎么在SpringBoot中使用德鲁伊数据库连接池

  介绍

这篇文章主要介绍怎么在SpringBoot中使用德鲁伊数据库连接池,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

德鲁伊是阿里开源的一款数据库连接池,除了常规的连接池功能外,它还提供了强大的监控和扩展功能。这对没有做数据库监控的小项目有很大的吸引力。

下列步骤可以让你无脑式的在SpringBoot2.x中使用德鲁伊。

1。Maven中的pom文件

& lt; dependency>   & lt; groupId> com.alibaba   & lt; artifactId> druid   & lt; version> 1.1.14   & lt;/dependency>   & lt; dependency>   & lt; groupId> log4j   & lt; artifactId> log4j   & lt; version> 1.2.17   & lt;/dependency>

使用春天Boot-2。x时,如果未引入log4j,在配置德鲁伊时,会遇到造成的:. lang。ClassNotFoundException: org.apache.log4j。优先级的报的错。因为弹簧引导默认使用的是log4j2。

2。SpringBoot配置文件

下面是一个完整的yml文件的,其中使用mybatis作为数据库访问框架

服务器:   ,servlet:   ,上下文路径:/,会议:   超时:才能60米   ,端口:8080      春天:   ,数据源:   ,url: jdbc: mysql://127.0.0.1:9080/医院吗? allowMultiQueries=true& useUnicode=true& characterEncoding=UTF-8& zeroDateTimeBehavior=convertToNull   ,用户名:根   ,密码:根   ,#环境,dev | |专业测试   ,配置文件:   活动:才能开发   ,driver-class-name: com.mysql.cj.jdbc.Driver      ,# # # # # # # # # # # # # # # # # # # # # # # # # #,德鲁伊配置,# # # # # # # # # # # # # # # # # # # # # # # # # #   类型:大敌;com.alibaba.druid.pool.DruidDataSource   ,#初始化大小,最小,最大,   initialSize:大敌;5   ,minIdle: 1   ,maxActive: 20   #,才能配置获取连接等待超时的时间,   60000年,maxWait:   ,#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒,   60000年,timeBetweenEvictionRunsMillis:   ,#配置一个连接在池中最小生存的时间,单位是毫秒,   300000年,minEvictableIdleTimeMillis:   ,#校验SQL,甲骨文配置,validationQuery=SELECT  1,得到双,如果不配validationQuery项,则下面三项配置无用,   ,validationQuery: SELECT  1,得到双   ,testWhileIdle:真的   ,testOnBorrow:假的   ,testOnReturn:假的   ,#打开PSCache,并且指定每个连接上PSCache的大小,   ,poolPreparedStatements:真的   ,maxPoolPreparedStatementPerConnectionSize: 20   ,#配置监控统计拦截的过滤器,去掉后监控界面sql无法统计,& # 39;墙# 39;用于防火墙,   ,过滤器:统计,墙,log4j   ,#通过connectProperties属性来打开mergeSql功能,慢SQL记录,   ,connectionProperties: druid.stat.mergeSql=true; druid.stat.slowSqlMillis=5000   ,#合并多个DruidDataSource的监控数据,   ,useGlobalDataSourceStat:真的   ,   #,Mybatis配置   mybatis:   ,mapperLocations:类路径:mapper/* . xml,类路径:mapper/扩展/* . xml   ,配置:   ,map-underscore-to-camel-case:真的   以前,log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

3。配置德鲁伊数据源实例

由于德鲁伊暂时不春天在引导中的直接支持,故需要进行配置信息的定制:

SpringBoot中的配置信息无法再德鲁伊中直接生效,需要在春天容器中实现一个数据源实例。

import  java.sql.SQLException;   import  javax.sql.DataSource;   import  org.apache.log4j.Logger;   import  org.springframework.beans.factory.annotation.Value;   import  org.springframework.context.annotation.Bean;   import  org.springframework.context.annotation.Configuration;   import  org.springframework.context.annotation.Primary;      import  com.alibaba.druid.pool.DruidDataSource;      @ configuration   public  class  DruidDBConfig  {   时间=private  Logger  Logger  Logger.getLogger (this.getClass ());//log4j日志      @ value (“$ {spring.datasource.url}“)   private  String  dbUrl;      @ value (“$ {spring.datasource.username}“)   private  String 用户名;      @ value (“$ {spring.datasource.password}“)   private  String 密码;      @ value (“$ {spring.datasource.driver-class-name}“)   private  String  driverClassName;      @ value (“$ {spring.datasource.initialSize}“)   private  int  initialSize;      @ value (“$ {spring.datasource.minIdle}“)   private  int  minIdle;      @ value (“$ {spring.datasource.maxActive}“)   private  int  maxActive;      @ value (“$ {spring.datasource.maxWait}“)   private  int  maxWait;      @ value (“$ {spring.datasource.timeBetweenEvictionRunsMillis}“)   private  int  timeBetweenEvictionRunsMillis;      @ value (“$ {spring.datasource.minEvictableIdleTimeMillis}“)   private  int  minEvictableIdleTimeMillis;      @ value (“$ {spring.datasource.validationQuery}“)   private  String  validationQuery;      @ value (“$ {spring.datasource.testWhileIdle}“)   private  boolean  testWhileIdle;      @ value (“$ {spring.datasource.testOnBorrow}“)   private  boolean  testOnBorrow;      @ value (“$ {spring.datasource.testOnReturn}“)   private  boolean  testOnReturn;      @ value (“$ {spring.datasource.poolPreparedStatements}“)   private  boolean  poolPreparedStatements;      @ value (“$ {spring.datasource.maxPoolPreparedStatementPerConnectionSize}“)   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在SpringBoot中使用德鲁伊数据库连接池