溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Mybatis中Mapper使用package方式配置報(bào)錯(cuò)如何解決

發(fā)布時(shí)間:2021-07-13 13:38:28 來源:億速云 閱讀:511 作者:chen 欄目:開發(fā)技術(shù)

這篇文章主要講解了“Mybatis中Mapper使用package方式配置報(bào)錯(cuò)如何解決”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Mybatis中Mapper使用package方式配置報(bào)錯(cuò)如何解決”吧!

踩了個(gè)坑,寫出來

Mybatis 中Mapper使用package方式配置報(bào)錯(cuò)

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

Mybatis中Mapper使用package方式配置報(bào)錯(cuò)如何解決

UserDaoTest中調(diào)用了UserDao的insert方法。

1.項(xiàng)目結(jié)構(gòu)如下

Mybatis中Mapper使用package方式配置報(bào)錯(cuò)如何解決

2.UserDao接口

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;
 
@Mapper
public 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

Mybatis中Mapper使用package方式配置報(bào)錯(cuò)如何解決

4.Mybatis配置文件 Mybatis-config.xml

Mybatis中Mapper使用package方式配置報(bào)錯(cuò)如何解決

已經(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ò)誤。

感謝各位的閱讀,以上就是“Mybatis中Mapper使用package方式配置報(bào)錯(cuò)如何解決”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)Mybatis中Mapper使用package方式配置報(bào)錯(cuò)如何解決這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI