Sql Mybatis 有則更新無則插入(批量)

環(huán)境及版本:

  • 系統(tǒng):Mysql \ Oracle

一 基礎表數(shù)據(jù)

CREATE TABLE `sys_user` (
  `id` varchar(100) primary key,
  `username` varchar(100) not null,
  `password` varchar(100) not null,
  `status` int not null
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

二 Mysql

1 批量插入

mapper 接口

/**
 * 批量新增數(shù)據(jù)(MyBatis原生foreach方法)
 *
 * @param entities List<SysUser> 實例對象列表
 * @return 影響行數(shù)
 */
int insertBatch(@Param("entities") List<SysUser> entities);

mapper xml

<insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
    insert into sys_user(id, username, password, status)
    values
    <foreach collection="entities" item="entity" separator=",">
        (#{entity.id}, #{entity.username}, #{entity.password}, #{entity.status})
    </foreach>
</insert>

2 有則更新 無則插入 單條

mapper 接口

/**
 * 新增或按主鍵更新數(shù)據(jù)(MyBatis原生foreach方法)
 *
 * @param entities List<SysUser> 實例對象列表
 * @return 影響行數(shù)
 */
int insertOrUpdate(@Param("sysuser") SysUser sysuser);

mapper xml

<insert id="insertOrUpdate" keyProperty="id" useGeneratedKeys="true">
    insert into 
    sys_user(id, username, password, status)
    values
    (#{sysuser.id}, #{sysuser.username}, #{sysuser.password}, #{sysuser.status})
    on duplicate key update
    id = values(id) , username = values(username) , password = values(password) , status = values(status)
</insert>

3 有則更新 無則插入 批量

mapper 接口

/**
 * 批量新增或按主鍵更新數(shù)據(jù)(MyBatis原生foreach方法)
 *
 * @param entities List<SysUser> 實例對象列表
 * @return 影響行數(shù)
 */
int insertOrUpdateBatch(@Param("entities") List<SysUser> entities);

mapper xml

<insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
    insert into sys_user(id, username, password, status)
    values
    <foreach collection="entities" item="entity" separator=",">
        (#{entity.id}, #{entity.username}, #{entity.password}, #{entity.status})
    </foreach>
    on duplicate key update
    id = values(id) , username = values(username) , password = values(password) , status = values(status)
</insert>

三 Oracle

1 批量插入

mapper 接口

int insertBatch(List<SysUser> list);

mapper xml

<insert id="insertBatch" parameterType="java.util.List" >
  insert into sys_user(id, username, password, status)
  <!--oracle 沒有values,只能這種-->
  select A.* from(
    <foreach collection="list" item="item" index="index" separator="union all">
      select
         #{item.id} id,
        #{item.username} username,
        #{item.password} password,
        #{item.status} status
      from dual
    </foreach>
  )A
</insert>

2 有則更新 無則插入 單條

mapper 接口

int insertOrUpdateBatch(SysUser sysuser);

mapper xml

<!--批量插入或更新-->
<update id="insertOrUpdateBatch" parameterType="java.util.List" >
  merge into sys_user t1
  using (
    select
      #{sysuser.id} id,
      #{sysuser.username} username,
      #{sysuser.password} password,
      #{sysuser.status} status
    from dual
  ) t2
  on (t1.id=t2.id)
  when matched then
    update
    <set>
      <if test="username!=null">
        t1.username=#{sysuser.username},
      </if>
      <if test="password!=null">
        t1.password=#{sysuser.password},
      </if>
      <if test="status!=null">
        <!--直接使用t2的值,待驗證-->
        t1.status=t2.status,
      </if>
    </set>
  when not matched then
    insert (id, username, password, status)
    values (t2.id, t2.username, t2.password,t2.status)
</update>

3 有則更新 無則插入 批量

mapper 接口

int insertOrUpdateBatch(List<DefaultEnterprise> list);

mapper xml

<!--批量插入或更新-->
<update id="insertOrUpdateBatch" parameterType="java.util.List" >
  merge into sys_user t1
  using (
  <foreach collection="list" item="item" index="index" separator="union all">
    select
      #{item.id} id,
      #{item.username} username,
      #{item.password} password,
      #{item.status} status
    from dual
  </foreach>
  ) t2
  on (t1.id=t2.id)
  when matched then
    update
    <set>
      <if test="item.username!=null">
        t1.username=t2.username,
      </if>
       <if test="item.password!=null">
        t1.password=t2.password,
      </if>
       <if test="item.status!=null">
        t1.status=t2.status,
      </if>
    </set>
  when not matched then
    insert (id, username, password, status)
    values (t2.id, t2.username, t2.password,t2.status)
</update>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容