springboot+mybatis+druid 多資料來源
Druid介紹和使用
在使用Druid之前,先來簡單的瞭解下Druid。
Druid是一個數據庫連線池。Druid可以說是目前最好的資料庫連線池!因其優秀的功能、效能和擴充套件性方面,深受開發人員的青睞。
Druid已經在阿里巴巴部署了超過600個應用,經過一年多生產環境大規模部署的嚴苛考驗。Druid是阿里巴巴開發的號稱為監控而生的資料庫連線池!
同時Druid不僅僅是一個數據庫連線池,Druid 核心主要包括三部分:
- 基於Filter-Chain模式的外掛體系。
- DruidDataSource 高效可管理的資料庫連線池。
- SQLParser
Druid的主要功能如下:
- 是一個高效、功能強大、可擴充套件性好的資料庫連線池。
- 可以監控資料庫訪問效能。
- 資料庫密碼加密
- 獲得SQL執行日誌
- 擴充套件JDBC
介紹方面這塊就不再多說,具體的可以看官方文件。
那麼開始介紹Druid如何使用。
首先是Maven依賴,只需要新增druid這一個jar就行了。
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.8</version> </dependency>
配置方面,主要的只需要在application.properties或application.yml新增如下就可以了。
說明:因為這裡我是用來兩個資料來源,所以稍微有些不同而已。Druid 配置的說明在下面中已經說的很詳細了,這裡我就不在說明了。
application.yml
server: port: 8080 servlet: context-path: /equaker spring: #主資料來源 master: datasource: type: com.alibaba.druid.pool.DruidDataSource name: master url: jdbc:mysql://localhost:3306/wdz?useUnicode=true&characterEncoding=utf-8 username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver #從資料來源 cluster: datasource: type: com.alibaba.druid.pool.DruidDataSource name: cluster url: jdbc:mysql://localhost:3306/yufu?useUnicode=true&characterEncoding=utf-8 username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver datasource: druid: # 下面為連線池的補充設定,應用到上面所有資料來源中 # 初始化大小,最小,最大 initialSize: 5 minIdle: 5 maxActive: 20 # 配置獲取連線等待超時的時間 maxWait: 60000 # 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一個連線在池中最小生存的時間,單位是毫秒 minEvictableIdleTimeMillis: 30000 validationQuery: SELECT 1 validationQueryTimeout: 10000 testWhileIdle: true testOnBorrow: false testOnReturn: false # 開啟PSCache,並且指定每個連線上PSCache的大小 poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 filters: stat,wall # 通過connectProperties屬性來開啟mergeSql功能;慢SQL記錄 connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 合併多個DruidDataSource的監控資料 useGlobalDataSourceStat: true mybatis: # 已經再 masterdatasource/clusterdatasource指定了mapper對映檔案,所以不用配置 # mapper-locations: classpath:master/*Mapper.xml type-aliases-package: com.equaker.model configuration: database-id: db1 #pagehelper pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql logging: level: com.equaker.mapper : debug
成功添加了配置檔案之後,我們再來編寫Druid相關的類。
首先是MasterDataSourceConfig.java這個類,這個是預設的資料來源配置類。
MasterDataSourceConfig.hava
package com.equaker.druid;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
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 org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
import java.sql.SQLException;
/**
* 主資料來源
*/
@Configuration
@MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterDataSourceConfig {
static final String PACKAGE = "com.equaker.mapper.master";
static final String MAPPER_LOCATION = "classpath:mapper/master/*.xml";
@Value("${spring.master.datasource.url}")
private String url;
@Value("${spring.master.datasource.username}")
private String username;
@Value("${spring.master.datasource.password}")
private String password;
@Value("${spring.master.datasource.driver-class-name}")
private String driverClassName;
@Value("${spring.master.datasource.name}")
private String name;
@Value("${spring.datasource.druid.initialSize}")
private int initialSize;
@Value("${spring.datasource.druid.minIdle}")
private int minIdle;
@Value("${spring.datasource.druid.maxActive}")
private int maxActive;
@Value("${spring.datasource.druid.maxWait}")
private int maxWait;
@Value("${spring.datasource.druid.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.druid.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis;
@Value("${spring.datasource.druid.validationQuery}")
private String validationQuery;
@Value("${spring.datasource.druid.testWhileIdle}")
private boolean testWhileIdle;
@Value("${spring.datasource.druid.testOnBorrow}")
private boolean testOnBorrow;
@Value("${spring.datasource.druid.testOnReturn}")
private boolean testOnReturn;
@Value("${spring.datasource.druid.poolPreparedStatements}")
private boolean poolPreparedStatements;
@Value("${spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize}")
private int maxPoolPreparedStatementPerConnectionSize;
@Value("${spring.datasource.druid.filters}")
private String filters;
@Value("{spring.datasource.druid.connectionProperties}")
private String connectionProperties;
@Bean(name = "masterDataSource")
@Primary
public DataSource masterDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setName(name);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClassName);
//具體配置
dataSource.setInitialSize(initialSize);
dataSource.setMinIdle(minIdle);
dataSource.setMaxActive(maxActive);
dataSource.setMaxWait(maxWait);
dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
dataSource.setValidationQuery(validationQuery);
dataSource.setTestWhileIdle(testWhileIdle);
dataSource.setTestOnBorrow(testOnBorrow);
dataSource.setTestOnReturn(testOnReturn);
dataSource.setPoolPreparedStatements(poolPreparedStatements);
dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
try {
dataSource.setFilters(filters);
} catch (SQLException e) {
e.printStackTrace();
}
dataSource.setConnectionProperties(connectionProperties);
return dataSource;
}
@Bean(name = "masterTransactionManager")
@Primary
public DataSourceTransactionManager masterTransactionManager() {
return new DataSourceTransactionManager(masterDataSource());
}
@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(MasterDataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
ClusterDataSourceConfig.java
package com.equaker.druid;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
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 org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
import java.sql.SQLException;
/**
* 副資料來源
*/
@Configuration
@MapperScan(basePackages = ClusterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "clusterSqlSessionFactory")
public class ClusterDataSourceConfig {
static final String PACKAGE = "com.equaker.mapper.cluster";
static final String MAPPER_LOCATION = "classpath:mapper/cluster/*.xml";
@Value("${spring.cluster.datasource.url}")
private String url;
@Value("${spring.cluster.datasource.username}")
private String username;
@Value("${spring.cluster.datasource.password}")
private String password;
@Value("${spring.cluster.datasource.driver-class-name}")
private String driverClassName;
@Value("${spring.cluster.datasource.name}")
private String name;
@Value("${spring.datasource.druid.initialSize}")
private int initialSize;
@Value("${spring.datasource.druid.minIdle}")
private int minIdle;
@Value("${spring.datasource.druid.maxActive}")
private int maxActive;
@Value("${spring.datasource.druid.maxWait}")
private int maxWait;
@Value("${spring.datasource.druid.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.druid.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis;
@Value("${spring.datasource.druid.validationQuery}")
private String validationQuery;
@Value("${spring.datasource.druid.testWhileIdle}")
private boolean testWhileIdle;
@Value("${spring.datasource.druid.testOnBorrow}")
private boolean testOnBorrow;
@Value("${spring.datasource.druid.testOnReturn}")
private boolean testOnReturn;
@Value("${spring.datasource.druid.poolPreparedStatements}")
private boolean poolPreparedStatements;
@Value("${spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize}")
private int maxPoolPreparedStatementPerConnectionSize;
@Value("${spring.datasource.druid.filters}")
private String filters;
@Value("{spring.datasource.druid.connectionProperties}")
private String connectionProperties;
@Bean(name = "clusterDataSource")
public DataSource masterDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setName(name);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClassName);
//具體配置
dataSource.setInitialSize(initialSize);
dataSource.setMinIdle(minIdle);
dataSource.setMaxActive(maxActive);
dataSource.setMaxWait(maxWait);
dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
dataSource.setValidationQuery(validationQuery);
dataSource.setTestWhileIdle(testWhileIdle);
dataSource.setTestOnBorrow(testOnBorrow);
dataSource.setTestOnReturn(testOnReturn);
dataSource.setPoolPreparedStatements(poolPreparedStatements);
dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
try {
dataSource.setFilters(filters);
} catch (SQLException e) {
e.printStackTrace();
}
dataSource.setConnectionProperties(connectionProperties);
return dataSource;
}
@Bean(name = "clusterTransactionManager")
public DataSourceTransactionManager masterTransactionManager() {
return new DataSourceTransactionManager(masterDataSource());
}
@Bean(name = "clusterSqlSessionFactory")
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("clusterDataSource") DataSource masterDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(ClusterDataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
其中這兩個註解說明下:
-
@Primary :標誌這個 Bean 如果在多個同類 Bean 候選時,該 Bean
優先被考慮。多資料來源配置的時候注意,必須要有一個主資料來源,用 @Primary 標誌該 Bean。 -
@MapperScan: 掃描 Mapper 介面並容器管理。
需要注意的是sqlSessionFactoryRef 表示定義一個唯一 SqlSessionFactory 例項。
上面的配置完之後,就可以將Druid作為連線池使用了。但是Druid並不簡簡單單的是個連線池,它也可以說是一個監控應用,它自帶了web監控介面,可以很清晰的看到SQL相關資訊。
在SpringBoot中運用Druid的監控作用,只需要編寫StatViewServlet和WebStatFilter類,實現註冊服務和過濾規則。這裡我們可以將這兩個寫在一起,使用@Configuration和@Bean。
為了方便理解,相關的配置說明也寫在程式碼中了,這裡就不再過多贅述了。
程式碼如下:
DruidConfiguration.java
package com.equaker.druid;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
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;
/**
* druid資料來源監控web應用
*/
@Configuration
public class DruidConfiguration {
@Bean
public ServletRegistrationBean druidStatViewServle() {
//註冊服務
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(
new StatViewServlet(), "/druid/*");
// 白名單(為空表示,所有的都可以訪問,多個IP的時候用逗號隔開)
servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
// IP黑名單 (存在共同時,deny優先於allow)
servletRegistrationBean.addInitParameter("deny", "127.0.0.2");
// 設定登入的使用者名稱和密碼
servletRegistrationBean.addInitParameter("loginUsername", "equaker");
servletRegistrationBean.addInitParameter("loginPassword", "123456");
// 是否能夠重置資料.
servletRegistrationBean.addInitParameter("resetEnable", "false");
return servletRegistrationBean;
}
@Bean
public FilterRegistrationBean druidStatFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(
new WebStatFilter());
// 新增過濾規則
filterRegistrationBean.addUrlPatterns("/*");
// 新增不需要忽略的格式資訊
filterRegistrationBean.addInitParameter("exclusions",
"*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
System.out.println("druid初始化成功!");
return filterRegistrationBean;
}
}
編寫完之後,啟動程式,在瀏覽器輸入:http://127.0.0.1:8084/druid/index.html ,然後輸入設定的使用者名稱和密碼,便可以訪問Web介面了。
效果如圖:
DemoController.java:
package com.equaker.controller;
import com.equaker.mapper.cluster.UserMapper;
import com.equaker.mapper.master.EQMapper;
import com.equaker.model.EQ;
import com.equaker.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("demo")
public class DemoController {
@Autowired
private EQMapper eqMapper;
@Autowired
private UserMapper userMapper;
@RequestMapping("/save")
@ResponseBody
public EQ save(){
EQ eq = new EQ();
eq.setcAmount(new BigDecimal(10));
eq.setcCardno("1");
eq.setcDatetime(new Date());
eq.setcGcode("1");
eq.setcGname("sdq");
eq.setcId("1");
eq.setcQtty(new BigDecimal(10));
eq.setcSname("ss");
eq.setcStoreId("1");
eqMapper.insert(eq);
return eq;
}
@RequestMapping("/saveUser")
@ResponseBody
public User saveUser(){
User user = new User();
user.setName("sdq");
user.setAge(23);
user.setCreateTime(new Date());
user.setUpdateTime(new Date());
userMapper.save(user);
return user;
}
}
啟動類 :
因為使用了自定義的資料來源配置,所以在啟動類裡面一定要把springboot自己預設的配置去除
package com.equaker.fhcrm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@ComponentScan(basePackages = {"com.equaker"})
//@MapperScan(basePackages = {"com.equaker.mapper"})
public class FhCrmApplication {
public static void main(String[] args) {
SpringApplication.run(FhCrmApplication.class, args);
}
}
訪問:http://localhost:8080/equaker/demo/save 初始化並使用master資料來源 ok
訪問:http://localhost:8080/equaker/demo/saveUser 初始化並使用cluster資料來源 ok
注意!注意!注意!
由於在資料來源中已經配置了mapper介面和mapper對映檔案,所以再啟動類main方法中不需要指定mapperscan 了,,在application.yml裡面也不需要執行mapper對映檔案了。否則會提示waring:
2018-12-17 11:06:14.294 WARN 36624 --- [ main] o.m.s.mapper.ClassPathMapperScanner : Skipping MapperFactoryBean with name 'userMapper' and 'com.equaker.mapper.cluster.UserMapper' mapperInterface. Bean already defined with the same name!
2018-12-17 11:06:14.294 WARN 36624 --- [ main] o.m.s.mapper.ClassPathMapperScanner : Skipping MapperFactoryBean with name 'EQMapper' and 'com.equaker.mapper.master.EQMapper' mapperInterface. Bean already defined with the same name!
2018-12-17 11:06:14.294 WARN 36624 --- [ main] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[com.equaker.mapper]' package. Please check your configuration.