引用地址:https://guozh.net/idea-org-apache-ibatis-binding-bindingexception-invalid-bound-statement-not-found/ 這位大哥寫的很詳細,就拿過來用了
IDEA org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
前言
在使用 IDEA 當作工具使用 Mybatis+Spring 時,總是能碰到 Eclipse 碰不到的“蛋疼問題”,這應該是屬於 IDEA 的陣痛期,這個錯我已經碰到4次了,而且每次原因還不太同。久病成良醫,下一次應該不會再出現了,記錄下解決辦法,希望能幫到你,因為我已經在這個問題上浪費太多時間了。
情況
Mybatis+Spring 配置有好幾種方法。最常用的應該是 Mapper 動態代理開發,但是動態代理配置也有兩種。
<!-- Mapper代理的方式開發方式一,配置Mapper代理對象 --> <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <!-- 配置Mapper接口 --> <property name="mapperInterface" value="cn.itcast.mybatis.mapper.UserMapper" /> <!-- 配置sqlSessionFactory --> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean>
<!-- Mapper動態代理開發 開發方式2 掃描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.guozh.bitbooks.mapper" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean>
一般使用第二種,本人也是使用第二種。如果第二種 首先有幾點要確定。
- *Mapper.java 和 *Mapper.xml 命名是否一致
- 是否在一個包下,掃描能不能掃到 .xml。
- *Mapper.xml 的 namespace 配置是否准確,接口方法名是否准確。
以上都是 mybatis 的基本知識,當然第二點 可以不再同一個包下。很多種寫法,可能有人喜歡將 *Mapper.xml 放在 resources 下。無所謂,只要能掃描到就可以了。
ok 到這里如果還報錯如上錯誤,和我一樣,接着看吧
解決
- 刪除編輯后的文件 class,然后
Rebuild在運行。 - 如果上面方法不行,仔細找找在編譯后的classes 中 *Mapper.xml 生成了沒,如果沒 那一定有問題。

我的項目結構是這樣的,並且 *Mapper.xml 放在 java 中。這里就會有一個問題。
idea默認是不編譯 src\main\java下的xml文件
所以這就需要我們手動配置下。
- 將 *Mapper.xml 復制一份到 resources 下,記得修改 mybatis 配置文件的路徑。
- 在 pom.xml 中配置資源代碼
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>
ok 希望能幫到你。
