Java书签 #MyBatis之setMapperLocations配置多个mapper路径的两种方法详解

1.今日书签

SpringBoot 集成 MyBatis 的项目中,怎么使用 sqlSessionFactoryBean.setMapperLocations 通配符来配置多个包路径呢?
或者说,通过 MyBatis JavaBean 的形式,怎么使用 setMapperLocations 加载多个路径下的 xml 文件?
亦或者说,使用 sqlSessionFactoryBean.setMapperLocationsmybatis.mapper-locations 通配符配置多个 mapper 路径有哪两种设置方式?

项目技术栈升级 SpringBoot 2 + MyBatis 3,说一说踩过的坑,梦里挑灯看剑。

2.挑灯看剑

1)使用JavaBean配置

主要是使用 org.mybatis.spring.SqlSessionFactoryBean 的 setMapperLocations(Resource... mapperLocations) 方法,入参为一组 Resource,即我们需要加载的 xml 文件路径数组

@Slf4j
@Configuration
@MapperScan(basePackages = {
    
    "com.meiwei.tan.dao.mall", "com.meiwei.ping.dao.crm"})
public class MyBatisConfiguration {
    
    

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception{
    
    
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setMapperLocations(resolveMapperLocations());
        return sqlSessionFactoryBean.getObject();
    }

    public Resource[] resolveMapperLocations() {
    
    
        ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
        List<String> mapperLocations = new ArrayList<>();
        mapperLocations.add("classpath*:com/meiwei/tan/dao/**/*Dao.xml");
        mapperLocations.add("classpath*:com/meiwei/ping/dao/**/*Mapper.xml");
        List<Resource> resources = new ArrayList();
        if (!CollectionUtils.isEmpty(mapperLocations)) {
    
    
            for (String mapperLocation : mapperLocations) {
    
    
                try {
    
    
                    Resource[] mappers = resourceResolver.getResources(mapperLocation);
                    resources.addAll(Arrays.asList(mappers));
                } catch (IOException e) {
    
    
                    log.error("Get myBatis resources happened exception", e);
                }
            }
        }

        return resources.toArray(new Resource[resources.size()]);
    }

2)使用application.yml配置

application.yml 配置中集成 mybatis,可能要写多份。

spring:
  application:
    name: meiwei
  profiles:
    active: dev
apollo:
  bootstrap:
    enabled: true
    namespaces: application,db,dubbo,redis,datasource,zk,mq

gatewayapi:
  client:
    autoRegistry:
      enabled: true
    connectStr: ${
    
    zk.address} #${zk.address} 来自于 apollo 里面的 zk
    root: /gateway/api/services

mybatis:
  mapper-locations: classpath*:com/meiwei/tan/dao/**/*Dao.xml,classpath*:com/meiwei/ping/dao/**/*Mapper.xml
  type-aliases-package: com.meiwei.tan.dao.mall,com.meiwei.ping.dao.crm
  IDENTITY: MYSQL #取回主键的方式
  notEmpty: false #insert和update中,是否判断字符串类型!=''
  configuration:  #进行自动映射时,数据以下划线命名,如数据库返回的"order_total"命名字段是否映射为class的"orderTotal"字段。默认为false
    map-underscore-to-camel-case: true

对于 MyBatis,这里可以写一个空的自定义 MyBatisConfiguration 类不做实现,仅加 @MapperScan 和 @Configuration 注解,以便使用 MyBatisConfiguration 类做为 mybatis 配置文件被加载,并去扫描指定包路径下的 dao interface。然后 启动 主启动类。

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan(value = {
    
    "com.meiwei.tan.dao.mall", "com.meiwei.ping.dao.crm"})
public class MyBatisConfiguration {
    
    

}

 
也可以不写这个自定义的 MyBatisConfiguration 类,直接把 @MapperScan 配置注解到 主启动类上。

但是,不难看出,业务层 module 的单元测试不好读到 web 或 remote module 的 yml 配置,所以第二种方法用 application.yml 配置集成 mybatis 启动主程序方法后,是跑主程序所在的 module 中的测试用例(通过RPC服务消费),而不是跑业务层 module 中的测试用例。

猜你喜欢

转载自blog.csdn.net/itanping/article/details/108563519