Mybatis 中Mapper使用package方式配置報(bào)錯(cuò)的解決方案
Mybatis 中Mapper使用package方式配置報(bào)錯(cuò)
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

UserDaoTest中調(diào)用了UserDao的insert方法。
1.項(xiàng)目結(jié)構(gòu)如下
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


已經(jīng)按照正常的package的配置方式,將接口與xml文件放在同一個(gè)目錄下,其他配置也沒(méi)問(wèn)題,就是報(bào)找不到UserDao中的方法。
結(jié)果去target中看了一眼發(fā)現(xiàn),xml文件沒(méi)加載。。。。。
解決方案原來(lái)是IDEA maven項(xiàng)目默認(rèn)不會(huì)把src下除java文件外的文件打包到classes文件夾下,需要在maven中增加配置如下
<build><resources> <resource><directory>src/main/java</directory><includes> <include>**/*.xml</include></includes><!--默認(rèn)是true--><!--<filtering>true</filtering>--> </resource></resources> </build>
這樣xml文件就可以加載了,動(dòng)態(tài)代理為UserDao接口生成實(shí)現(xiàn)類(lèi),而實(shí)現(xiàn)類(lèi)的具體實(shí)現(xiàn)細(xì)節(jié)就是在xml中,通過(guò)package掃描的方式找到xml,就可以正確的生成UserDao的代理類(lèi)了。
而xml無(wú)法加載,就會(huì)造成動(dòng)態(tài)代理生成的代理類(lèi)是無(wú)效的(這個(gè)代理類(lèi)對(duì)象是可以生成的),當(dāng)調(diào)用方法就會(huì)出現(xiàn)開(kāi)頭的錯(cuò)誤。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Linux CentOS7安裝Oracle11g的超完美新手教程2. MySQL存儲(chǔ)過(guò)程的查詢(xún)命令介紹3. 傳甲骨文將增加對(duì)MySQL投資與微軟競(jìng)爭(zhēng)4. SQLite教程(十四):C語(yǔ)言編程實(shí)例代碼(2)5. SQLite數(shù)據(jù)庫(kù)安裝及基本操作指南6. centos編譯安裝mariadb的詳細(xì)過(guò)程7. SQLITE3 使用總結(jié)8. Mysql InnoDB和MyISAM區(qū)別原理解析9. MariaDB的安裝與配置教程10. SQLite教程(二):C/C++接口簡(jiǎn)介

網(wǎng)公網(wǎng)安備