SpringBoot使用Druid数据源的配置方法
程序员文章站
2023-12-01 09:29:34
druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了c3p0、dbcp、proxool等db池的优点,同时加入了日志监控,可以很好的监控db池连接和sql的执行情况...
druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了c3p0、dbcp、proxool等db池的优点,同时加入了日志监控,可以很好的监控db池连接和sql的执行情况,可以说是针对监控而生的db连接池(据说是目前最好的连接池)
一、依赖
为了测试,使用jdbctemplate
<!-- jdbctemplate --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-jdbc</artifactid> </dependency> <!-- druid数据库连接池 --> <dependency> <groupid>com.alibaba</groupid> <artifactid>druid</artifactid> <version>1.0.26</version> </dependency> <!-- mysql connector --> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <scope>runtime</scope> </dependency>
二、druid配置
druid.properties #数据库设置 spring.datasource.driverclassname=com.mysql.jdbc.driver spring.datasource.url=jdbc:mysql://localhost:3306/uu_core?useunicode=true&characterencoding=utf8&autoreconnect=true&usessl=true spring.datasource.username=root spring.datasource.password=root #-------------------------- # 下面为连接池的补充设置,应用到上面所有数据源中 # 初始化大小,最小,最大 spring.datasource.initialsize=5 spring.datasource.minidle=1 spring.datasource.maxactive=50 # 配置获取连接等待超时的时间 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 # 打开pscache,并且指定每个连接上pscache的大小 spring.datasource.poolpreparedstatements=false #spring.datasource.maxpoolpreparedstatementperconnectionsize=20 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 spring.datasource.filters=stat,wall,log4j # 通过connectproperties属性来打开mergesql功能;慢sql记录 spring.datasource.connectionproperties=druid.stat.mergesql=true;druid.stat.slowsqlmillis=5000 # 合并多个druiddatasource的监控数据 #spring.datasource.useglobaldatasourcestat=true
三、实例化druid datasource
package cn.aduu.config; import com.alibaba.druid.pool.druiddatasource; import com.alibaba.druid.support.http.statviewservlet; import com.alibaba.druid.support.http.webstatfilter; import org.springframework.boot.context.properties.configurationproperties; 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.propertysource; import javax.sql.datasource; /** * @author zh * @classname cn.aduu.config.druidconfiguration * @description */ @configuration @propertysource(value = "classpath:druid.properties") public class druidconfiguration { @bean(destroymethod = "close", initmethod = "init") @configurationproperties(prefix = "spring.datasource") public datasource druiddatasource() { druiddatasource druiddatasource = new druiddatasource(); return druiddatasource; } /** * 注册一个statviewservlet * @return */ @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","123456"); //是否能够重置数据. servletregistrationbean.addinitparameter("resetenable","false"); return servletregistrationbean; } /** * 注册一个:filterregistrationbean * @return */ @bean public filterregistrationbean druidstatfilter(){ filterregistrationbean filterregistrationbean = new filterregistrationbean(new webstatfilter()); //添加过滤规则. filterregistrationbean.addurlpatterns("/*"); //添加不需要忽略的格式信息. filterregistrationbean.addinitparameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterregistrationbean; } }
四、监控
访问 , 使用上面配置的账号密码。
五、测试
@restcontroller public class hellocontroller{ private static final logger logger = loggerfactory.getlogger(hellocontroller.class); @autowired private jdbctemplate jdbctemplate; @requestmapping("hello") public list<map<string, object>> hello() { list<map<string, object>> list = jdbctemplate.queryforlist("select user,password from mysql.user ", new object[]{}); return list; } }
访问 localhost:8080/hello
[ { "user": "root", "password": "*81f5e21e35407d884a6cd4a731aebfb6af209e1b" }, { "user": "root", "password": "*81f5e21e35407d884a6cd4a731aebfb6af209e1b" } ]
总结
以上所述是小编给大家介绍的springboot使用druid数据源,希望对大家有所帮助
下一篇: SQL处理多级分类,查询结果呈树形结构