溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

mybatis怎么批量修改數據

發布時間:2022-03-14 13:32:52 來源:億速云 閱讀:1056 作者:小新 欄目:開發技術

這篇文章給大家分享的是有關mybatis怎么批量修改數據的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

批量修改主要有兩種方式

第一種

可以通過for循環一條一條修改數據,這樣會影響效率,因此我不推薦,所以在這里我也不多說。

第二種

通過修改mybatis中mapper.xml文件,如下:

<update id="updateRoleMenus" parameterType="java.util.List"> 
      <foreach collection="list" item="item" index="index" open="" close="" separator=";"> 
       update TB_ROLE_MENU  
          <set> 
          FID=#{item.fid}
          </set> 
          where ROLEID = #{item.roleid} 
      </foreach> 
    </update>

mysql及mybatis批量更新數據update

mysql批量更新update

使用case when語句,數據表如下:

mybatis怎么批量修改數據

case1:

其中age為非空字段。sql如下

update test1
set age=case
when id=2 then 1
when id =3 then 2
end
where id in (2,3,4)

對id為2,3,4的設置age字段,id為2的設置為1,3的設置為2,結果為:

Column &lsquo;age&rsquo; cannot be null.

原因是由于id=4沒有被上面的when匹配到,因此會被默認設為空null。即未被匹配到的記錄并不會保持原來的數值,而是會被設置為null。

case2:

如果想設默認值,可寫為:

update test1
set age=case
when id=2 then 1
when id =3 then 2
else 30
end
where id in (2,3,4)

結果是

mybatis怎么批量修改數據

可見id=4的設為了30,也就是通過else default_value可以對未被匹配到的行設置默認值。

case3:

如果想對未匹配到的行設置未原來的值,則可寫為:

update test1
set age=case
when id=2 then 1
when id =3 then 2
when id =4 then test1.age
end
where id in (2,3,4)

這樣就可以通過test1.age來使id=4的行保持原值。在mybatis中可看到這種寫法的用處。

mybatis實現批量更新update

對應上面的各種case,來寫xml。

通常在寫代碼的時候,有時候不更新的字段就不會設置到對象里,比如Person對象分別有id,name,age三個屬性對應數據庫的三個字段,如果不想更新id=4的age時,就通常不設置age的值,也就是age=null,這樣在case1里,就會被誤設為null,詳細見如下代碼。

case1:

update test1
<set>
	<trim prefix="age=case" suffix="end,">
		<foreach collection="list" item="item" index="index">
			<if test="item.age !=null">
				when id=#{item.id} then #{item.age}
			</if>
		</foreach>
	</trim>
	<trim prefix="name=case" suffix="end,">
		<foreach collection="list" item="item" index="index">
			when id=#{item.id} then #{item.name}
		</foreach>
	</trim>
</set>
where id in
<foreach collection="list" item="item" index="index" separator="," open="(" close=")">
	#{item.id}
</foreach>

當傳入的list中person對象有三個,分別對應id=2,3,4,若不想更新4的age,而只想更新4的姓名,則id=4的person對象age就會被設為null,這樣傳入該sql時,id=4的數據不會被when匹配,而又沒有設置默認值,則原數據會被刪除并設為null。

case2:

update test1
<set>
	<trim prefix="age=case" suffix="end,">
		<foreach collection="list" item="item" index="index">
			<if test="item.age !=null">
				when id=#{item.id} then #{item.age}
			</if>
		</foreach>
		else 30
	</trim>
	<trim prefix="name=case" suffix="end,">
		<foreach collection="list" item="item" index="index">
			when id=#{item.id} then #{item.name}
		</foreach>
	</trim>
</set>
where id in
<foreach collection="list" item="item" index="index" separator="," open="(" close=")">
	#{item.id}
</foreach>

該代碼由于設置了else 30,因此會id=4的數據在不設置age的情況下會被更新為30。

case3:

update test1
<set>
	<trim prefix="age=case" suffix="end,">
		<foreach collection="list" item="item" index="index">
			<if test="item.age !=null">
				when id=#{item.id} then #{item.age}
			</if>
			<if test="item.age ==null">
				when id=#{item.id} then test1.age
			</if>
		</foreach>
	</trim>
	<trim prefix="name=case" suffix="end,">
		<foreach collection="list" item="item" index="index">
			when id=#{item.id} then #{item.name}
		</foreach>
	</trim>
</set>
where id in
<foreach collection="list" item="item" index="index" separator="," open="(" close=")">
	#{item.id}
</foreach>

通過if標簽,若傳入的屬性為null,則保持數據庫原值。

補充:

更新多條數據同一字段為同一值:

UPDATE test1 SET age=24 WHERE id in(2,3,4);

感謝各位的閱讀!關于“mybatis怎么批量修改數據”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女