温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在mybatis中使用update对字段进行更新

发布时间:2021-01-25 16:05:37 来源:亿速云 阅读:2037 作者:Leah 栏目:开发技术

怎么在mybatis中使用update对字段进行更新?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

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

特点是一个mapper方法包含所有字段,不为空的就update。

但是需要控制入参,一般有2中方式:

new 一个对象然后set id和要改的字段

如果字段多比较费劲,需要一个一个set。

查询出对象,然后set要改的字段

这2种方式差不多,就是代码看起来不一样。

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

要更新的字段加if没有什么问题

但是定位条件不要加if,因为万一忘记传递了,变成没有where条件,那么条数不可控了。搞不好把全表更新了,可就万劫不复了。

补充:mybatis执行批量更新update

目前想批量更新,如果update的值是相同的话,很简单,

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>

但是这样的需求很少,一般是有个集合,每个元素中的值是不一样的,然后需要一次性更新。一般的处理方式是使用for循环。这样的效率较低,当数据量大时,期望有种一次性插入的操作。如果使用的是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)

两种方式可以处理。

当前数据库是oracle,可以使用case when来拼成一长串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)

实际上这种方式对于mysql也有效。

最开始的时候,想着写一系列并列的更新语句就可以了

<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>

这样直接报错,因为Mybatis映射文件中的sql语句不允许 ; 符号。

两种方法:

第一种:需要在db链接url后面带一个参数 &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>

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

项目中实际使用案例:

<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>

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI