springboot 整合多数据源 (多数据库操作)
第一步 application.properties 配置文件
//默认数据源
#spring.datasource.url=jdbc:mysql:///test
#spring.datasource.username=root
#spring.datasource.password=root
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
//test01 数据源(数据库)
##spring.datasource.test01 discriminate
spring.datasource.test01.url=jdbc:mysql:///test01
spring.datasource.test01.username=root
spring.datasource.test01.password=root
spring.datasource.test01.driver-class-name=com.mysql.jdbc.Driver
//test02 数据源(数据库)
## spring.datasource.test02 discriminate
spring.datasource.test02.url=jdbc:mysql:///test02
spring.datasource.test02.username=root
spring.datasource.test02.password=root
spring.datasource.test02.driver-class-name=com.mysql.jdbc.Driver
第二步 普通的 bean
public class Emp {
private Integer eid;
private String ename;
private String sex;
get set 方法 ..
}
第三步 写 操作连个不同的数据源的 接口
操作 Test01 数据源的接口
package com.springboot_mybatis_ManyDatasource.Test01.dao;
public interface EmpmapperTest01 {
//添加
@Insert(" insert into emp values(null,#{ename},#{sex}) ")
void saveEmp(Emp emp);
//删除
@Delete(" delete from emp where eid = #{eid} ")
void deleteEmp(@Param("eid")Integer eid);
//修改
@Update("update emp set ename = #{ename},sex=#{sex} where eid = #{eid}")
void updateEmp(Emp emp);
//查询
@Select("select * from emp")
List<Emp> selectEmp();
//根据 id 查询 Emp 数据
@Select(" select * from emp where eid = #{eid} ")
Emp getbyid(@Param("eid")Integer eid);
}
操作 Test02 数据源的接口
package com.springboot_mybatis_ManyDatasource.Test02.dao;
public interface EmpmapperTest02 {
//添加
@Insert(" insert into emp values(null,#{ename},#{sex}) ")
void saveEmp(Emp emp);
//删除
@Delete(" delete from emp where eid = #{eid} ")
void deleteEmp(@Param("eid")Integer eid);
//修改
@Update("update emp set ename = #{ename},sex=#{sex} where eid = #{eid}")
void updateEmp(Emp emp);
//查询
@Select("select * from emp")
List<Emp> selectEmp();
//根据 id 查询 Emp 数据
@Select(" select * from emp where eid = #{eid} ")
Emp getbyid(@Param("eid")Integer eid);
}
第四步 自定义数据源
定义 Test01 数据源
@Configuration //注册到 spring 容器中
@MapperScan(basePackages="com.springboot_mybatis_ManyDatasource.Test01",sqlSessionFactoryRef="test1SqlSessionFactory")
public class DataSource1Config {
@Bean(name="test1DataSource") //将自定义的数据源注册到 spring 容器中
@Primary //默认数据源
@ConfigurationProperties(prefix="spring.datasource.test01")
public DataSource testDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name="test1SqlSessionFactory")
@Primary //默认数据源
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource")DataSource dataSource)
throws Exception{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name="test1TransactionManager")
@Primary //默认数据源
public DataSourceTransactionManager testTransaction(@Qualifier("test1DataSource")DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Bean(name="test1SqlSessionTemplate")
@Primary //默认数据源
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory")SqlSessionFactory sqlSessionFactory)throws Exception{
return new SqlSessionTemplate(sqlSessionFactory);
}
}
定义 Test02 数据源
@Configuration //注册到 spring 容器中
@MapperScan(basePackages="com.springboot_mybatis_ManyDatasource.Test02",sqlSessionFactoryRef="test2SqlSessionFactory")
public class DataSource2Config {
@Bean(name="test2DataSource") //将自定义的数据源注册到 spring 容器中
@ConfigurationProperties(prefix="spring.datasource.test02")
public DataSource testDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name="test2SqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource")DataSource dataSource)
throws Exception{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name="test2TransactionManager")
public DataSourceTransactionManager testTransaction(@Qualifier("test2DataSource")DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Bean(name="test2SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory")SqlSessionFactory sqlSessionFactory)throws Exception{
return new SqlSessionTemplate(sqlSessionFactory);
}
}
第一步 application.properties 配置文件
//默认数据源
#spring.datasource.url=jdbc:mysql:///test
#spring.datasource.username=root
#spring.datasource.password=root
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
//test01 数据源(数据库)
##spring.datasource.test01 discriminate
spring.datasource.test01.url=jdbc:mysql:///test01
spring.datasource.test01.username=root
spring.datasource.test01.password=root
spring.datasource.test01.driver-class-name=com.mysql.jdbc.Driver
//test02 数据源(数据库)
## spring.datasource.test02 discriminate
spring.datasource.test02.url=jdbc:mysql:///test02
spring.datasource.test02.username=root
spring.datasource.test02.password=root
spring.datasource.test02.driver-class-name=com.mysql.jdbc.Driver
第二步 普通的 bean
public class Emp {
private Integer eid;
private String ename;
private String sex;
get set 方法 ..
}
第三步 写 操作连个不同的数据源的 接口
操作 Test01 数据源的接口
package com.springboot_mybatis_ManyDatasource.Test01.dao;
public interface EmpmapperTest01 {
//添加
@Insert(" insert into emp values(null,#{ename},#{sex}) ")
void saveEmp(Emp emp);
//删除
@Delete(" delete from emp where eid = #{eid} ")
void deleteEmp(@Param("eid")Integer eid);
//修改
@Update("update emp set ename = #{ename},sex=#{sex} where eid = #{eid}")
void updateEmp(Emp emp);
//查询
@Select("select * from emp")
List<Emp> selectEmp();
//根据 id 查询 Emp 数据
@Select(" select * from emp where eid = #{eid} ")
Emp getbyid(@Param("eid")Integer eid);
}
操作 Test02 数据源的接口
package com.springboot_mybatis_ManyDatasource.Test02.dao;
public interface EmpmapperTest02 {
//添加
@Insert(" insert into emp values(null,#{ename},#{sex}) ")
void saveEmp(Emp emp);
//删除
@Delete(" delete from emp where eid = #{eid} ")
void deleteEmp(@Param("eid")Integer eid);
//修改
@Update("update emp set ename = #{ename},sex=#{sex} where eid = #{eid}")
void updateEmp(Emp emp);
//查询
@Select("select * from emp")
List<Emp> selectEmp();
//根据 id 查询 Emp 数据
@Select(" select * from emp where eid = #{eid} ")
Emp getbyid(@Param("eid")Integer eid);
}
第四步 自定义数据源
定义 Test01 数据源
@Configuration //注册到 spring 容器中
@MapperScan(basePackages="com.springboot_mybatis_ManyDatasource.Test01",sqlSessionFactoryRef="test1SqlSessionFactory")
public class DataSource1Config {
@Bean(name="test1DataSource") //将自定义的数据源注册到 spring 容器中
@Primary //默认数据源
@ConfigurationProperties(prefix="spring.datasource.test01")
public DataSource testDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name="test1SqlSessionFactory")
@Primary //默认数据源
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource")DataSource dataSource)
throws Exception{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name="test1TransactionManager")
@Primary //默认数据源
public DataSourceTransactionManager testTransaction(@Qualifier("test1DataSource")DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Bean(name="test1SqlSessionTemplate")
@Primary //默认数据源
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory")SqlSessionFactory sqlSessionFactory)throws Exception{
return new SqlSessionTemplate(sqlSessionFactory);
}
}
定义 Test02 数据源
@Configuration //注册到 spring 容器中
@MapperScan(basePackages="com.springboot_mybatis_ManyDatasource.Test02",sqlSessionFactoryRef="test2SqlSessionFactory")
public class DataSource2Config {
@Bean(name="test2DataSource") //将自定义的数据源注册到 spring 容器中
@ConfigurationProperties(prefix="spring.datasource.test02")
public DataSource testDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name="test2SqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource")DataSource dataSource)
throws Exception{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name="test2TransactionManager")
public DataSourceTransactionManager testTransaction(@Qualifier("test2DataSource")DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Bean(name="test2SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory")SqlSessionFactory sqlSessionFactory)throws Exception{
return new SqlSessionTemplate(sqlSessionFactory);
}
}