How to configure multiple data sources using MybatisPlus

1. Demand

Assuming that we originally only queried the mysql database data in a single environment, how can we support querying the mysql database data in multiple environments if we want to transform it now?

Second, the environment that needs to be prepared

Two mysql database servers:
Host1: 192.168.25.134
Host2: 192.168.25.166

3. Configuration file modification

Modify the content of application.properties, the configuration names of multiple databases need to be unique

#三套环境
spring.datasource.api3.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.api3.sql-script-encoding=utf-8
spring.datasource.api3.url=jdbc:mysql://192.168.25.134:3306/api?allowMultiQueries=true&useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.api3.username=api
spring.datasource.api3.password=apiapi
#四套环境
spring.datasource.api4.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.api4.sql-script-encoding=utf-8
spring.datasource.api4.url=jdbc:mysql://192.168.25.166:3306/api?allowMultiQueries=true&useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.api4.username=api
spring.datasource.api4.password=apiapi

4. Configuration code modification

Configure multiple data sources in code

@Configuration
public class DataSourceConfig {
    
    

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.api3")
    public DataSource dataSource2() {
    
    
        return DataSourceBuilder.create().build();
    }
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.api4")
    public DataSource dataSource3() {
    
    
        return DataSourceBuilder.create().build();
    }
}

Create bean objects corresponding to multiple data sources

@Configuration
@MapperScan(basePackages = "com.icbc.ndf.jft.manager.mapper3", sqlSessionFactoryRef = "sqlSessionFactory2")
public class MyBatisConfig2 {
    
    
    @Autowired
    @Qualifier("dataSource2")
    private DataSource dataSource2;
    @Bean
    public SqlSessionFactory sqlSessionFactory2() throws Exception {
    
    
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource2);
        return factoryBean.getObject();
    }
    @Bean
    public SqlSessionTemplate sqlSessionTemplate2() throws Exception {
    
    
        return new SqlSessionTemplate(sqlSessionFactory2());
    }
}
@Configuration
@MapperScan(basePackages = "com.icbc.ndf.jft.manager.mapper4", sqlSessionFactoryRef = "sqlSessionFactory3")
public class MyBatisConfig3 {
    
    
    @Autowired
    @Qualifier("dataSource3")
    private DataSource dataSource3;
    @Bean
    public SqlSessionFactory sqlSessionFactory3() throws Exception {
    
    
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource3);
        return factoryBean.getObject();
    }
    @Bean
    public SqlSessionTemplate sqlSessionTemplate3() throws Exception {
    
    
        return new SqlSessionTemplate(sqlSessionFactory3());
    }
}

5. Create multiple mapper directories

Each mapper directory corresponds to mapper and mapper.xml files of different databases. Take mapper as an example here. Create ApiResExtendMapper3 and ApiResExtendMapper4 under mapper3 and mapper4 respectively, and the corresponding xml files

@Mapper
public interface ApiResExtendMapper3 {
    
    



    /**
     * 通过实体作为筛选条件查询
     *
     * @param apiResExtend 实例对象
     * @return 对象列表
     */
    List<ApiResExtend> queryByUrl(ApiResExtend apiResExtend);
}
@Mapper
public interface ApiResExtendMapper4 {
    
    



    /**
     * 通过实体作为筛选条件查询
     *
     * @param apiResExtend 实例对象
     * @return 对象列表
     */
    List<ApiResExtend> queryByUrl(ApiResExtend apiResExtend);

}

In this way, we have completed the configuration of multiple data sources, and we can start the project test~~

Guess you like

Origin blog.csdn.net/stalin_/article/details/132361501