Spring boot + mybatis 只讀取到一個jar包中的mapper配置文件
阿新 • • 發佈:2019-01-29
typealias cor 需要 name art subst sources model eal
采用spring boot 開發了一個多模塊項目,有多個模塊中都有mapper配置文件。
采用如下的方式配置,制度去到了一個模塊jar包中配置文件:
@Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactoryBean() { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setTypeAliasesPackage("tk.mybatis.springboot.model"); MybatisInterceptor interceptor = new MybatisInterceptor(); bean.setPlugins(new Interceptor[]{interceptor}); //添加XML目錄 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); try { //\com\tritrust\t\core\dict\sqlmap//\com\tritrust\t\system\sqlmap bean.setMapperLocations(resolver.getResources("classpath:com/tritrust/t/**/sqlmap/*.xml")); return bean.getObject(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } }
需要讀取多個jar裏面的配置需要修改配置路徑為:
bean.setMapperLocations(resolver.getResources("classpath*:com/tritrust/t/**/sqlmap/*.xml"));
classpath*:就可以讀取多個jar裏面文件了。
查看spring core 中org.springframework.core.io.support.PathMatchingResourcePatternResolver類有如下代碼:
public Resource[] getResources(String locationPattern) throws IOException { Assert.notNull(locationPattern, "Location pattern must not be null"); if (locationPattern.startsWith("classpath*:")) { return this.getPathMatcher().isPattern(locationPattern.substring("classpath*:".length())) ? this.findPathMatchingResources(locationPattern) : this.findAllClassPathResources(locationPattern.substring("classpath*:".length())); } else { int prefixEnd = locationPattern.startsWith("war:") ? locationPattern.indexOf("*/") + 1 : locationPattern.indexOf(58) + 1; return this.getPathMatcher().isPattern(locationPattern.substring(prefixEnd)) ? this.findPathMatchingResources(locationPattern) : new Resource[]{this.getResourceLoader().getResource(locationPattern)}; } }
Spring boot + mybatis 只讀取到一個jar包中的mapper配置文件