package org.focusns.common.dao.mybatis;

import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

public class SqlSessionFactoryBean extends org.mybatis.spring.SqlSessionFactoryBean implements ResourceLoaderAware {

    private static final String MAPPER_LOCATION_PATTERN = "classpath*:*/**/*Mapper.xml";

    private String mapperBasePackage;
    //private String mapperLocationPattern = MAPPER_LOCATION_PATTERN;
    private ResourcePatternResolver resourcePatternResolver;


    public void setMapperBasePackage(String mapperBasePackage) {
        this.mapperBasePackage = mapperBasePackage;
    }

    /*public void setMapperLocationPattern(String mapperLocationPattern) {
        this.mapperLocationPattern = mapperLocationPattern;
    }*/

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        //
        Assert.hasText(mapperBasePackage, "Mapper base package can not be null!");
        //
        String mapperLocationPattern = ClassUtils.convertClassNameToResourcePath(mapperBasePackage) + "/**/*Mapper.xml";
        Resource[] mapperLocations = resourcePatternResolver.getResources(mapperLocationPattern);
        Assert.notEmpty(mapperLocations, String.format("Mapper Locations is empty under package %s!", mapperBasePackage));
        //
        super.setMapperLocations(mapperLocations);
        //
        super.afterPropertiesSet();
    }
}

@@ -538,192 +538,32 @@
 ate 
-static final String MAPPER_LOCATION_PATTERN = %22classpath*:*/**/*Mapper.xml%22;%0A%0A    private String mapperBasePackage;%0A    //private String mapperLocationPattern = MAPPER_LOCATION_PATTERN
+String mapperBasePackage
 ;%0A  
@@ -622,17 +622,16 @@
 olver;%0A%0A
-%0A
     publ
@@ -750,152 +750,8 @@
  %7D%0A%0A
-    /*public void setMapperLocationPattern(String mapperLocationPattern) %7B%0A        this.mapperLocationPattern = mapperLocationPattern;%0A    %7D*/%0A%0A
     
@@ -1105,32 +1105,32 @@
 !%22);%0A        //%0A
-
         String m
@@ -1151,16 +1151,83 @@
 attern =
+ ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +%0A               
  ClassUt

解决 SqlSessionFactoryBean 在单元测试中无法扫描到 Mapper 文件的 BUG