只是个配置,跟着走就行了
先看application.properties的配置内容
# 数据源A
spring.datasource.sysA.driverClassName = com.mysql.jdbc.Driver
spring.datasource.sysA.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.sysA.username = root
spring.datasource.sysA.password = 123
# 数据源B
spring.datasource.sysB.driverClassName = com.mysql.jdbc.Driver
spring.datasource.sysB.url = jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8
spring.datasource.sysB.username = root
spring.datasource.sysB.password = 123
#这下面才是durid的配置。。。。。
#duird连接池 参考 https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_DruidDataSource%E5%8F%82%E8%80%83%E9%85%8D%E7%BD%AE
#基本属性 url、user、password
spring.datasource.druidA.driverClassName = com.mysql.jdbc.Driver
spring.datasource.druidA.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.druidA.username = root
spring.datasource.druidA.password = 123
# 配置初始化大小、最小、最大
spring.datasource.druidA.initialSize=3
spring.datasource.druidA.minIdle=3
spring.datasource.druidA.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.druidA.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.druidA.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.druidA.minEvictableIdleTimeMillis=300000
spring.datasource.druidA.validationQuery=SELECT 1 FROM DUAL
spring.datasource.druidA.testWhileIdle=true
spring.datasource.druidA.testOnBorrow=false
spring.datasource.druidA.testOnReturn=false
DSSysA.java
package cn.ywj.app.datasource;
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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
// 扫描cn.ywj.app.mapper.a的mapper
@MapperScan(basePackages = "cn.ywj.app.mapper.a", sqlSessionTemplateRef = "sqlSessionTemplateA")
public class DSSysA {
@Bean(name = "dataSourceA")
@ConfigurationProperties(prefix = "spring.datasource.sysA")// 配置按application.properties中spring.datasource.sysA开关的为准
@Primary // 配置一个默认的,不然启动项目报出出现多个DataSource
public DataSource dataSourceA() {
return DataSourceBuilder.create().build();
}
@Bean(name = "sqlSessionFactoryA")
@Primary
public SqlSessionFactory sqlSessionFactoryA(@Qualifier("dataSourceA") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "transactionManagerA")
@Primary
public DataSourceTransactionManager transactionManagerA(@Qualifier("dataSourceA") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sqlSessionTemplateA")
@Primary
public SqlSessionTemplate sqlSessionTemplateA(@Qualifier("sqlSessionFactoryA") SqlSessionFactory sqlSessionFactory)
throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
现在把红色那里的数据源换成durid的就行
package cn.ywj.app.datasource;
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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
// 扫描cn.ywj.app.mapper.a的mapper
@MapperScan(basePackages = "cn.ywj.app.mapper.a", sqlSessionTemplateRef = "sqlSessionTemplateA")
public class DSSysA {
@Bean(name = "dataSourceA")
@ConfigurationProperties(prefix = "spring.datasource.druidAA")// 配置按application.properties中spring.datasource.sysA开关的为准
@Primary // 配置一个默认的,不然启动项目报出出现多个DataSource
public DruidDataSource dataSourceA() {
return new DruidDataSource();
}
@Bean(name = "sqlSessionFactoryA")
@Primary
public SqlSessionFactory sqlSessionFactoryA(@Qualifier("dataSourceA") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "transactionManagerA")
@Primary
public DataSourceTransactionManager transactionManagerA(@Qualifier("dataSourceA") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sqlSessionTemplateA")
@Primary
public SqlSessionTemplate sqlSessionTemplateA(@Qualifier("sqlSessionFactoryA") SqlSessionFactory sqlSessionFactory)
throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
ok