Springboot配置双数据源

1. 多数据源的配置文件,采用application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    jdbc-url: jdbc:mysql://xx.xx.xx.xx:32306/xx-db?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: p@ssw0rd
    hikari:
      minimum-idle: 10
      maximum-pool-size: 40
  datasource-access:
    driver-class-name: com.mysql.cj.jdbc.Driver
    jdbc-url: jdbc:mysql://xx.xx.xx.xx:32306/xx-db2?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: p@ssw0rd
    hikari:
      minimum-idle: 10
      maximum-pool-size: 40

2. 多数据源的配置JAVA文件

@Configuration
#xx.xx.mapper.access 包下的Mapper接口
@MapperScan(basePackages = {"xx.xx.mapper.access"}, sqlSessionFactoryRef = "accessSqlSessionFactory")
public class SqlSessionFactoryAccessConfig {
    static final String MAPPER_LOCATION = "classpath:mapper/access/*.xml";

    /**
     * 配置数据源
     */
    @Bean(name = "dataSourceAccess")
    @ConfigurationProperties(prefix = "spring.datasource-access")
    public DataSource dataSourceAccess() {
        return DataSourceBuilder.create().build();
    }


    /**
     * 配置SqlSessionFactory
     */
    @Bean(name = "accessSqlSessionFactory")
    public SqlSessionFactory accessSqlSessionFactory(@Qualifier("dataSourceAccess") DataSource dataSource) throws Exception {
//        final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        //用这个MybatisSqlSessionFactoryBean是因为我用的是mybatis普拉斯 用上面的会报错
        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
        mybatisConfiguration.setDefaultEnumTypeHandler(CoreEnumTypeHandler.class);
        mybatisConfiguration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
        mybatisConfiguration.setMapUnderscoreToCamelCase(true);
        mybatisConfiguration.setCacheEnabled(false);
        final MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setConfiguration(mybatisConfiguration);
        //这里注意getResources()方法,一定要加s因为有一个方法是getResource()
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        sqlSessionFactoryBean.setPlugins(mybatisPlusInterceptor);
        return sqlSessionFactoryBean.getObject();
    }
}

@Configuration
@MapperScan(basePackages = {"xx.xx.mapper.video"}, sqlSessionFactoryRef = "videoSqlSessionFactory")
public class SqlSessionFactoryVideoConfig {
    static final String MAPPER_LOCATION = "classpath:mapper/*.xml";

    /**
     * 配置数据源
     */
    @Bean(name = "dataSourceVideo")
    @ConfigurationProperties(prefix = "spring.datasource")
    @Primary
    public DataSource dataSourceVideo() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 配置SqlSessionFactory
     */
    @Bean(name = "videoSqlSessionFactory")
    @Primary
    public SqlSessionFactory videoSqlSessionFactory(@Qualifier("dataSourceVideo") DataSource dataSource) throws Exception {
//        final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        //用这个MybatisSqlSessionFactoryBean是因为我用的是mybatis普拉斯 用上面的会报错
        final MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
        mybatisConfiguration.setDefaultEnumTypeHandler(CoreEnumTypeHandler.class);
        mybatisConfiguration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
        mybatisConfiguration.setMapUnderscoreToCamelCase(true);
        mybatisConfiguration.setCacheEnabled(false);
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setConfiguration(mybatisConfiguration);
        //这里注意getResources()方法,一定要加s因为有一个方法是getResource()
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        sqlSessionFactoryBean.setPlugins(mybatisPlusInterceptor);
        return sqlSessionFactoryBean.getObject();
    }
}

注意:jdbc-url 和 url的区别。
当我们通过DataSourceProperties方式注入数据源配置,获取DataSource时,这个时候需要使用url
如果是直接通过配置获取DataSource,那必须是jdbc-url。
如果是单数据源,非手动获取配置注入数据源的不考虑该问题。

你可能感兴趣的:(spring,boot,后端,java)