解決Mybatis中mapper.xml文件update,delete及insert返回值問題
最近寫了幾個(gè)非常簡單的接口(CRUD),在單元測試的時(shí)候卻出了問題,報(bào)錯(cuò)如下:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ’messageListener’: Unsatisfied dependency expressed through field ’reviewCheckInfoService’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ’reviewCheckInfoServiceImpl’: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ’reviewCheckInfoDao’ defined in file [/Users/a1475368628/IdeaProjects/baby-customer-parent/baby-customer-service/target/classes/com/dianping/baby/customer/reviewcheck/dao/ReviewCheckInfoDao.class]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property ’sqlSessionFactory’ threw exception; nested exception is java.lang.NullPointerException
經(jīng)過仔細(xì)排查,問題的原因出在sql的xml配置文件出錯(cuò),直接原因是在update中錯(cuò)誤的使用了KeyProperty、useGeneratedKeys。
學(xué)習(xí)相關(guān)知識(shí)之后,在這里仔細(xì)總結(jié)一下。
在Mybatis的xml配置文件中,insert和update中可以設(shè)置屬性KeyProperty、useGeneratedKeys,用來返回自增主鍵的值。
在DAO層中有一個(gè)方法:
public int addUser(@Param('user')User user);
對(duì)應(yīng)的xml文件中為:
<insert id='addUserType='map' keyProperty='user.id' useGeneratedKeys='true'> INSERT INTO a_user ( AgeType, CityId, AddTime, UpdateTime ) VALUES ( #{user.ageType}, #{user.cityId}, NOW(), NOW() )</insert>
我錯(cuò)誤的認(rèn)為是addUser()這個(gè)方法會(huì)返回插入記錄的自增id值,結(jié)果測試的時(shí)候,addUser()方法返回值始終是1。仔細(xì)學(xué)習(xí)后得知:
insert對(duì)應(yīng)的方法返回值為插入數(shù)據(jù)庫的條數(shù)(如上,每次插入一條數(shù)據(jù),所以每次addUser()都是返回1)
update對(duì)應(yīng)的方法返回值為匹配數(shù)據(jù)庫的條數(shù)(不論最終是否對(duì)數(shù)據(jù)進(jìn)行了修改,只要某條記錄符合匹配條件,返回值就加1)
舉例:
update table_name set name='li' where cid = 3.
假如數(shù)據(jù)庫中有2條數(shù)據(jù)如下:
1. name:li cid=3
2. name:ly cid=3
這兩條數(shù)據(jù)都符合update匹配條件,但是第1條數(shù)據(jù)不需要修改,只是更改了第2條數(shù)據(jù)的name值,最終返回值依舊為2
delete對(duì)應(yīng)方法返回值為刪除的數(shù)據(jù)條數(shù)
而KeyProperty、useGeneratedKeys這兩個(gè)屬性是用來設(shè)置user對(duì)象中的id值(id為自增主鍵)的。
User user = new User();user.setAgeType(1); user.setCityId(1);addUser(user);System.out.println(user.id);
如上,我們并沒有設(shè)置user對(duì)象的id值,但是卻能輸出正確的id。
當(dāng)然,使用 selectKey也能達(dá)到同樣的效果
補(bǔ)充知識(shí):Mybatis Update操作 返回值修改為受影響條數(shù)
到底 update 返回值代表什么呢?我們來驗(yàn)證一下便知道了,假設(shè)有如下一張表以及兩條數(shù)據(jù):
我們來編寫一個(gè)簡單的單元測試用例來驗(yàn)證下,首先使用 mybatis 簡單的寫個(gè) mapper 進(jìn)行更新操作,其中 xml 中的內(nèi)容為:
數(shù)據(jù)庫連接配置為:
接來下,我們來編寫一個(gè)簡單的單元測試來驗(yàn)證下: update 的返回值是不是受影響的記錄的條數(shù) ,對(duì)應(yīng)的單元測試代碼如下:
由單元測試代碼可以得知,我們將要把數(shù)據(jù)庫中兩條記錄的 phone 字段的值由 12345678 修改為 66666666 ,正常情況下, resultCode 將會(huì)返回 2 。因?yàn)?update 操作影響到數(shù)據(jù)庫中這 2 條記錄,這和我們期望 2 是相符合的。那么一切正常的情況下,這次單元測試將會(huì)通過,那么我們運(yùn)行看看結(jié)果:
單元測試通過了,再查看數(shù)據(jù)庫中的記錄:
這說明 mybatis 的 update 更新操作返回值的確是返回受影響的行數(shù)……真的是這樣嗎?
我們知道,當(dāng)數(shù)據(jù)庫中的記錄被修改之后,再次執(zhí)行重復(fù)的 update 操作將不會(huì)影響到新的行數(shù),為了驗(yàn)證我說的話,我們?cè)囋嚕?/p>
那么,按照這個(gè)邏輯:我們?cè)俅螆?zhí)行這個(gè)單元測試應(yīng)該是, resultCode 返回的應(yīng)該是 0 ,和我們的期望的數(shù)字 2 不一致,將會(huì)導(dǎo)致測試不通過。再次運(yùn)行單元測試:
居然還是 passed ,看到這里聰明的你已經(jīng)看出來了, 默認(rèn)情況下,mybatis 的 update 操作返回值是記錄的 matched 的條數(shù),并不是影響的記錄條數(shù)。 嚴(yán)格意義上來將,這并不是 mybatis 的返回值,mybatis 僅僅只是返回的數(shù)據(jù)庫連接驅(qū)動(dòng)(通常是 JDBC )的返回值,也就是說,如果驅(qū)動(dòng)告知更新 2 條記錄受影響,那么我們將得到 mybatis 的返回值就會(huì)是 2 和 mybatis 本身是沒有關(guān)系的。 道理我都懂,如果我們非得要 mybatis 的 update 操作明確的返回受影響的記錄條數(shù),有沒有什么辦法呢? 當(dāng)然是有的。 通過對(duì) JDBC URL 顯式的指定 useAffectedRows 選項(xiàng),我們將可以得到受影響的記錄的條數(shù):
我們對(duì)我們的數(shù)據(jù)庫連接配置稍做修改,添加 useAffectedRows 字段:
此時(shí),mybatis 的 update 操作返回的應(yīng)該是受影響的條數(shù)了,我們?cè)俅芜\(yùn)行單元測試試試看:
update 操作返回的是受影響的記錄條數(shù),我們知道為 0 和我們預(yù)期的 2 不一致,自然而然單元測試不通過。
以上這篇解決Mybatis中mapper.xml文件update,delete及insert返回值問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
