多資料來源結合mybatis-plus的使用

腳一晃發表於2020-11-24

多資料來源

資料來源mysql配置
@Configuration
@MapperScan(basePackages="com.database.multidatademo.mapper.first",sqlSessionFactoryRef = "firstSqlSessionFactory")
public class DataSource1Config {

 @Autowired
 Datasource1Properties datasource1Properties;



 @Bean(name = "firstDataSource")
 @Primary
 public DataSource getDataSource1(){
     HikariConfig config = new HikariConfig();
     config.setUsername(datasource1Properties.getUsername());
     config.setPassword(datasource1Properties.getPassword());
     config.setJdbcUrl(datasource1Properties.getUrl());
     config.setDriverClassName(datasource1Properties.getDriverClassName());
     HikariDataSource ds = new HikariDataSource(config);
     return ds;

 }

 @Bean(name = "firstSqlSessionFactory")
 public SqlSessionFactory firstSqlSessionFactory(@Qualifier("firstDataSource") DataSource dataSource1) throws Exception {
     //使用BaseMapper需要使用MybatisSqlSessionFactoryBean而不是SqlSessionFactoryBean,否則Mapper中繼承的BaseMapper無效
     MybatisSqlSessionFactoryBean mssfb = new MybatisSqlSessionFactoryBean();
     mssfb.setDataSource(dataSource1);
     //xml位置
     mssfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapping/*.xml"));
     //別名
     mssfb.setTypeAliasesPackage("com.database.multidatademo.domain");
     //分頁外掛,需要放在駝峰配置前,否則分頁不生效,total=0
     mssfb.setPlugins(new Interceptor[] {new PaginationInterceptor()});
     //駝峰
     mssfb.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
     mssfb.getObject().getConfiguration().setCallSettersOnNulls(true);
     return mssfb.getObject();

 }

 @Bean(name = "firstSqlSessionTemplate")
 public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("firstSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
     return new SqlSessionTemplate(sqlSessionFactory);
 }

 @Bean(name = "firstDataSourceTransactionManager")
 public DataSourceTransactionManager firstDataSourceTransactionManager(@Qualifier("firstDataSource") DataSource dataSource){
     return new DataSourceTransactionManager(dataSource);
 }
}

資料來源oracle配置

@Configuration
@MapperScan(basePackages="com.database.multidatademo.mapper.sec",sqlSessionFactoryRef = "secSqlSessionFactory")
public class DataSource2Config {

    @Autowired
    Datasource2Properties datasource2Properties;



    @Bean(name = "secDataSource")
    public DataSource getDataSource2(){
        HikariConfig config = new HikariConfig();
        config.setUsername(datasource2Properties.getUsername());
        config.setPassword(datasource2Properties.getPassword());
        config.setJdbcUrl(datasource2Properties.getUrl());
        config.setDriverClassName(datasource2Properties.getDriverClassName());
        HikariDataSource ds = new HikariDataSource(config);
        return ds;

    }

    @Bean(name = "secSqlSessionFactory")
    public SqlSessionFactory secSqlSessionFactory(@Qualifier("secDataSource") DataSource dataSource2) throws Exception {
        //使用BaseMapper需要使用MybatisSqlSessionFactoryBean而不是SqlSessionFactoryBean,否則Mapper中繼承的BaseMapper無效
        MybatisSqlSessionFactoryBean mssfb = new MybatisSqlSessionFactoryBean();
        mssfb.setDataSource(dataSource2);
        //xml位置
        mssfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapping/*.xml"));
        //別名
        mssfb.setTypeAliasesPackage("com.database.multidatademo.domain");
        //分頁外掛,需要放在駝峰配置前,否則分頁不生效,total=0
        mssfb.setPlugins(new Interceptor[] {new PaginationInterceptor()});
        //駝峰
        mssfb.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
        mssfb.getObject().getConfiguration().setCallSettersOnNulls(true);
        return mssfb.getObject();

    }

    @Bean(name = "secSqlSessionTemplate")
    public SqlSessionTemplate secSqlSessionTemplate(@Qualifier("secSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean(name = "secDataSourceTransactionManager")
    public DataSourceTransactionManager secDataSourceTransactionManager(@Qualifier("secDataSource") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }
}
demo已經上傳,使用swagger3可以除錯,3和2的請求地址也有所不同,http://localhost:8888/swagger-ui/index.html,表中的資料自己可以新增幾條測試下https://gitee.com/cherrychao/case.git

相關文章