概述
配置多数据源有两种方案。一种是使用dynamic依赖的@DS注解的方法,这种是比较简单方便的方法。另一种是本文介绍的方式,配置不同数据源的SqlSessionFactory 。
第二种方法是我在开发一个老项目时,老项目配置的方法。
application.xml
xml
server:
port: 8002
spring:
datasource:
master:
jdbc-url: xxx
username: username
password: password
driver-class-name: driver-class-name
slave:
jdbc-url: xxx
username: username
password: password
driver-class-name: driver-class-name
初始化多数据源
java
package com.zou.myproject.config;
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 javax.sql.DataSource;
@Configuration
public class DatasourceConfig {
@Primary
@Bean("masterDatasource")
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSource masterDatasource(){
return DataSourceBuilder.create().build();
}
@Bean("slaveDatasource")
@ConfigurationProperties(prefix = "spring.datasource.slave")
public DataSource slaveDatasource(){
return DataSourceBuilder.create().build();
}
}
主数据源配置
java
package com.zou.myproject.config;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
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.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.zou.myproject.mapper",
sqlSessionFactoryRef = "sqlSessionFactoryMaster",
sqlSessionTemplateRef = "sqlSessionTemplateMaster"
)
public class MasterDatasourceConfig {
private DataSource masterDatasource;
public MasterDatasourceConfig(@Qualifier("masterDatasource") DataSource masterDatasource){
this.masterDatasource = masterDatasource;
}
@Bean("sqlSessionFactoryMaster")
@Primary
public SqlSessionFactory sqlSessionFactoryMaster() throws Exception{
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(masterDatasource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setMapperLocations(resolver.getResources("classpath*:/mapper/*.xml"));
return bean.getObject();
}
@Bean("sqlSessionTemplateMaster")
@Primary
public SqlSessionTemplate sqlSessionTemplateMaster() throws Exception{
return new SqlSessionTemplate(sqlSessionFactoryMaster());
}
}
次数据源
java
package com.zou.myproject.config;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
@MapperScan(
basePackages = "com.zou.myproject.mapper2",
sqlSessionFactoryRef = "sqlSessionFactorySlave",
sqlSessionTemplateRef = "sqlSessionTemplateSlave"
)
public class SlaveDatasourceConfig {
private DataSource slaveDatasource;
public SlaveDatasourceConfig(@Qualifier("slaveDatasource") DataSource slaveDatasource){
this.slaveDatasource = slaveDatasource;
}
@Bean("sqlSessionFactorySlave")
public SqlSessionFactory sqlSessionFactorySlave() throws Exception{
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(slaveDatasource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setMapperLocations(resolver.getResources("classpath*:/mapper2/*.xml"));
return bean.getObject();
}
@Bean("sqlSessionTemplateSlave")
public SqlSessionTemplate sqlSessionTemplateSlave() throws Exception{
return new SqlSessionTemplate(sqlSessionFactorySlave());
}
}
测试
我这里就不贴代码了。思路就是创建单元测试,新建两个mapper,不同mapper对应不同数据源。最后我自己的测试方法是可行的。
启动类取消mybatisplus的自动装配
如果上面配置完启动后,使用mybatisplus封装的方法报了Invalid bound statement的错误,就把MybatisAutoConfiguration.class从启动类中排除掉。
java
package com.zou.myproject;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(exclude = MybatisAutoConfiguration.class)
public class MyProjectApplication {
public static void main(String[] args) {
SpringApplication.run(MyProjectApplication.class, args);
}
}
总结
springboot mybatis mybatisplus+多数据源配置+事务,这块内容开发的时候都是一直查一直试错才找到解决方案的,有时间得看源码才能理清楚。