原创

集成druid数据源【JWordPress前台项目实战】

写在前面

  DRUID是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0、DBCP、PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,下面我们来讲讲springboot如何继承druid连接池

代码

pom.xml添加相应的jar
<!--alibaba 数据库连接池 druid 配置 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.28</version> </dependency>
配置druid代码
/** * MIT License * Copyright (c) 2018 haihua.liu * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package cn.liuhaihua.web.config; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 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.Primary; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.stereotype.Component; import org.springframework.transaction.PlatformTransactionManager; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; /** * * @ClassName: ApplicationConfiguration * @Description: druid配置类 * @author Liuhaihua * @date 2018年6月26日 * */ @Configuration @Component public class DruidConfiguration { // 其中 dataSource 框架会自动为我们注入 @Bean public PlatformTransactionManager txManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } /** * @Title: dataSource * @Description: 加载数据源 * @param @return 参数 * @return DataSource 返回类型 * @throws */ @Bean(name = "dataSource") @Qualifier(value = "dataSource") @Primary @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return DataSourceBuilder.create().build(); } /** * @Title: DruidStatViewServle2 * @Description: 注册一个StatViewServlet * @param @return 参数 * @return ServletRegistrationBean 返回类型 * @throws */ @Bean public ServletRegistrationBean DruidStatViewServle2() { ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); // 添加初始化参数:initParams /** 白名单,如果不配置或value为空,则允许所有 */ // servletRegistrationBean.addInitParameter("allow","127.0.0.1,192.0.0.1"); /** 黑名单,与白名单存在相同IP时,优先于白名单 */ // servletRegistrationBean.addInitParameter("deny","192.0.0.1"); /** 用户名 */ servletRegistrationBean.addInitParameter("loginUsername", "admin"); /** 密码 */ servletRegistrationBean.addInitParameter("loginPassword", "123456"); /** 禁用页面上的“Reset All”功能 */ servletRegistrationBean.addInitParameter("resetEnable", "false"); return servletRegistrationBean; } /** * @Title: druidStatFilter2 * @Description: 注册一个:WebStatFilter * @param @return 参数 * @return FilterRegistrationBean 返回类型 * @throws */ @Bean public FilterRegistrationBean druidStatFilter2() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); /** 过滤规则 */ filterRegistrationBean.addUrlPatterns("/*"); /** 忽略资源 */ filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*"); return filterRegistrationBean; } }
属性文件配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.filters=stat,log4j spring.datasource.initialSize=5 spring.datasource.maxActive=200 spring.datasource.maxOpenPreparedStatements=-1 spring.datasource.maxWait=60000 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.maxEvictableIdleTimeMillis=600000 spring.datasource.minIdle=20 spring.datasource.name=om_dev_datasource spring.datasource.poolPreparedStatements=false spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false spring.datasource.testWhileIdle=true spring.datasource.timeBetweenEvictionRunsMillis=60000 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.url=jdbc:mysql://127.0.0.1:3306/jwordpress?allowMultiQueries=true&autoReconnect=true spring.datasource.username=root spring.datasource.password=123456 spring.datasource.validationQuery=select 'x'

测试

启动应用,然后访问http://127.0.0.1:8090/druid/index.html 1530786841(1)

实战项目介绍

项目介绍: 为了满足Java新手朋友课程要求,我特出此教程,由于时间仓促的问题,代码写得不好之处的地方还请多多包涵。 目标如下
  1. 优化wordpress效率低下的问题(目前博主文章数量大概10万+)
  2. 让群里面初级Java朋友们更快上手springboot应用
GIT地址:https://gitee.com/jxuasea/JWordpress
正文到此结束
Loading...