springboot-配置多个数据源
1、创建一个datasource包,新建DataSource1,DataSource2两个文件,通过注解来配置数据源
DataSource1:
package com.springboot.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.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; 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(){ return DataSourceBuilder.create().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); return bean.getObject(); } /** * 返回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); } }
DataSource2:
package com.springboot.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.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.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(){ return DataSourceBuilder.create().build(); } /** * 返回data2数据库的会话工厂 * @param ds * @return * @throws Exception */ @Bean(name = "data2SqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("data2Source") DataSource ds) throws Exception{ SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(ds); return bean.getObject(); } /** * 返回data2数据库的会话模板 * @param sessionFactory * @return * @throws Exception */ @Bean(name = "data2SqlSessionTemplate") public SqlSessionTemplate sqlSessionTemplate(@Qualifier("data2SqlSessionFactory") SqlSessionFactory 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); } }
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.driver-class-name=com.mysql.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.driver-class-name=com.mysql.jdbc.Driver
3、两个测试的Service&Mapper
4、Controller调用
@RequestMapping("getTopicList") public List<Map<String,Object>> getTopicList(){ List<Map<String,Object>> list1 = userServiceData1.getTopicListData1(); List<Map<String,Object>> list2 = userServiceData2.getTopicListData2(); return list1; }