springboot-配置多个数据源
1、创建⼀个datasource包,新建DataSource1,DataSource2两个⽂件,通过注解来配置数据源
DataSource1:
package com.springboot.datasource;
import org.apache.ibatis.session.SqlSessionFactory;
batis.spring.SqlSessionFactoryBean;
batis.spring.SqlSessionTemplate;
batis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.t.properties.ConfigurationProperties;
import t.annotation.Bean;
import t.annotation.Configuration;
import t.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration//注解到spring容器中
@MapperScan(basePackages = "com.springboot.data1.mapper",sqlSessionFactoryRef = "data1SqlSessionFactory")
public class DataSource1 {
/**
* 返回data1数据库的数据源
* @return
*/
@Bean(name="data1Source")
@Primary//主数据源
@ConfigurationProperties(prefix = "spring.datasource.data1")
public DataSource dataSource(){
ate().build();
}
/**
* 返回data1数据库的会话⼯⼚
* @param ds
* @return
* @throws Exception
*/
@Bean(name = "data1SqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("data1Source") DataSource ds) throws Exception{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(ds);
Object();
}
/**
* 返回data1数据库的会话模板
* @param sessionFactory
* @return
* @throws Exception
*/
@Bean(name = "data1SqlSessionTemplate")
@Primary
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("data1SqlSessionFactory") SqlSessionFactory sessionFactory) throws  Exception{ return new SqlSessionTemplate(sessionFactory);
}
/**
* 返回data1数据库的事务
* @param ds
* @return
*/
@Bean(name = "data1TransactionManager")
@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("data1Source") DataSource ds){
return new DataSourceTransactionManager(ds);
}
}
View Code
DataSource2:
package com.springboot.datasource;
import org.apache.ibatis.session.SqlSessionFactory;
batis.spring.SqlSessionFactoryBean;
batis.spring.SqlSessionTemplate;
batis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.t.properties.ConfigurationProperties;
import t.annotation.Bean;
import t.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration//注解到spring容器中
@MapperScan(basePackages = "com.springboot.data2.mapper",sqlSessionFactoryRef = "data2SqlSessionFactory")
public class DataSource2 {
/**
* 返回data2数据库的数据源
* @return
*/
@Bean(name="data2Source")
@ConfigurationProperties(prefix = "spring.datasource.data2")
public DataSource dataSource(){
ate().build();
}
/**
* 返回data2数据库的会话⼯⼚
* @param ds
* @return
* @throws Exception
springframework事务
*/
@Bean(name = "data2SqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("data2Source") DataSource ds) throws Exception{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(ds);
Object();
}
/**
* 返回data2数据库的会话模板
* @param sessionFactory
* @return
* @throws Exception
*/
@Bean(name = "data2SqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("data2SqlSessionFactory") SqlSessionFac
tory sessionFactory) throws  Exception{ return new SqlSessionTemplate(sessionFactory);
}
/**
* 返回data2数据库的事务
* @param ds
* @return
*/
@Bean(name = "data2TransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("data2Source") DataSource ds){
return new DataSourceTransactionManager(ds);
}
}
View Code
2、在application.properties⽂件配置两个数据源
spring.datasource.data1.url=jdbc:mysql://127.0.0.1:3306/mysql-boot
spring.datasource.data1.username=root
spring.datasource.data1.password=123123
spring.datasource.data1.sql.jdbc.Driver
spring.datasource.data2.url=jdbc:mysql://127.0.0.1:3306/mysql-confswh
spring.datasource.data2.username=root
spring.datasource.data2.password=123123
spring.datasource.data2.sql.jdbc.Driver
3、两个测试的Service&Mapper
4、Controller调⽤
@RequestMapping("getTopicList")
public List<Map<String,Object>> getTopicList(){
List<Map<String,Object>> list1 = TopicListData1();        List<Map<String,Object>> list2 = TopicListData2();
return list1;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。