Mybatis语法(四)mysql插入或更新

现有user表,userId为用户id,做为数据表user的主键:

由于userId不可以重复,而这里userId直接作为主键。为防止并发操作,插入语句可以这样设计:不存在userId则插入,否则更新:

<!--修改或更新-->
 <insert id="insertOrUpdate" parameterType="com.iflytek.edu.zx.activitity.dto.ActivityUser">
 
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="userId != null">
        userId,
      </if>
      <if test="nickName != null">
        nickName,
      </if>
      <if test="role != null">
        role,
      </if>
      <if test="createTime != null">
        createTime,
      </if>
      <if test="updateTime != null">
        updateTime,
      </if>
      <if test="nickNameUpdateTime != null">
        nickNameUpdateTime,
      </if>
      <if test="nickNameCreateTime != null">
        nickNameCreateTime,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="userId != null">
        #{userId,jdbcType=VARCHAR},
      </if>
      <if test="nickName != null">
        #{nickName,jdbcType=VARCHAR},
      </if>
      <if test="role != null">
        #{role,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="nickNameUpdateTime != null">
        #{nickNameUpdateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="nickNameCreateTime != null">
        #{nickNameCreateTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
    ON DUPLICATE KEY UPDATE
    updateTime=#{updateTime,jdbcType=TIMESTAMP}
     <if test="nickName != null">
     	,nickName=#{nickName,jdbcType=VARCHAR}
     </if>
     <if test="role != null">
     	,role = #{role,jdbcType=VARCHAR}
     </if>
    
    <if test="nickNameUpdateTime != null">
     	,nickNameUpdateTime=#{nickNameUpdateTime,jdbcType=TIMESTAMP}
     </if>
    
     <if test="nickNameCreateTime != null">
        ,nickNameCreateTime = #{nickNameCreateTime,jdbcType=TIMESTAMP}
      </if>
 
 </insert>

on udplicate key update后的内容表示,主键存在时则执行更新操作,需要注意的是insert字段中需要含有唯一性字段(主键索引或唯一索引)。

再如:

 <insert id="insertOnDuplicate" parameterType="XXXX">
    insert into t_product (
        id,
        intention_code,
        add_type,
        product_id,
        sbom_code,
        sbom_name)
    values (#{id},#{intentionCode,jdbcType=VARCHAR}, #{addType,jdbcType=INTEGER},  #{productId,jdbcType=BIGINT}, #{sbomCode,jdbcType=VARCHAR}, #{sbomName,jdbcType=VARCHAR}})
      ON DUPLICATE KEY UPDATE
      product_id = VALUES(product_id),
        sbom_name = VALUES(sbom_name),
        price = VALUES(price))
  </insert>

多条记录仅仅是多了foreach 标签:

  
<insert id="insertByBatchOnDuplicate" parameterType="java.util.List">
        insert into t_xxx (
        id,
        intention_code,
        add_type,
        product_id,
        sbom_code,
        sbom_name
    )
        values
        <foreach collection="list" item="item" index="index" separator=",">
            ( #{item.id},#{item.intentionCode,jdbcType=VARCHAR},
            #{item.addType,jdbcType=INTEGER},
            #{item.productId,jdbcType=BIGINT},
            #{item.sbomCode,jdbcType=VARCHAR},
            #{item.sbomName,jdbcType=VARCHAR}
         )
        </foreach>
        ON DUPLICATE KEY UPDATE
        product_id = VALUES(product_id),
        sbom_name = VALUES(sbom_name)
    </insert>

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w_t_y_y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值