简介
开发当中经常会遇到需要进行多库多表数据整合的需求,在无法拆分项目的情况下,就需要在一个项目中配置多数据源,实现多库数据的整合。本文是在springboot框架的基础上进行的多数据源配置,可参考,也欢迎指正
1、第一步:application配置
application.yml配置如下:
- 在单数据源配置的基础上新增db名称的属性,这里起名为“db1”、“db2”,方便区分,如有需要也可以命名为自己需要的名字;
- 在对应的数据库配置下填入数据库的地址、端口号、账号、密码等信息;
- 切记数据库信息要匹配哦,否则会连接失败。
spring:datasource:db1:jdbc-url: jdbc:mysql://127.0.0.1:3306/db1?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=UTC&allowPublicKeyRetrieval=trueusername: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driverdb2:jdbc-url: jdbc:mysql://127.0.0.1:3306/db2?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=UTC&allowPublicKeyRetrieval=trueusername: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driver
2、第二步:DataSourceConfig配置
每个数据库配置一个对应的DataSourceConfig
- class类名我使用的是DB1DataSourceConfig、DB2DataSourceConfig,更直观的对应application中配置的名称;
- @MapperScan:mapper层又分了“db1”,“db2”两个文件夹,分别实现相应数据库的持久化。所以在属性“basePackages”中要填入对应关系的包地址,确保连接正确;
DB1DataSourceConfig.java
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.context.properties.ConfigurationProperties;import org.springframework.boot.jdbc.DataSourceBuilder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import javax.sql.DataSource;@Configuration@MapperScan(basePackages = "com.test.myproject.mapper.db1",sqlSessionFactoryRef = "db1SqlSessionFactory")public class DB1DataSourceConfig {@Primary // 表示这个数据源是默认数据源, 这个注解必须要加,因为不加的话spring将分不清楚那个为主数据源(默认数据源)@Bean("db1DataSource")@ConfigurationProperties(prefix = "spring.datasource.db1") //读取application.yml中的配置参数映射成为一个对象public DataSource getDb1DataSource() {return DataSourceBuilder.create().build();}@Primary@Bean("db1SqlSessionFactory")public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);// mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml")); // 持久化.xml文件的地址return bean.getObject();}@Primary@Bean("db1SqlSessionTemplate")public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}}
DB2DataSourceConfig.java
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.context.properties.ConfigurationProperties;import org.springframework.boot.jdbc.DataSourceBuilder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import javax.sql.DataSource;@Configuration@MapperScan(basePackages ="com.test.myproject.mapper.db2",sqlSessionFactoryRef = "db2SqlSessionFactory")public class DB2DataSourceConfig {@Bean("db2DataSource")@ConfigurationProperties(prefix = "spring.datasource.db2")public DataSource getDb1DataSource(){return DataSourceBuilder.create().build();}@Bean("db2SqlSessionFactory")public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));return bean.getObject();}@Bean("db2SqlSessionTemplate")public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){return new SqlSessionTemplate(sqlSessionFactory);}}