mysql 批量添加和批量修改

 

以该表为例

批量添加语句:

<!--批量插入-->

  <insert id="SimpleInsert" parameterType="java.util.List">

    INSERT INTO power_role(

    role_id,

    role_name,

    effective,

    create_date,

    creater,

    update_date,

    updater,

    remark

    )

    VALUES

    <foreach collection ="list" item="item" index= "index" separator =",">

      (

      #{item.roleId,jdbcType=INTEGER},

      #{item.roleName,jdbcType=VARCHAR},

      #{item.effective,jdbcType=INTEGER},

      #{item.createDate,jdbcType=CHAR},

      #{item.creater,jdbcType=VARCHAR},

      #{item.updateDate,jdbcType=CHAR},

      #{item.updater,jdbcType=VARCHAR},

      #{item.remark,jdbcType=VARCHAR}

      )

    </foreach >

  </insert>

 

批量修改:

<update id="updatePath" parameterType="java.util.List">

    UPDATE power_role

    SET

    role_name = CASE role_id

    <foreach collection="list" item="item" index="index" open="" separator="" close="">

      <choose>

        <when test="item.roleName != null and item.roleName != ''">

          WHEN #{item.roleId} THEN #{item.roleName}

        </when>

        <otherwise>

          WHEN #{item.roleId} THEN role_name

        </otherwise>

      </choose>

    </foreach>

    END,

    remark = CASE role_id

    <foreach collection="list" item="item" index="index" open="" separator="" close="">

      <choose>

        <when test="item.remark != null and item.remark != ''">

          WHEN #{item.roleId} THEN #{item.remark}

        </when>

        <otherwise>

          WHEN #{item.roleId} THEN remark

        </otherwise>

      </choose>

    </foreach>

    END,

    effective = CASE role_id

    <foreach collection="list" item="item" index="index" open="" separator="" close="">

      <choose>

        <when test="item.effective != null">

          WHEN #{item.roleId} THEN #{item.effective }

        </when>

        <otherwise>

          WHEN #{item.roleId} THEN effective  

        </otherwise>

      </choose>

    </foreach>

    END,

   create_date= CASE role_id

    <foreach collection="list" item="item" index="index" open="" separator="" close="">

      <choose>

        <when test="item.createDate!= null and item.createDate!=''">

          WHEN #{item.roleId} THEN #{item.createDate}

        </when>

        <otherwise>

          WHEN #{item.roleId} THEN create_date

        </otherwise>

      </choose>

    </foreach>

    END,

   creater= CASE role_id

    <foreach collection="list" item="item" index="index" open="" separator="" close="">

      <choose>

        <when test="item.creater!= null and item.creater!=''">

          WHEN #{item.roleId} THEN #{item.creater}

        </when>

        <otherwise>

          WHEN #{item.roleId} THEN creater

        </otherwise>

      </choose>

    </foreach>

    END,

   update_date= CASE role_id

    <foreach collection="list" item="item" index="index" open="" separator="" close="">

      <choose>

        <when test="item.updateDate!= null and item.updateDate!= ''">

          WHEN #{item.roleId} THEN #{item.updateDate}

        </when>

        <otherwise>

          WHEN #{item.roleId} THEN update_date  

        </otherwise>

      </choose>

    </foreach>

    END,

    udpater= CASE role_id

    <foreach collection="list" item="item" index="index" open="" separator="" close="">

      <choose>

        <when test="item.udpater!= null and item.udpater!=''">

          WHEN #{item.roleId} THEN #{item.udpater}

        </when>

        <otherwise>

          WHEN #{item.roleId} THEN udpater

        </otherwise>

      </choose>

    </foreach>

    END,

    WHERE

    role_id in

    <foreach collection="list" item="item" index="index" open="(" separator="," close=")">

      #{item.roleId}

    </foreach>

  </update>

 

 

相关文章: