Search in sources :

Example 1 with MapperScan

use of org.mybatis.spring.annotation.MapperScan in project mybatis-pro by Dreamroute.

the class MyBatisProAutoConfiguration method getMapperPackages.

/**
 * 获取mapper接口的包路径集合
 */
private Set<String> getMapperPackages() {
    Set<String> mapperPackages = new HashSet<>();
    // mapperScanMap如果为空会返回一个size = 0的Map
    Map<String, Object> mapperScanMap = context.getBeansWithAnnotation(MapperScan.class);
    mapperScanMap.values().forEach(scan -> {
        Class<?> scanCls = scan.getClass();
        MapperScan ms = AnnotationUtils.findAnnotation(scanCls, MapperScan.class);
        if (ms != null) {
            String[] value = ms.value() != null ? ms.value() : new String[0];
            String[] basePackages = ms.basePackages() != null ? ms.basePackages() : new String[0];
            Class<?>[] basePackageClasses = ms.basePackageClasses() != null ? ms.basePackageClasses() : new Class<?>[0];
            mapperPackages.addAll(asList(value));
            mapperPackages.addAll(asList(basePackages));
            mapperPackages.addAll(stream(basePackageClasses).map(cls -> cls.getPackage().getName()).collect(toSet()));
        }
    });
    logger.info("MyBatis-Pro检测出Mapper路径包括: {}", mapperPackages);
    return mapperPackages;
}
Also used : MapperScan(org.mybatis.spring.annotation.MapperScan) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) HashSet(java.util.HashSet)

Aggregations

HashSet (java.util.HashSet)1 MapperScan (org.mybatis.spring.annotation.MapperScan)1 ConditionalOnClass (org.springframework.boot.autoconfigure.condition.ConditionalOnClass)1