Spring Cloud + Mybatis multiple data source configuration

In case of defects in project development, the use of multiple data sources can avoid the steps of data synchronization between sub-databases, and the configuration steps are recorded here.

First, the automatic configuration of the data source is disabled

1. Disable the DataSourceAutoConfiguration that comes with spring boot to prevent the automatic configuration of the data source. Just add the exclude attribute to the @SpringBootApplication annotation.

2. Add the annotation @EnableTransactionManagement and create two transaction managers

@SpringBootApplication(exclude = {
		DataSourceAutoConfiguration.class
})
@EnableTransactionManagement
public class ManagerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ManagerApplication.class, args);
	}
	@Bean
	public PlatformTransactionManager testDBaTransactionManager(@Qualifier("testDBa") DataSource prodDataSource) {
		return new DataSourceTransactionManager(prodDataSource);
	}

	@Bean
	public PlatformTransactionManager testDBbTransactionManager(@Qualifier("testDBb") DataSource sitDataSource) {
		return new DataSourceTransactionManager(sitDataSource);
	}
}

Second, the @Transactional annotation configuration of the serviceImpl layer

Specify the transaction manager in the @Transactional annotation of the serviceImpl layer.

  a、若service用到了两个操作不同数据源中mapper中的方法,则不指定事务管理器

  b、若service只用到了一个数据源的mapper,则需要指定事务管理器
@Service
@Transactional(value = "testDBaTransactionManager")
public class testaServiceImpl implements testaService {
    @Autowired
    private TestaMapper  testaMapper ;
}
@Service
@Transactional(value = "testDBbTransactionManager")
public class testbServiceImpl implements testbService {
    @Autowired
    private TestbMapper  testbMapper ;
}
@Service
@Transactional
public class testServiceImpl implements testService {
    @Autowired
    private TestaMapper  testaMapper ;
    @Autowired
    private TestbMapper  testbMapper ;
}

Third, the modification of the yml configuration file

spring:
  datasource:
    testDBa:
      driver-class-name: com.mysql.jdbc.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      url: jdbc:mysql://xx.xx.xx.xx:xxxx/db?useUnicode=true&characterEncoding=utf8&autoReconnect=true
      username: user
      password: pwd
    testDBb:
      driver-class-name: com.mysql.jdbc.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      url: jdbc:mysql://**.**.**.**:****/db?useUnicode=true&characterEncoding=utf8&autoReconnect=true
      username: user
      password: pwd

Fourth, create a new mapper folder

Put the mapper file that needs to connect to the new DB in

1. Put the xml file that needs to connect to the new DataSource

2. The src layer also creates a new corresponding folder to store the mapper.java file

Five, the configuration of the data source

DataSourceConfig.java

@Configuration
public class DataSourceConfig {

    @Bean(name = "testDBa")
    // application.properteis中对应属性的前缀
    @ConfigurationProperties(prefix = "spring.datasource.testDBa")
    public DataSource dataSource1() {
        return DataSourceBuilder.create().build();
    }




    @Bean(name = "testDBb")
    // application.properteis中对应属性的前缀
    @ConfigurationProperties(prefix = "spring.datasource.testDBb")
    public DataSource dataSource2() {
        return DataSourceBuilder.create().build();
    }

}

Six, Mybatis configuration

Next, you need to configure two mybatis SqlSessionFactory to use different data sources:

MybatisDbAConfig.java

@Configuration
@MapperScan(basePackages = {"testa.mapper"}, sqlSessionFactoryRef = "sqlSessionFactory1")
public class MybatisDbAConfig {

    @Autowired
    @Qualifier("testDBa")
    private DataSource testDBa;


    @Bean
    public SqlSessionFactory sqlSessionFactory1() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        factoryBean.setConfiguration(configuration);
        // 使用testDBa数据源, 连接testDBa库
        factoryBean.setDataSource(testDBa);

        return factoryBean.getObject();

    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
        // 使用注解中配置的Factory
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory1());
        return template;
    }
}

MybatisDbBConfig.java

@Configuration
@MapperScan(basePackages = {"testb.mapper"}, sqlSessionFactoryRef = "sqlSessionFactory2")
public class MybatisDbBConfig {

    @Autowired
    @Qualifier("testDBb")
    private DataSource testDBb;


    @Bean
    public SqlSessionFactory sqlSessionFactory1() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        factoryBean.setConfiguration(configuration);
        // 使用testDBb数据源, 连接testDBb库
        factoryBean.setDataSource(testDBb);

        return factoryBean.getObject();

    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
        // 使用注解中配置的Factory
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory2());
        return template;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324520318&siteId=291194637