项目是Springboot Mybaits,使用了org.mybatis.spring.boot.autoconfigure,可以自动配置Mybaits。(我的理解)
我的locations是这样写的:
mybatis.mapper-locations=classpath*:com/uaes/ehatsc/SSM/Dao/mappers/*.xml
在项目运行时出现了not found的报错,百度无果,遂源码debug,最终发现设置的locations并没有生效,而是使用了---Dao同路径下的同名xml。
源码 org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.class :
private void loadXmlResource() { // Spring may not know the real resource name so we check a flag // to prevent loading again a resource twice // this flag is set at XMLMapperBuilder#bindMapperForNamespace if (!configuration.isResourceLoaded("namespace:" + type.getName())) {
// Debug可以看到 xmlResource=“com/uaes/ehatsc/SSM/Dao/SummaryDao.xml”
//对应的Dao是com/uaes/ehatsc/SSM/Dao/SummaryDao.class
//设置的mybaits.mapper-locations=classpath*:com/uaes/ehatsc/SSM/Dao/mappers/*.xml并没有生效!
String xmlResource = type.getName().replace('.', '/') + ".xml";
InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(type.getClassLoader(), xmlResource); } catch (IOException e) { // ignore, resource is not required } if (inputStream != null) { XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, assistant.getConfiguration(), xmlResource, configuration.getSqlFragments(), type.getName()); xmlParser.parse(); } } }
最后将xml移动到了Dao同路径下,并且改成了Dao的名字,暂时解决了问题,后续继续查找locations为什么没有设置进去。