简述
Druid是阿里巴巴开源平台上的一个数据库连接池实现,它结合了C3P0、DPCP、RPOXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB连接池,据说是目前最好的连接池。
集成
1、添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
2、配置文件application.yml
spring:
datasource:
druid:
max-active: 100
time-between-eviction-runs-millis: 60000
remove-abandoned-timeout: 180 #超时时间(以秒数为单位)
min-evictable-idle-time-millis: 1800000
initial-size: 10 #初始化连接
max-wait: 100000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
primary: #主数据库
driverClassName: oracle.jdbc.driver.OracleDriver
url: jdbc:mysql://localhost:3306/test1 #地址
username: root #账号
password: root #密码
min-idle: 2 #最小空闲连接
remove-abandoned: true #是否自动回收超时连接
validation-query: 'SELECT 1 FROM DUAL' #心跳验证
validation-query-timeout: 100000
keep-alive: true
test-while-idle: true
test-on-borrow: true
test-on-return: false
secondary: 从数据库
driverClassName: oracle.jdbc.driver.OracleDriver
url: jdbc:mysql://localhost:3306/test2 #地址
username: root
password: root
min-idle: 2 #最小空闲连接
remove-abandoned: true #是否自动回收超时连接
validation-query: 'SELECT 1 FROM DUAL' #心跳验证
validation-query-timeout: 100000
keep-alive: true
test-while-idle: true
test-on-borrow: true
test-on-return: false
3、多数据源配置类DataSourceConfig.class
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
/**
* 多数据源配置中心
* @author shi
*
*/
@Configuration
public class DataSourceConfig {
/**
* 基础数据库
* @return
*/
@Bean(name = "primaryDataSource")
@Qualifier("primaryDataSource")
@Primary
@ConfigurationProperties(prefix="spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
/**
* 第二个数据库
* @return
*/
@Bean(name = "secondaryDataSource")
@Qualifier("secondaryDataSource")
@ConfigurationProperties(prefix="spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
4、主数据库配置SqlSessionPrimary.class
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import com.github.pagehelper.PageHelper;
/**
* spring 整合 oracle配置sqlSession
* @author shi
*
*/
@Configuration
//basePackage是对应项目中dao的包名
@MapperScan(basePackages = "com.shinho.dp.cust.dao", sqlSessionTemplateRef = "primarySqlSessionTemplate")
public class SqlSessionPrimary {
/**
* sqlSession创建工厂配置类
* @param dataSource
* @return
* @throws Exception
*/
@Bean(name = "primarySqlSessionFactory")
public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);//注入数据库配置
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/mapper/*.xml"));//mapper文件扫描文件位置
//将分页注入到sqlSessionFactory中
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
//是否将参数offset作为PageNum使用
p.setProperty("offsetAsPageNum", "true");
//是否进行count查询
p.setProperty("rowBoundsWithCount", "true");
//是否分页合理化
p.setProperty("reasonable", "true");
pageHelper.setProperties(p);
bean.setPlugins(new PageHelper[] {pageHelper});
return bean.getObject();
}
/**
* 将sqlSession传递给spring boot
* @param sqlSessionFactory
* @return
* @throws Exception
*/
@Bean(name = "primarySqlSessionTemplate")
public SqlSessionTemplate primarySqlSessionTemplate(
@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
5、辅数据库配置SqlSessionSecondary.class
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import com.github.pagehelper.PageHelper;
/**
* spring 整合 oracle配置sqlSession
* @author 18030196
*
*/
@Configuration
//basePackage是对应项目中dao的包名
@MapperScan(basePackages = "com.shinho.dp.cust.secondaryDao", sqlSessionTemplateRef = "secondarySqlSessionTemplate")//跟dao层接口对应
public class SqlSessionSecondary {
/**
* sqlSession创建工厂配置类
* @param dataSource
* @return
* @throws Exception
*/
@Bean(name = "secondarySqlSessionFactory")
public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);//注入数据库配置
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/secondaryMapper/*.xml"));//mappper文件扫描文件位置
//将分页注入到sqlSessionFactory中
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
//是否将参数offset作为PageNum使用
p.setProperty("offsetAsPageNum", "true");
//是否进行count查询
p.setProperty("rowBoundsWithCount", "true");
//是否分页合理化
p.setProperty("reasonable", "true");
pageHelper.setProperties(p);
bean.setPlugins(new PageHelper[] {pageHelper});
return bean.getObject();
}
/**
* 将sqlSession传递给spring boot
* @param sqlSessionFactory
* @return
* @throws Exception
*/
@Bean(name = "secondarySqlSessionTemplate")
public SqlSessionTemplate secondarySqlSessionTemplate(
@Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
6、项目目录结构
总结
以上就是阿里Druid多数据源的配置方式,本文采用的是配置文件加配置类的方式实现的,当然还有纯配置文件的版本;上面的示例使用的是Oracle数据库的两个数据源,有多个数据源的情况下,可以以此类推添加多个。
这种物理分离mapper和dao的方式可以实现动态访问不同的数据源,在service层做数据组装或者处理。