项目结构

springboot+mybatis+mapper 多数据源映射不到实体-LMLPHP

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资源文件中寻找配置文件

springboot+mybatis+mapper 多数据源映射不到实体-LMLPHP

<!--指定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>
06-22 22:51