mybatis批量操作

Posted super超人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis批量操作相关的知识,希望对你有一定的参考价值。

mybatis批量操作:

批量保存:

<insert id="batchInsertBeneficiary" parameterType="com.api.params.BeneficiaryParam" >
    insert into ins_beneficiary
    <trim prefix="(" suffix=")" suffixOverrides="," >
      id, reservation_id, channel_id, last_name, first_name, name, sex, identity_type,
      identity_no, identity_validate_date, benefit_order, benefit_proportion, relation,
      create_id, create_name, update_id, update_name
    </trim>
    VALUES
    <foreach collection="beneficiaryParamList" item="item" index="index" separator="," >
      <trim prefix="(" suffix=")" suffixOverrides="," >
        #{item.id,jdbcType=BIGINT}, #{item.reservationId,jdbcType=VARCHAR}, #{item.channelId,jdbcType=BIGINT},
        #{item.lastName,jdbcType=VARCHAR}, #{item.firstName,jdbcType=VARCHAR}, #{item.name,jdbcType=VARCHAR},
        #{item.sex,jdbcType=TINYINT}, #{item.identityType,jdbcType=TINYINT}, #{item.identityNo,jdbcType=VARCHAR},
        #{item.identityValidateDate,jdbcType=DATE}, #{item.benefitOrder,jdbcType=INTEGER}, #{item.benefitProportion,jdbcType=DECIMAL},
        #{item.relation,jdbcType=TINYINT}, #{item.createId,jdbcType=VARCHAR},
        #{item.createName,jdbcType=VARCHAR}, #{item.updateId,jdbcType=VARCHAR},
        #{item.updateName,jdbcType=VARCHAR}
      </trim>
    </foreach>
  </insert>

批量保存解析出来的语句格式为:

insert into 表名 (字段1,字段2) values (value1,value2)(value3,value4);

批量更新:

<update id="batchUpdateBeneficiary"  parameterType="com.api.params.BeneficiaryParam">
    update ins_beneficiary
    <trim prefix="set" suffixOverrides=",">
      <trim prefix="last_name =case" suffix="end,">
        <foreach collection="beneficiaryParamList" item="item" index="index">
          when id=#{item.id} then #{item.lastName}
        </foreach>
      </trim>

      <trim prefix="first_name =case" suffix="end,">
        <foreach collection="beneficiaryParamList" item="item" index="index">
          when id=#{item.id} then #{item.firstName}
        </foreach>
      </trim>

      <trim prefix="name =case" suffix="end,">
        <foreach collection="beneficiaryParamList" item="item" index="index">
          when id=#{item.id} then #{item.name}
        </foreach>
      </trim>

      <trim prefix="sex =case" suffix="end,">
        <foreach collection="beneficiaryParamList" item="item" index="index">
          when id=#{item.id} then #{item.sex}
        </foreach>
      </trim>

      <trim prefix="identity_type =case" suffix="end,">
        <foreach collection="beneficiaryParamList" item="item" index="index">
          when id=#{item.id} then #{item.identityType}
        </foreach>
      </trim>

      <trim prefix="identity_no =case" suffix="end,">
        <foreach collection="beneficiaryParamList" item="item" index="index">
          when id=#{item.id} then #{item.identityNo}
        </foreach>
      </trim>

      <trim prefix="identity_validate_date =case" suffix="end,">
        <foreach collection="beneficiaryParamList" item="item" index="index">
          when id=#{item.id} then #{item.identityValidateDate}
        </foreach>
      </trim>

      <trim prefix="relation =case" suffix="end,">
        <foreach collection="beneficiaryParamList" item="item" index="index">
          when id=#{item.id} then #{item.relation}
        </foreach>
      </trim>

      <trim prefix="benefit_order =case" suffix="end,">
        <foreach collection="beneficiaryParamList" item="item" index="index">
          when id=#{item.id} then #{item.benefitOrder}
        </foreach>
      </trim>

      <trim prefix="benefit_proportion =case" suffix="end,">
        <foreach collection="beneficiaryParamList" item="item" index="index">
          when id=#{item.id} then #{item.benefitProportion}
        </foreach>
      </trim>

      <trim prefix="update_id =case" suffix="end,">
        <foreach collection="beneficiaryParamList" item="item" index="index">
          when id=#{item.id} then #{item.updateId}
        </foreach>
      </trim>

      <trim prefix="update_name =case" suffix="end,">
        <foreach collection="beneficiaryParamList" item="item" index="index">
          when id=#{item.id} then #{item.updateName}
        </foreach>
      </trim>
    </trim>
    <where>
      <foreach collection="beneficiaryParamList" separator="or" item="item" index="index" >
        <trim prefix="(" suffix=")">
          <if test="item.id!=null and item.id!=‘‘">
            id = #{item.id}
          </if>
        </trim>
      </foreach>
    </where>
  </update>

批量更新解析出来的语句格式为:

UPDATE ins_reservation_product_relation
SET policy_amount = CASE
WHEN insurance_code =?
AND reservation_id =? THEN
    ?
WHEN insurance_code =?
AND reservation_id =? THEN
    ?
END,
 policy_charge = CASE
WHEN insurance_code =?
AND reservation_id =? THEN
    ?
WHEN insurance_code =?
AND reservation_id =? THEN
    ?
END
WHERE
    1 = 1
AND is_delete = 0 AND (
    reservation_id = ?
    AND insurance_code = ?
)
OR (
    reservation_id = ?
    AND insurance_code = ?
)

 

以上是关于mybatis批量操作的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis实现批量添加操作

MyBatis批量新增和更新

mybatis的批量删除操作

mybatis批量更新

mybatis 怎么批量更新操作

mybatis 批量操作数据