码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis批量操作

时间:2018-01-26 10:57:26      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:解析   integer   mybatis   validate   port   post   entity   rod   保存   

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批量操作

标签:解析   integer   mybatis   validate   port   post   entity   rod   保存   

原文地址:https://www.cnblogs.com/super-chao/p/8358139.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!