springboot2.0单数据源转多数据源配置及问题解决

项目中遇到springboot2.0要配置多数据源的情况,在这里记录下,原理只有一个数据源,直接配置在application.yml下面了,由于要转多数据源,则需要配置多个数据源类。同时application.yml也要相应的添加上数据库配置。

application.yml:

mybatis:
  mapperLocations: classpath:mapper/*.xml
  configuration:
    callSettersOnNulls: true  # mybatis3 空字段null字段不返回  设置为true则返回null
    mapUnderscoreToCamelCase: true


spring:
  datasource:
    aa:
      driverClassName: com.mysql.jdbc.Driver
      #开发环境
      jdbc-url: jdbc:mysql://192.168.1.1:3306/aa?useUnicode=true&characterEncoding=utf-8&useOldAliasMetadataBehavior=true
      username: root
      password: root
      max-active: 10
      initial-size: 5
      max-idle: 5
      min-idle: 1
      test-while-idle: true
      test-on-borrow: true
      validation-query: SELECT 1
      time-between-eviction-runs-millis: 5000
      min-evictable-idle-time-millis: 60000
    bb:
      driver-class-name: com.mysql.jdbc.Driver
      #开发环境
      jdbc-url: jdbc:mysql://192.168.1.2:3306/bb?useUnicode=true&characterEncoding=utf-8&useOldAliasMetadataBehavior=true
      username: root
      password: root
      max-active: 10
      initial-size: 5
      max-idle: 5
      min-idle: 1
      test-while-idle: true
      test-on-borrow: true
      validation-query: SELECT 1
      time-between-eviction-runs-millis: 5000
      min-evictable-idle-time-millis: 60000

上面配置要注意到jdbc-url,这个在springboot1.x的时候配的是url,这里需要改成jdbc-url否则到时候启动时会报错:jdbcUrl is required with driverClassName.网上也有解决方案:点我查看。这是第一个问题吧。

好了,按上面的配置完,application.yml就配置完成了。接着编写配置类,这里只写两个,放在springboot能扫描到的地方就行:

package com.weimingfj.background.datasource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
//basePackages 配置的是mapper接口对应的包
@Configuration
@MapperScan(basePackages = "com.xxx.mapper.aaPackage", sqlSessionTemplateRef  = "aaSqlSessionTemplate")
public class DataSourceTmsConfig {

    @Bean
    @ConfigurationProperties(prefix = "mybatis.configuration")
    public org.apache.ibatis.session.Configuration globalConfiguration() {
        return new org.apache.ibatis.session.Configuration();
    }

    @Bean(name = "aaDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.aa")
    @Primary
    public DataSource tmsDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "aaSqlSessionFactory")
    @Primary
    public SqlSessionFactory aaSqlSessionFactory(@Qualifier("aaDataSource") DataSource dataSource, org.apache.ibatis.session.Configuration config) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setConfiguration(config);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "aaTransactionManager")
    @Primary
    public DataSourceTransactionManager aaTransactionManager(@Qualifier("aaDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "aaSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate aaSqlSessionTemplate(@Qualifier("aaSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

次:

package com.weimingfj.background.datasource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * Created by summer on 2016/11/25.
 */
@Configuration
@MapperScan(basePackages = "com.xxx.mapper.bb", sqlSessionTemplateRef  = "bbSqlSessionTemplate")
public class DataSourceWlgjConfig {

    @Bean(name = "bbDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.bb")
    public DataSource bbDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "bbSqlSessionFactory")
    public SqlSessionFactory bbSqlSessionFactory(@Qualifier("bbDataSource") DataSource dataSource, org.apache.ibatis.session.Configuration config) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
//        bean.setConfiguration(config);
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session
                .Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        bean.setConfiguration(configuration);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "bbTransactionManager")
    public DataSourceTransactionManager wlgjTransactionManager(@Qualifier("bbDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "bbSqlSessionTemplate")
    public SqlSessionTemplate wlgjSqlSessionTemplate(@Qualifier("bbSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

这里要注意的就是两点,对应的关键代码为:

org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session
                .Configuration();
configuration.setMapUnderscoreToCamelCase(true);
bean.setConfiguration(configuration);

bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));

由于我在代码中用到mybatis的下划线转驼峰配置:mapUnderscoreToCamelCase: true,当没有写上面的bean.setConfiguration(configuration);这段在配置类中将配置写入的话,只在application.yml中使用是不会生效的,原因是用了多数据源之后,springboot不在会替我们自动配置,而需要我们在这里手动配。原因可参看:点我查看或点我查看。

然后bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));这段是是同样的道理,如果不在这里手动配置的话,只在application.yml中配置同样不会生效。

还有一个重要的点就是,如果你的配置类的basePackages中进行了配置,主配置的basePackages不能够覆盖到次的basePackages,否则全部都会走主的数据源进行数据操作,次的就失效了。

最后附上springboot多个example,里面就有多数据源配置的源码例子哦:点我查看

 

你可能感兴趣的:(java,springboot,多数据源)