1. 程式人生 > >springboot+mybatis+mapper 多資料來源對映不到實體

springboot+mybatis+mapper 多資料來源對映不到實體

專案結構

application.yml

spring:
  profiles:
    active: prd

application-prd.yml

spring:
  datasource:
    test1:
      driver-class-name: com.mysql.jdbc.Driver
      username: ****
      password: ****
      jdbc-url: jdbc:mysql://192.168.5.155:3306/test01?characterEncoding=UTF-8
    test2:
      driver-class-name: com.mysql.jdbc.Driver
      username: ****
      password: ****
      jdbc-url: jdbc:mysql://192.168.5.155:3306/test02?characterEncoding=UTF-8


mybatis:
  #對映實體位置
  #如果mapper.xml放在resources中可以生效
  #如果mapper.xml放在dao介面的包中不生效
  type-aliases-package: cn.***.pojo
  #如果mapper.xml檔案放在resources中需要下面這行配置,如果和dao介面放在一個包中不需要
  #  mapper-locations: classpath:mybatis/mapper/*.xml


TestDataSource1

    如果mapper.xml檔案放在和dao一個包中一定要加入bean.setTypeAliasesPackage("cn.***.pojo"),不然對映不到實體

@Configuration // 註冊到springboot容器中
@MapperScan(basePackages = "cn.***.dao", sqlSessionFactoryRef = "test1SqlSessionFactory")
public class TestDataSource1 {

    /**
     * @methodDesc: 功能描述:(配置test1資料庫)
     */
    @Bean(name = "test1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.test1")//把同類的配置資訊自動封裝成實體類
    @Primary//自動裝配時當出現多個Bean候選者時,被註解為@Primary的Bean將作為首選者,否則將丟擲異常
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
     * @methodDesc: 功能描述:(test1 sql會話工廠)
     */
    @Bean(name = "test1SqlSessionFactory")
    @Primary//自動裝配時當出現多個Bean候選者時,被註解為@Primary的Bean將作為首選者,否則將丟擲異常
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //mybatis實體對映
        bean.setTypeAliasesPackage("cn.***.pojo");
        // bean.setMapperLocations(
        // new
        // PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
        return bean.getObject();
    }

    /**
     * @methodDesc: 功能描述:(test1 事物管理)
     */
    @Bean(name = "test1TransactionManager")
    @Primary//自動裝配時當出現多個Bean候選者時,被註解為@Primary的Bean將作為首選者,否則將丟擲異常
    public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "test1SqlSessionTemplate")
    @Primary//自動裝配時當出現多個Bean候選者時,被註解為@Primary的Bean將作為首選者,否則將丟擲異常
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

TestDataSource1

    如果mapper.xml檔案放在和dao一個包中一定要加入bean.setTypeAliasesPackage("cn.***.pojo"),不然對映不到實體

@Configuration // 註冊到springboot容器中
@MapperScan(basePackages = "cn.***.dao1", sqlSessionFactoryRef = "test2SqlSessionFactory")
public class TestDataSource2 {
    /**
     *
     * @methodDesc: 功能描述:(配置test2資料庫)
     */
    @Bean(name = "test2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.test2")//把同類的配置資訊自動封裝成實體類

    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
     *
     * @methodDesc: 功能描述:(test2 sql會話工廠)

     */
    @Bean(name = "test2SqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //如果mapper和dao放在一個包中需要mybatis實體對映
        bean.setTypeAliasesPackage("cn.***.pojo");
        // bean.setMapperLocations(
        // new
        // PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test2/*.xml"));
        return bean.getObject();
    }

    /**
     *
     * @methodDesc: 功能描述:(test2 事物管理)
     */
    @Bean(name = "test2TransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "test2SqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

pom.xml

    如果想將mapper.xml和和dao介面放在一個包中需要在pom中的<build>標籤中加入如下配置

    如果mapper.xml放在resource資原始檔中,pom中不需要配置。

    原因:編譯器會預設到resources資原始檔中尋找配置檔案

<!--指定mapper存放路徑-->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
      <!--指定配置檔案存放路徑-->
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.yml</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>