最近踩到一个IntelliJ IDEA编译Springboot集成mybaits-plus项目的坑,记录一下分析过程。
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.sjjd.scanpen.mapper.QwBehaviorMapper.selectRecoredCount
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:227) ~[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.$Proxy105.selectRecoredCount(Unknown Source) ~[na:na]
at com.sjjd.scanpen.service.impl.QwBehaviorServiceImpl.isTokenAvaliable(QwBehaviorServiceImpl.java:26) ~[classes/:na]
at com.sjjd.scanpen.service.impl.QwBehaviorServiceImpl$.$FastClassBySpringCGLIB$.$e7278daa.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.19.RELEASE.jar:4.3.19.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:667) ~[spring-aop-4.3.19.RELEASE.jar:4.3.19.RELEASE]
at com.sjjd.scanpen.service.impl.QwBehaviorServiceImpl$.$EnhancerBySpringCGLIB$$2825463b.isTokenAvaliable(<generated>) ~[classes/:na]
at com.sjjd.scanpen.controller.UserPictureController.getExercisesByUid(UserPictureController.java:91) ~[classes/:na]
at com.sjjd.scanpen.controller.UserPictureController$.$FastClassBySpringCGLIB$.$84e195bf.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.19.RELEASE.jar:4.3.19.RELEASE]
-
贴出运行时异常
-
分析异常:
- org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.sjjd.scanpen.mapper.QwBehaviorMapper.selectRecoredCount
- 解释:QwBehaviorMapper.selectRecoredCount方法绑定异常
- 查找原因:这个方法绑定了xml文件的同名select语句,贴出QwBehaviorMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.sjjd.scanpen.mapper.QwBehaviorMapper"> <!-- 通用查询映射结果 --> <resultMap id="BaseResultMap" type="com.sjjd.scanpen.entity.QwBehavior"> <id column="id" property="id" /> <result column="uid" property="uid" /> <result column="type" property="type" /> <result column="detail" property="detail" /> <result column="datetime" property="datetime" /> </resultMap> <select id="selectRecoredCount" parameterType="java.lang.Integer" resultType="java.lang.Integer"> select count(id) from qw_behavior where DateDiff(datetime,now())=0 and uid= #{uid} and type=#{type} </select> </mapper>
- 检查Xmapper.xml文件编写
- <mapper namespace="com.sjjd.scanpen.mapper.QwBehaviorMapper"> 中的namespace中的全类名:无误;
- <select id="selectRecoredCount" parameterType="java.lang.Integer" resultType="java.lang.Integer">
select count(id) from qw_behavior where DateDiff(datetime,now())=0 and uid= #{uid} and type=#{type}
</select>- id="selectRecoredCount" 与中定义的方法名是否一致:无误;
- resultType是否设置错误:无误;(该方法只需要返回count数量,所以不使用resultMap)
- 如果以上xml文件编写均正确,检查编译后的target文件夹中是否存在Xmapper.xml:发现错误;
- 贴图
- 发现编译后的文件夹中缺少xml文件
- 检查Xmapper.xml文件编写
-
解决:
- 搜索“为什么编译后的target文件夹中没有mybatis的xml文件”,发现是IntelliJ IDEA编译Maven项目时不会像Eclipse一样将.xml 文件放到 target文件夹下的classes文件夹中。解决方式却很简单,在maven的pom.xml中<build>节点中增加以下代码
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources>
-
然后重新启动项目编译,就可以找到xml文件
-
完成
- 搜索“为什么编译后的target文件夹中没有mybatis的xml文件”,发现是IntelliJ IDEA编译Maven项目时不会像Eclipse一样将.xml 文件放到 target文件夹下的classes文件夹中。解决方式却很简单,在maven的pom.xml中<build>节点中增加以下代码
- 参考链接解决eclipse MAVEN项目导入使用intellij idea开发target目录下不存在mapper.xml文件问题