1. 程式人生 > >集成druid數據源【JWordPress前臺項目實戰】

集成druid數據源【JWordPress前臺項目實戰】

person perm prepare rom indent nconf sin 寫在前面 enter

寫在前面

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

技術分享圖片

實戰項目介紹

項目介紹

為了滿足Java新手朋友課程要求,我特出此教程,由於時間倉促的問題,代碼寫得不好之處的地方還請多多包涵。

目標如下

  1. 優化wordpress效率低下的問題(目前博主文章數量大概10萬+)

  2. 讓群裏面初級Java朋友們更快上手springboot應用

GIT地址:https://gitee.com/jxuasea/JWordpress

技術分享圖片


集成druid數據源【JWordPress前臺項目實戰】