溫馨提示×

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

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

怎么在mybatis中使用update對(duì)字段進(jìn)行更新

發(fā)布時(shí)間:2021-01-25 16:05:37 來(lái)源:億速云 閱讀:2057 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

怎么在mybatis中使用update對(duì)字段進(jìn)行更新?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

通用mapper方法,java代碼控制字段

特點(diǎn)是一個(gè)mapper方法包含所有字段,不為空的就update。

但是需要控制入?yún)?,一般?中方式:

new 一個(gè)對(duì)象然后set id和要改的字段

如果字段多比較費(fèi)勁,需要一個(gè)一個(gè)set。

查詢出對(duì)象,然后set要改的字段

這2種方式差不多,就是代碼看起來(lái)不一樣。

特別注意,定位字段不要加if

要更新的字段加if沒(méi)有什么問(wèn)題

但是定位條件不要加if,因?yàn)槿f(wàn)一忘記傳遞了,變成沒(méi)有where條件,那么條數(shù)不可控了。搞不好把全表更新了,可就萬(wàn)劫不復(fù)了。

補(bǔ)充:mybatis執(zhí)行批量更新update

目前想批量更新,如果update的值是相同的話,很簡(jiǎn)單,

update table set column='...' where id in (1,2,3)l

這樣的sql就可以了。Mybatis中這樣寫就行

<update id="batchUpdateStudentWithMap" parameterType="java.util.Map" >
 UPDATE STUDENT SET name = #{name} WHERE id IN
 <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
  #{item}
 </foreach>
</update>

但是這樣的需求很少,一般是有個(gè)集合,每個(gè)元素中的值是不一樣的,然后需要一次性更新。一般的處理方式是使用for循環(huán)。這樣的效率較低,當(dāng)數(shù)據(jù)量大時(shí),期望有種一次性插入的操作。如果使用的是mysql,有

insert into table (aa,bb,cc) values(xx,xx,xx),(oo,oo,oo) on duplicate key update

replace into table (aa,bb,cc) values(xxx,xxx,xxx),(ooo,ooo,ooo),(ccc,ccc,ccc)

兩種方式可以處理。

當(dāng)前數(shù)據(jù)庫(kù)是oracle,可以使用case when來(lái)拼成一長(zhǎng)串sql處理

UPDATE mytable
 SET myfield = CASE id
  WHEN 1 THEN 'value'
  WHEN 2 THEN 'value'
  WHEN 3 THEN 'value'
 END
WHERE id IN (1,2,3)

實(shí)際上這種方式對(duì)于mysql也有效。

最開(kāi)始的時(shí)候,想著寫一系列并列的更新語(yǔ)句就可以了

<update id="updateBatch" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" separator=";"
 open="" close="">
 update REGION_CODE set
 CODE=#{item.Code,jdbcType=VARCHAR},
 NAME=#{item.Name,jdbcType=VARCHAR}
 where ID = #{item.id,jdbcType=DECIMAL}
</foreach>
</update>

這樣直接報(bào)錯(cuò),因?yàn)镸ybatis映射文件中的sql語(yǔ)句不允許 ; 符號(hào)。

兩種方法:

第一種:需要在db鏈接url后面帶一個(gè)參數(shù) &allowMultiQueries=true

即:

jdbc:mysql://localhost:3306/mysqlTest?characterEncoding=utf-8&allowMultiQueries=true

第二種:按照可行的case when處理方式,Mybatis映射文件書寫方式如下:

<update id="updateBatch" parameterType="java.util.List">
 update REGION_CODE set
 CODE=
 <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
  when #{item.id,jdbcType=DECIMAL} then #{item.Code,jdbcType=VARCHAR}
 </foreach>
 ,NAME=
 <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
  when #{item.id,jdbcType=DECIMAL} then #{item.Name,jdbcType=VARCHAR}
 </foreach>
 where ID in
 <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
  #{item.id,jdbcType=DECIMAL}
 </foreach>
</update>

至此,批量更新功能完成。

項(xiàng)目中實(shí)際使用案例:

<update id="updateForBatch" parameterType="java.util.List"> 
  update user_credit_black_list set 
  type= 
  <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end"> 
  when #{item.id,jdbcType=BIGINT} then #{item.type,jdbcType=VARCHAR} 
  </foreach> 
  ,user_id= 
  <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end"> 
  when #{item.id,jdbcType=BIGINT} then #{item.userId,jdbcType=BIGINT} 
  </foreach> 
  ,update_time= 
  <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end"> 
  when #{item.id,jdbcType=BIGINT} then #{item.updateTime,jdbcType=TIMESTAMP} 
  </foreach> 
  ,delete_flg= 
  <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end"> 
  when #{item.id,jdbcType=BIGINT} then #{item.deleteFlg,jdbcType=BIT} 
  </foreach> 
  ,update_code= 
  <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end"> 
  when #{item.id,jdbcType=BIGINT} then #{item.updateCode,jdbcType=BIGINT} 
  </foreach> 
  where ID in 
  <foreach collection="list" index="index" item="item" separator="," open="(" close=")"> 
  #{item.id,jdbcType=BIGINT} 
  </foreach> 
 </update>

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

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

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

AI