mybatis update更新字段的使用操作

多个mapper方法,更新单字段

说实话不太推荐,因为如果有10个字段要更新,难道写10个方法。

但是实际中很多人都这么写。

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。如有错误或未考虑完全的地方,望不吝赐教。

(0)

相关推荐

  • Mybatis中使用updateBatch进行批量更新

    背景描述:通常如果需要一次更新多条数据有两个方式,(1)在业务代码中循环遍历逐条更新.(2)一次性更新所有数据(更准确的说是一条sql语句来更新所有数据,逐条更新的操作放到数据库端,在业务代码端展现的就是一次性更新所有数据).两种方式各有利弊,下面将会对两种方式的利弊做简要分析,主要介绍第二种方式在mybatis中的实现. 逐条更新 这种方式显然是最简单,也最不容易出错的,即便出错也只是影响到当条出错的数据,而且可以对每条数据都比较可控,更新失败或成功,从什么内容更新到什么内容,都可以在逻辑代码

  • mybatis执行批量更新batch update 的方法(oracle,mysql两种)

    Oracle和MySQL数据库的批量update在mybatis中配置不太一样: oracle数据库: <code class="hljs tcl" style=""><<span class="hljs-keyword" style="">update</span> id=<span class="hljs-string" style=""

  • 详解MyBatis-Plus updateById方法更新不了空字符串/null解决方法

    最近遇到了Mybatis-Plus updateById(),更新某一个字段为null,却发现没有更新成功,发现有一个博客记录挺好的.转载过来,方便自己看. 一.简介 因为最近在忙项目,好久都没有更新博客,最近在项目中刚好遇到一个问题,就是在使用MyBatis-Plus updateById(xxx)的时候,居然更新不了字符串或者null,本文分享两种解决方案,具体大家可以根据自己的需求选择一种方法解决. 二.原理 在实际项目中,难免更新的时候,有可能会把已有的值更新成空字符串或者null,但是

  • 解决mybatis批量更新(update foreach)失败的问题

    如下所示: <!--批量更新报表 --> <update id="updateIssueByBatch" parameterType="java.util.List"> <foreach collection="issueList" item="item" index="index" separator=";"> update sys_issue &l

  • mybatis update更新字段的使用操作

    多个mapper方法,更新单字段 说实话不太推荐,因为如果有10个字段要更新,难道写10个方法. 但是实际中很多人都这么写. 通用mapper方法,java代码控制字段 特点是一个mapper方法包含所有字段,不为空的就update. 但是需要控制入参,一般有2中方式: new 一个对象然后set id和要改的字段 如果字段多比较费劲,需要一个一个set. 查询出对象,然后set要改的字段 这2种方式差不多,就是代码看起来不一样. 特别注意,定位字段不要加if 要更新的字段加if没有什么问题 但

  • mybatis plus更新字段为null处理方法

    目录 测试代码如下 数据库表 实体类 mapper controller 运行测试 运行测试 mybatis plus 将字段更新为 null,默认如果不做任何处理,使用 mybatis plus 自带的更新方法是不能将字段更新为 null 的,如果要将字段更新为 null,需要做下面的处理 在需要的字段上加 @TableField 注解,并设置属性 updateStrategy = FieldStrategy.IGNORED 测试代码如下 数据库表 实体类 package com.sbmp.b

  • Mybatis-Plus使用updateById()、update()将字段更新为null

    目录 问题背景 问题原因 解决方案 问题背景 昨晚同事找我帮他看一个问题,他使用mybatis-plus中提供的updateById方法,想将查询结果中某个字段原本不为null的值更新为null(数据库设计允许为null),但结果该字段更新失败,执行更新方法后还是查询的结果. 问题原因 mybatis-plus FieldStrategy 有三种策略: IGNORED:0 忽略 NOT_NULL:1 非 NULL,默认策略 NOT_EMPTY:2 非空 而默认更新策略是NOT_NULL:非 NU

  • Mybatis的update更新批量与普通解决方式对比

    目录 需求前提: 1.第一种:应该是效率最低的更新 2.通过批量更新 xml改造 注意事项:使用set导致逗号出现的问题 < trim>节点标签解读: 需求前提: 通过其他库里面查询出一条数据,并且对另外一个库中的oederId进行更新里面的内容注意:使用批量update时连接数据库的语句需要添加allowMultiQueries=true jdbc:mysql://127.0.0.1:3306/tb?useUnicode=true&characterEncoding=utf-8&am

  • 解决mybatis update并非所有字段需要更新问题

    目录 mybatis update并非所有字段需要更新 解决办法 mybatis update时遇到的问题 mybatis update并非所有字段需要更新 mybatis update 需求:更新字段作为参数,未更新字段不传入 解决办法 <update id="updateUser" parameterType="com.test.entity.User"> update BS_USER <trim prefix="set"

  • Mybatis Update操作返回值问题

    后端的数据持久化使用的是 Mybatis ,在做高并发下账户增减余额的时候,打算使用乐观锁来解决这个问题.在获取update操作的返回值时遇到了一个问题,似乎 Mybatis 进行 update 操作得到的 int 返回值并不是影响的行数.这下就尴尬了. 一般而言,我们知道当我们使用 Mybatis 在 mapper 接口中定义 insert delete 等操作,定义一个 int 类型的返回值,通过该值是否为 0 来判断数据库中受影响的行数进而判断操作是否成功. 到底 update 返回值代表

  • mybatis-plus update更新操作的三种方式(小结)

    目录 1.@ 根据id更新 2.@ 条件构造器作为参数进行更新 3.@ lambda构造器 mybatisplus update语句为null时没有拼接上去 1.@ 根据id更新 User user = new User(); user.setUserId(1); user.setAge(29); userMapper.updateById(user); 2.@ 条件构造器作为参数进行更新 //把名字为rhb的用户年龄更新为18,其他属性不变 UpdateWrapper<User> updat

  • mybatis 获取更新(update)记录的id之<selectKey>用法说明

    目录 获取更新(update)记录的id之<selectKey> 问题 简介 解决 获取update 纪录的id 详解 <selectKey>标签的含义 获取更新(update)记录的id之<selectKey> 问题 用mybatis update 记录,更新过后想要更新记录的id 怎么办? 平常我门都是更新数据,用更新的条件再查询一次,得到更新的记录.这样我门就进行了两次数据库操作,链接了两次数据库.增加了接口的处理事件,因为链接数据库是很耗时的操作. 简介 其实可

  • Mybatis批量更新报错问题

    下面给大家介绍mybatis批量更新报错问题, allowMultiQueries=true 后来发现是jdbc链接没有加允许批量更新操作的参数引起的,不加会报badsql,mysql版的mybatis批量更新操作如下 <update id="updateOrderOverdueStatus" parameterType="java.util.List"> <foreach collection="list" item=&quo

随机推荐