1. 程式人生 > >maven專案下報錯mybatis org.apache.ibatis.binding.BindingException: Invalid bound statement(not found)

maven專案下報錯mybatis org.apache.ibatis.binding.BindingException: Invalid bound statement(not found)

maven專案下報錯mybatis org.apache.ibatis.binding.BindingException: Invalid bound statement(not found

問題原因

今天在maven專案下遇到org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)這個錯,到target目錄下看是否有xxmapper.xml檔案生成(按照自己對xxmapper.xml檔案的路徑配置到target下classes目錄下找),發現沒有對應的xxmapper.xml檔案。總結原因是受到以前整個springMvc整合mybatis的影響,將檔案放在了src/main/java下,導致一直掃描不到xxxMapper.xml檔案。而idea中maven打包的時候不會將xxxMapper.xml打包,target打包編譯之後不會有xxxMapper.xml檔案,所以一直掃描不到而報此錯。

解決辦法

有以下幾種辦法:
1、將xxxMapper.xml檔案放在了src/main/resources下,然後在application.properties檔案中新增配置

mybatis.mapper-locations=classpath:mappings/*.xml

2、在application.xml檔案中加入配置:

mybatis:
  mapper-locations: classpath:mappings/*.xml

3、到target目錄下看是否有mapper.xml檔案生成(按照自己對mapper.xml檔案的路徑配置到target下classes目錄下找),如果沒有可以在pom.xml檔案的<build></build>

之間新增

<resources>
     <resource>
          <directory>src/main/resources</directory>
          <includes>
              <include>**/*.xml</include>
          </includes>
     </resource>
 </resources>

或者也可以再在<resources></resources>之間再新增一項

<resource>
    <directory>src/main/java</directory>
    <includes>
        <include>**/*.xml</include>
    </includes>
</resource>

使mapper.xml檔案不論是放在src/main/resources或src/main/java下都可以被打包到classes目錄下。

4、使用@Select、@Insert、@Update、@Delete註解代替xxxMapper.xml裡面的內容,這樣就不存在掃描xxxMapper.xml檔案的問題了。

其它原因

1、xxxMapper.xml檔案中的namespace對應有誤
2、xxxMapper.java檔案中沒有對應的xxxMapper.xml中的方法。

在此mark以下,防止以後再次踩坑。