Caused by: org.apache.ibatis.builder.IncompleteElementException: Could not find result map xxxxxxx
由于对jar包方式加载资源感兴趣,单独拉出来代码
public class JarResoureLoaderTest { @Test public void test() throws IOException { String mapperLocation = "classpath*:com/coe/cdcs/**/xml/**/*.xml"; String rootDirPath = "classpath*:com/coe/cdcs/"; String subPattern = "**/xml/**/*.xml"; String path = "com/coe/cdcs/"; Resource[] rootDirResources = getResources(path); Set<Resource> result = new LinkedHashSet<>(16); for (Resource rootDirResource : rootDirResources) { URL rootDirUrl = rootDirResource.getURL(); if(ResourceUtils.isJarURL(rootDirUrl)){ result.addAll(doFindPathMatchingJarResources(rootDirResource,rootDirUrl,subPattern)); } } Resource[] mappers = result.toArray(new Resource[0]); } private Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,URL rootDirURL,String subPattern) throws IOException { URLConnection con = rootDirURL.openConnection(); JarFile jarFile = null; String jarFileUrl; //com/coe/cdcs/ String rootEntryPath = null; boolean closeJarFile = false; if (con instanceof JarURLConnection) { JarURLConnection jarCon = (JarURLConnection) con; ResourceUtils.useCachesIfNecessary(jarCon); jarFile = jarCon.getJarFile(); jarFileUrl = jarCon.getJarFileURL().toExternalForm(); JarEntry jarEntry = jarCon.getJarEntry(); rootEntryPath = (jarEntry != null ? jarEntry.getName() : ""); closeJarFile = !jarCon.getUseCaches(); } /** * 1. jar:file:/C:/Users/zhangjie/Desktop/出口组异常纪录/cdcs-admin-0.0.2-SNAPSHOT.jar!/BOOT-INF/classes * 1.1 第一次entryPath 为 "com/" * 2. result添加顺序有误(重点) */ try{ Set<Resource> result = new LinkedHashSet<>(8); for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) { PathMatcher pathMatcher = new AntPathMatcher(); JarEntry entry = entries.nextElement(); String entryPath = entry.getName(); if (entryPath.startsWith(rootEntryPath)) { //相对路径 String relativePath = entryPath.substring(rootEntryPath.length()); //subPattern = **/xml/**/*.xml -->判断相对路径是否符合正则 if(pathMatcher.match(subPattern,relativePath)){ result.add(rootDirResource.createRelative(relativePath)); } } } return result; }finally { if (closeJarFile) { jarFile.close(); } } } private Resource[] getResources(String rootDirPath) throws IOException{ // admin和core包都包含com/coe/cdcs/ Set<Resource> result = new LinkedHashSet<>(16); ResourceLoader resourceLoader = new DefaultResourceLoader(); ClassLoader cl = resourceLoader.getClassLoader(); Enumeration<URL> resourceUrls = cl.getResources(rootDirPath); while(resourceUrls.hasMoreElements()){ URL url = resourceUrls.nextElement(); Resource resource = new UrlResource(url); result.add(resource); } Resource[] rootDirResources = result.toArray(new Resource[0]); return rootDirResources; } }