spingboot+mybatis 多数据源配置,有坑请绕行

spingboot+mybatis 三个数据源配置,有坑请绕行

首先我们需要注意项目的搭建

  • jdk1.8
  • springboot项目架构
  • mysql 和mybatis的依赖

按照网上的一些配置我和同事进行多方优化

下面是项目的主要架构

spingboot+mybatis 多数据源配置,有坑请绕行_第1张图片

主要代码

  • 首先要将spring boot自带的DataSourceAutoConfiguration禁掉
package com.webi.speakhimidplan;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
//禁用单数据源
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }
public class SpeakhiMidPlanApplication {

  public static void main(String[] args) {
    SpringApplication.run(SpeakhiMidPlanApplication.class, args);
  }
}
  • 配置application.properties
    因为是三数据源
    配置如下:
#这里只声明所有环境中全部相同的变量值
spring.profiles.active=dev //这个是多环境配置
mybatis.configuration.mapUnderscoreToCamelCase=true
  • 注意这个 spring.datasource.speakhi.jdbc-url
  • 注意这里有个坑:该参数[spring.datasource.speakhi.jdbc-url]和单数据源的配置有区别,不然会报:【Error querying database. Cause: java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.】
env=dev
server.port=8997
mybatis.configuration.mapUnderscoreToCamelCase=true
#mybatis.mapper-locations=classpath:titan/mapper/*.xml
#mybatis.config-locations=classpath:mybatis-config.xml
# 成人数据库
spring.datasource.speakhi.jdbc-url=jdbc:mysql://10.1.139.167:3306/hwj
spring.datasource.speakhi.username=hwj_test
spring.datasource.speakhi.password=hwj_test
spring.datasource.speakhi.driver-class-name=com.mysql.jdbc.Driver
# 青少数据库
spring.datasource.teens.jdbc-url=jdbc:mysql://10.1.139.167:3306/speakhi_teenager
spring.datasource.teens.username=hwj_test
spring.datasource.teens.password=hwj_test
#spring.datasource.teens.driver-class-name=com.mysql.jdbc.Driver
# speakhi金蝶中间库
spring.datasource.kingdee.jdbc-url=jdbc:mysql://rdsojvzv2v2725l4rc91.mysql.rds.aliyuncs.com:3306/speakhi_outward_test
spring.datasource.kingdee.username=speakhi_test
spring.datasource.kingdee.password=yangMh653533
  • 如果不需要多环境 可直接如下配置application,properties
server.port=8997
mybatis.configuration.mapUnderscoreToCamelCase=true
#mybatis.mapper-locations=classpath:titan/mapper/*.xml
#mybatis.config-locations=classpath:mybatis-config.xml
# 成人数据库
spring.datasource.speakhi.jdbc-url=jdbc:mysql://10.1.139.167:3306/hwj
spring.datasource.speakhi.username=***
spring.datasource.speakhi.password=*****
spring.datasource.speakhi.driver-class-name=com.mysql.jdbc.Driver
# 青少数据库
spring.datasource.teens.jdbc-url=jdbc:mysql://10.1.139.167:3306/speakhi_teenager
spring.datasource.teens.username=***
spring.datasource.teens.password=****
#spring.datasource.teens.driver-class-name=com.mysql.jdbc.Driver
# 金蝶中间库
spring.datasource.kingdee.jdbc-url=*****
spring.datasource.kingdee.username=****
spring.datasource.kingdee.password=*****

  • 下面是多数据源的配置
    首先配置 DataSourceConfig主类
package com.webi.speakhimidplan.config;

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 javax.sql.DataSource;

/** 由于我们禁掉了自动数据源配置,因些下一步就需要手动将这些数据源创建出来 */

@Configuration
public class DataSourceConfig {
  @Bean(name = "speakHiDataSource")
  @ConfigurationProperties(prefix = "spring.datasource.speakhi")
  // application.properteis中对应属性的前缀
  public DataSource speakHiDataSource() {
    return DataSourceBuilder.create().build();
  }
  
  @Bean(name = "teensDataSource")
  @ConfigurationProperties(prefix = "spring.datasource.teens")
  // application.properteis中对应属性的前缀
  public DataSource teensDataSource() {
    return DataSourceBuilder.create().build();
  }
  /**
   * 金蝶第三库
   */
  @Bean(name = "kingDeeDataSource")
  @ConfigurationProperties(prefix = "spring.datasource.kingdee")
  // application.properteis中对应属性的前缀
  public DataSource kingDeeDataSource() {
    return DataSourceBuilder.create().build();
  }
}

  • 配置单独的文件,这里例如 下:
  • 这里有个坑就是
  • 由于我们禁掉了自动数据源配置,因些下一步就需要手动将这些数据源创建出来
    这里有多少个数据源就要有多少个配置类
    并且必须指定每个mapper.xml的路径【 factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
    .getResources(“classpath*:mapper/speakhi/*.xml”));】,不然会报:【org.apache.ibatis.binding.BindingException: Invalid bound statement
package com.webi.speakhimidplan.config;

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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
//这里的扫描包是同一个mapper包下分出三个包分别为kingdee, speakhi, teens 走不通的库
// @Bean(name = "sqlSessionFactoryKingDee") 必须与实体  sqlSessionFactoryRef = "sqlSessionFactoryKingDee" 名称一样
// private DataSource kingDeeDataSource必须和上面Datesource 文件中的@Bean对应上
@MapperScan(basePackages ={"com.webi.speakhimidplan.mapper.kingdee"},sqlSessionFactoryRef = "sqlSessionFactoryKingDee")
public class KingDeeDbConfig {
    @Resource
    private DataSource kingDeeDataSource;
    @Bean(name = "sqlSessionFactoryKingDee")
    public SqlSessionFactory sqlSessionFactorykingDee() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        // 使用kingdee数据源, 连接kingdee库
        factoryBean.setDataSource(kingDeeDataSource);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath*:mapper/kingdee/*.xml"));

        return factoryBean.getObject();

    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplatekingDee() throws Exception {
        // 使用上面配置的Factory
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactorykingDee());
        return template;
    }
}

  • 然后在下面的 src\main\java\com\webi\speakhimidplan\mapper\kingdee 文件目录下新建Mapper
  • 编写测试类进行测试
package com.webi.speakhimidplan;

import java.util.List;
import java.util.Map;

import com.webi.speakhimidplan.importdata.service.ImportDataService;
import com.webi.speakhimidplan.mapper.kingdee.KingDeeTestMapper;
import com.webi.speakhimidplan.mapper.speakhi.SpeakHiTestMapper;
import com.webi.speakhimidplan.mapper.teens.TeensTestMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpeakhiMidPlanApplicationTests {
  @Autowired
  ImportDataService importDataService;
  @Autowired
  SpeakHiTestMapper speakHiTestMapper;
  @Autowired
  TeensTestMapper teensTestMapper;
  @Resource
  KingDeeTestMapper kingdeeTestMapper;

  @Test
  public void contextLoads() {
    importDataService.importData();
    List all = kingdeeTestMapper.findAll();

    importDataService.importData();
    System.out.println("=============================");
    System.out.println(all.size());
    System.out.println("=============================");
  }

}

你可能感兴趣的:(springboot)