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è)目錄下,其他配置也沒問題,就是報(bào)找不到UserDao中的方法。
結(jié)果去target中看了一眼發(fā)現(xiàn),xml文件沒加載。。。。。
解決方案原來是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)類,而實(shí)現(xiàn)類的具體實(shí)現(xiàn)細(xì)節(jié)就是在xml中,通過package掃描的方式找到xml,就可以正確的生成UserDao的代理類了。
而xml無法加載,就會(huì)造成動(dòng)態(tài)代理生成的代理類是無效的(這個(gè)代理類對(duì)象是可以生成的),當(dāng)調(diào)用方法就會(huì)出現(xiàn)開頭的錯(cuò)誤。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
