1.在springboot配置文件中配置多个(2个为例)数据源

# 案件分析业务database   ##注意springboot2.0以后要使用jdbc-url,使用url会报错
spring.datasource.primary.jdbc-url=jdbc:postgresql://**.**.***.36:5432/inarc-case-analysis
spring.datasource.primary.username=postgres
spring.datasource.primary.password=12345+
spring.datasource.primary.driver-class-name=org.postgresql.Driver
# 上传数据所在database
spring.datasource.secondary.jdbc-url=jdbc:postgresql://**.**.***.50:5432/inarc-pocket-data
spring.datasource.secondary.username=postgres
spring.datasource.secondary.password=987654
spring.datasource.secondary.driver-class-name=org.postgresql.Driver

2.数据源配置类

@Configuration
//此数据源指定的mybatis接口所在包
@MapperScan(basePackages = {"com.hikvision.idatafusion.**.dao.**"}, sqlSessionFactoryRef = "primarySqlSessionFactory")
public class PrimaryDatasourceConfig {

    @Primary  //标识其为默认数据源
    @Bean(name = "primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSource getDataSource() {
        return DataSourceBuilder.create().build();
    }
    @Primary
    @Bean(name = "primarySqlSessionFactory")
    public SqlSessionFactory getSqlSessionFactory(@Qualifier("primaryDataSource") DataSource datasource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(datasource);
        sqlSessionFactoryBean.setMapperLocations(
                //设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:com.hikvision.idatafusion.jd.mapper/*.xml")
        ); 
    //开启驼峰 注意:配置多数据源情况 配置文件中的mybatis相关配置都失效,因此需要在配置类中配置, 如下为驼峰配置
    sqlSessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
    return sqlSessionFactoryBean.getObject(); 
  }
  @Primary 
  @Bean(name
= "primarySqlSessionTemplate")
  public SqlSessionTemplate getSqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
    return new SqlSessionTemplate(sqlSessionFactory);
  }

}
 1 @Configuration
 2 //此数据源指定的mybatis接口所在包
 3 @MapperScan(basePackages = {"com.hikvision.idatafusion.**.dao2.**"}, sqlSessionFactoryRef = "secondarySqlSessionFactory")
 4 public class SecondaryDatasourceConfig {
 5 
 6     @Bean(name = "secondaryDataSource")
 7     @ConfigurationProperties(prefix = "spring.datasource.secondary")
 8     public DataSource getDataSource() {
 9         return DataSourceBuilder.create().build();
10     }
11 
12     @Bean(name = "secondarySqlSessionFactory")
13     public SqlSessionFactory getSqlSessionFactory(@Qualifier("secondaryDataSource") DataSource datasource) throws Exception {
14         SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
15         sqlSessionFactoryBean.setDataSource(datasource);
16         sqlSessionFactoryBean.setMapperLocations(
17                 //设置mybatis的xml所在位置
18                 new PathMatchingResourcePatternResolver().getResources("classpath*:com.hikvision.idatafusion.jd.mapper2/*.xml")
19         );
20         return sqlSessionFactoryBean.getObject();
21     }
22 
23     @Bean(name = "secondarySqlSessionTemplate")
24     public SqlSessionTemplate getSqlSessionTemplate(@Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
25         return new SqlSessionTemplate(sqlSessionFactory);
26     }
27 }
第二个数据源配置类

3.注意:

1.每个数据源配置类都指定了扫描mapper接口所在的包,所以启动类的@MapperScan需要注释。

2.每个数据源配置类都指定了mapperLocations,所以需要在启动类中注释掉mybatis.mapper-locations的配置。

3.按照上述配置类分别建立对应的mapper接口包  和  mapperLocations包放置xml文件。即可操作对应的数据库。

posted on 2020-11-10 17:38  足下之路  阅读(1263)  评论(1编辑  收藏  举报