MyBatis如何通過xml方式實(shí)現(xiàn)SaveOrUpdate
MyBatis實(shí)現(xiàn)SaveOrUpdate
這篇文章主要講如何通過xml方式實(shí)現(xiàn)SaveOrUpdate,但是仍然建議在Service中實(shí)現(xiàn)。
例子
<insert id="saveOrUpdate" >
<selectKey keyProperty="count" resultType="int" order="BEFORE">
select count(*) from country where id = #{id}
</selectKey>
<if test="count > 0">
update country
set countryname = #{countryname},countrycode = #{countrycode}
where id = #{id}
</if>
<if test="count==0">
insert into country values(#{id},#{countryname},#{countrycode})
</if>
</insert>條件限制
根據(jù)不同的判斷邏輯,會有所不同,就上面這個(gè)例子而言,就要求實(shí)體類中包含count屬性(可以是別的名字)。否則selectKey的結(jié)果沒法保存,如果入?yún)⑹莻€(gè)Map類型,就沒有這個(gè)限制。
說明
從例子來看除了有個(gè)限制外,也沒別的麻煩。
通過selectKey做第一次查詢,然后根據(jù)結(jié)果進(jìn)行判斷,所以這里的order="BEFORE"是必須的。
也是因?yàn)?code>BEFORE,所以沒法通過<bind>標(biāo)簽來臨時(shí)存儲中間的值,只能在入?yún)⒅性黾訉傩詠泶娣拧?/p>
測試代碼
//數(shù)據(jù)庫中已經(jīng)存在該ID,但是countryname=China
Country country = new Country();
country.setId(35);
country.setCountryname("中國");
country.setCountrycode("CN");
//由于存在,這里會update
int result = countryMapper.saveOrUpdate(country);
//查詢結(jié)果,判斷是否已經(jīng)改變
Country c2 = countryMapper.selectById(35);
assertEquals("中國",c2.getCountryname());
//id=300的不存在
c2 = countryMapper.selectById(300);
assertNull(c2);
//將id=300
country.setId(300);
//由于id=300不存在,這里會Insert
result = countryMapper.saveOrUpdate(country);
//查詢結(jié)果
c2 = countryMapper.selectById(300);
assertNotNull(c2);輸出日志
DEBUG ==> Preparing: select count(*) from country where id = ?
DEBUG ==> Parameters: 35(Integer)
TRACE <== Columns: C1
TRACE <== Row: 1
DEBUG <== Total: 1
DEBUG ==> Preparing: update country set countryname = ?,countrycode = ? where id = ?
DEBUG ==> Parameters: 中國(String), CN(String), 35(Integer)
DEBUG <== Updates: 1
DEBUG ==> Preparing: select * from country where id = ?
DEBUG ==> Parameters: 35(Integer)
TRACE <== Columns: ID, COUNTRYNAME, COUNTRYCODE
TRACE <== Row: 35, 中國, CN
DEBUG <== Total: 1
DEBUG ==> Preparing: select * from country where id = ?
DEBUG ==> Parameters: 300(Integer)
DEBUG <== Total: 0
DEBUG ==> Preparing: select count(*) from country where id = ?
DEBUG ==> Parameters: 300(Integer)
TRACE <== Columns: C1
TRACE <== Row: 0
DEBUG <== Total: 1
DEBUG ==> Preparing: insert into country values(?,?,?)
DEBUG ==> Parameters: 300(Integer), 中國(String), CN(String)
DEBUG <== Updates: 1
DEBUG ==> Preparing: select * from country where id = ?
DEBUG ==> Parameters: 300(Integer)
TRACE <== Columns: ID, COUNTRYNAME, COUNTRYCODE
TRACE <== Row: 300, 中國, CN
DEBUG <== Total: 1
最后
這種方式只是利用了selectKey會多執(zhí)行一次查詢來實(shí)現(xiàn)的,但是如果你同時(shí)還需要通過selectKey獲取序列或者自增的id,就會麻煩很多(oracle麻煩,其他支持自增的還是很容易)。
建議在復(fù)雜情況下,還是選擇在Service中實(shí)現(xiàn)更好。
到此這篇關(guān)于MyBatis如何通過xml方式實(shí)現(xiàn)SaveOrUpdate的文章就介紹到這了,更多相關(guān)MyBatis實(shí)現(xiàn)SaveOrUpdate內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java web數(shù)據(jù)可視化實(shí)現(xiàn)原理解析
這篇文章主要介紹了Java web數(shù)據(jù)可視化實(shí)現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Java多線程編程中使用Condition類操作鎖的方法詳解
Condition是java.util.concurrent.locks包下的類,提供了對線程鎖的更精細(xì)的控制方法,下面我們就來看一下Java多線程編程中使用Condition類操作鎖的方法詳解2016-07-07
Spring Boot Redis客戶端遠(yuǎn)程操作實(shí)現(xiàn)過程解析
這篇文章主要介紹了Spring Boot Redis客戶端遠(yuǎn)程操作實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
springboot中縮短一個(gè)url鏈接的實(shí)現(xiàn)
縮短 URL 是現(xiàn)代應(yīng)用程序中常見的需求,通常用于減少長 URL 的長度,使其更易于分享,URL 縮短服務(wù)的核心思路是將長 URL 映射到一個(gè)唯一的短代碼,本文主要介紹了springboot中縮短一個(gè)url鏈接的實(shí)現(xiàn),感興趣的可以了解一下2024-09-09

