项目场景:
SpringBoot项目,MyBatis 异常:xxxMapper.xml 中无法找到 refid 为 xxx 的 片段;更正之后,多次重新启动仍然无法消除这个异常。
问题描述:
org.apache.ibatis.builder.IncompleteElementException: Could not find SQL statement to include with refid 'org.peanutvideo.system.mapper.AppUserInfoMapper.selectAppUser'
java.lang.IllegalArgumentException: XML fragments parsed from previous mappers does not contain value for org.peanutvideo.system.mapper.AppUserInfoMapper.selectAppUser
at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:888) ~[mybatis-3.4.6.jar:3.4.6]
at org.apache.ibatis.builder.xml.XMLIncludeTransformer.findSqlFragment(XMLIncludeTransformer.java:96) ~[mybatis-3.4.6.jar:3.4.6]
at org.apache.ibatis.builder.xml.XMLIncludeTransformer.applyIncludes(XMLIncludeTransformer.java:61) ~[mybatis-3.4.6.jar:3.4.6]
at org.apache.ibatis.builder.xml.XMLIncludeTransformer.applyIncludes(XMLIncludeTransformer.java:83) ~[mybatis-3.4.6.jar:3.4.6]
at org.apache.ibatis.builder.xml.XMLIncludeTransformer.applyIncludes(XMLIncludeTransformer.java:51) ~[mybatis-3.4.6.jar:3.4.6]
at org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode(XMLStatementBuilder.java:88) ~[mybatis-3.4.6.jar:3.4.6]
at org.apache.ibatis.session.Configuration.buildAllStatements(Configuration.java:788) ~[mybatis-3.4.6.jar:3.4.6]
at org.apache.ibatis.session.Configuration.hasStatement(Configuration.java:758) ~[mybatis-3.4.6.jar:3.4.6]
at org.apache.ibatis.session.Configuration.hasStatement(Configuration.java:753) ~[mybatis-3.4.6.jar:3.4.6]
at org.apache.ibatis.binding.MapperMethod$SqlCommand.resolveMappedStatement(MapperMethod.java:249) ~[mybatis-3.4.6.jar:3.4.6]
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:219) ~[mybatis-3.4.6.jar:3.4.6]
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:49) ~[mybatis-3.4.6.jar:3.4.6]
at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:65) ~[mybatis-3.4.6.jar:3.4.6]
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58) ~[mybatis-3.4.6.jar:3.4.6]
at com.sun.proxy.$Proxy62.selectAppUserCounter(Unknown Source) ~[na:na]
at org.peanutvideo.system.service.app.impl.AppUserServiceImpl.selectAppUserCounter(AppUserServiceImpl.java:30) ~[classes/:na]
at org.peanutvideo.web.controller.app.AppUserController.loginByCode(AppUserController.java:32) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_202]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_202]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_202]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_202]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at ………………………………………………………………………………
原因分析:
查看异常的 xxxMapper.xml 文件,找到问题片段, 片段确实在 引用之前, 中的 id 和 中的 refid 完全一样。
打开 target 目录下的 xxxMapper.xml 文件发现错误片段,项目重启后并没有更新代码到 target 目录,引起了这次异常。
注: target 目录是编译后文件存放的地方
解决方案:
方法 I
每次更新代码后,build 一下。
方法 II
在 maven 的 pom.xml 中配置 build 选项。