1. 程式人生 > >org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.x.x.xmapper.x

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.x.x.xmapper.x

最近踩到一個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]

  1. 貼出執行時異常

  2. 分析異常

    1. org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.sjjd.scanpen.mapper.QwBehaviorMapper.selectRecoredCount
    2. 解釋:QwBehaviorMapper.selectRecoredCount方法繫結異常
    3. 查詢原因:這個方法綁定了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>

       

      1. 檢查Xmapper.xml檔案編寫
        1. <mapper namespace="com.sjjd.scanpen.mapper.QwBehaviorMapper">   中的namespace中的全類名:無誤;
        2. <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>
          1. id="selectRecoredCount
            " 與中定義的方法名是否一致:無誤;
          2. resultType是否設定錯誤:無誤;(該方法只需要返回count數量,所以不使用resultMap
      2. 如果以上xml檔案編寫均正確,檢查編譯後的target資料夾中是否存在Xmapper.xml:發現錯誤;
        1. 貼圖
        2. 發現編譯後的資料夾中缺少xml檔案
    4. 解決

      1. 搜尋“為什麼編譯後的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>

         

      2. 然後重新啟動專案編譯,就可以找到xml檔案

      3. 完成

    5. 參考連結解決eclipse MAVEN專案匯入使用intellij idea開發target目錄下不存在mapper.xml檔案問題