Mybatis 中Mapper使用package方式配置報錯的解決方案
Mybatis 中Mapper使用package方式配置報錯
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
UserDaoTest中調用了UserDao的insert方法。
1.項目結構如下package com.mybatis.dao; import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param; import java.util.List;import com.mybatis.pojo.User; @Mapperpublic interface UserDao { void insert( User user); int insertSelective(@Param('user') User user); int insertList(@Param('users') List<User> users); int update(@Param('user') User user); User findbyId(@Param('id')Integer id); }3.UserDao.xml
已經按照正常的package的配置方式,將接口與xml文件放在同一個目錄下,其他配置也沒問題,就是報找不到UserDao中的方法。
結果去target中看了一眼發(fā)現(xiàn),xml文件沒加載。。。。。
解決方案原來是IDEA maven項目默認不會把src下除java文件外的文件打包到classes文件夾下,需要在maven中增加配置如下
<build><resources> <resource><directory>src/main/java</directory><includes> <include>**/*.xml</include></includes><!--默認是true--><!--<filtering>true</filtering>--> </resource></resources> </build>
這樣xml文件就可以加載了,動態(tài)代理為UserDao接口生成實現(xiàn)類,而實現(xiàn)類的具體實現(xiàn)細節(jié)就是在xml中,通過package掃描的方式找到xml,就可以正確的生成UserDao的代理類了。
而xml無法加載,就會造成動態(tài)代理生成的代理類是無效的(這個代理類對象是可以生成的),當調用方法就會出現(xiàn)開頭的錯誤。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關文章:
1. mybatis plus動態(tài)數(shù)據(jù)源切換及查詢過程淺析2. MongoDB快速入門筆記(八)之MongoDB的java驅動操作代碼講解3. Mybatis如何自動生成數(shù)據(jù)庫表的實體類4. SQLite3 命令行操作指南5. 解決db2事務日志已滿及日志磁盤空間已滿問題辦法詳解6. SQLite教程(二):C/C++接口簡介7. 解決mybatis中的mapper命名問題8. Mybatis分頁PageHelper插件代碼實例9. gearman中worker常駐后臺,導致MySQL server has gone away的解決方法10. SQL Server數(shù)據(jù)庫判斷最近一次的備份執(zhí)行結果(最新推薦)
