mybatis高效率批量update


更新單條記錄

UPDATE course SET name = 'course1' WHERE id = 'id1';

更新多條記錄的同一個字段為同一個值

UPDATE course SET name = 'course1' WHERE id in ('id1', 'id2', 'id3);

更新多條記錄為多個字段為不同的值

比較普通的寫法,是通過循環,依次執行update語句。

Mybatis寫法如下:

<update id="updateBatch"  parameterType="java.util.List">  
    <foreach collection="list" item="item" index="index" open="" close="" separator=";">
        update course
        <set>
            name=${item.name}
        </set>
        where id = ${item.id}
    </foreach>      
</update>

一條記錄update一次,性能比較差,容易造成阻塞。

MySQL沒有提供直接的方法來實現批量更新,但可以使用case when語法來實現這個功能。

UPDATE course
    SET name = CASE id 
        WHEN 1 THEN 'name1'
        WHEN 2 THEN 'name2'
        WHEN 3 THEN 'name3'
    END, 
    title = CASE id 
        WHEN 1 THEN 'New Title 1'
        WHEN 2 THEN 'New Title 2'
        WHEN 3 THEN 'New Title 3'
    END
WHERE id IN (1,2,3)

這條sql的意思是,如果id為1,則name的值為name1,title的值為New Title1;依此類推。

在Mybatis中的配置則如下:

<update id="updateBatch" parameterType="list">
            update course
            <trim prefix="set" suffixOverrides=",">
             <trim prefix="peopleId =case" suffix="end,">
                 <foreach collection="list" item="i" index="index">
                         <if test="i.peopleId!=null">
                          when id=#{i.id} then #{i.peopleId}
                         </if>
                 </foreach>
              </trim>
              <trim prefix=" roadgridid =case" suffix="end,">
                 <foreach collection="list" item="i" index="index">
                         <if test="i.roadgridid!=null">
                          when id=#{i.id} then #{i.roadgridid}
                         </if>
                 </foreach>
              </trim>
              
              <trim prefix="type =case" suffix="end," >
                 <foreach collection="list" item="i" index="index">
                         <if test="i.type!=null">
                          when id=#{i.id} then #{i.type}
                         </if>
                 </foreach>
              </trim>
       <trim prefix="unitsid =case" suffix="end," >
                  <foreach collection="list" item="i" index="index">
                          <if test="i.unitsid!=null">
                           when id=#{i.id} then #{i.unitsid}
                          </if>
                  </foreach>
           </trim>
             </trim>
            where
            <foreach collection="list" separator="or" item="i" index="index" >
              id=#{i.id}
          </foreach>
</update>

 

1、controlller

@RequestMapping(value = "/updateUserLog", method = RequestMethod.POST)
public BWJsonResult updateUserLog(@RequestBody List<UserLog> userLogList) {
    if (CollectionUtils.isEmpty(userLogList)) {
        return BWJsonResultUtil.buildErrBWJsonResult(UserServiceConstants.CODE_80110002, UserServiceConstants.CODE_MSG_80110002);
    }
    try {
        userService.updateUserLog(userLogList);
        return BWJsonResultUtil.buildSuccBWJsonResult("操作成功");
    } catch (Exception e) {
        logger.error("UserController findUserByUserArea error {}", e);
        return BWJsonResultUtil.buildErrBWJsonResult(UserServiceConstants.CODE_80110001, UserServiceConstants.CODE_MSG_80110001);
    }
}
2、service
void updateUserLog(List<UserLog> userLogList);
3、serviceImpl
public void updateUserLog(List<UserLog> userLogList) {

    //修改用戶日志表信息
    userLogMapper.updateUserLog(userLogList);
}
4、dao
void updateUserLog(List<UserLog> userLogList);
5、mapper
<update id="updateUserLog" parameterType="java.util.List">
    update ps_user_log
    SET dept_id =
    <foreach collection="list" item="item" index="index"
             separator=" " open="case id" close="end">
        when #{item.id} then #{item.deptId}
    </foreach>
    , type =
    <foreach collection="list" item="item" index="index"
             separator=" " open="case id" close="end">
        when #{item.id} then #{item.type}
    </foreach>
    where id in (
    <foreach collection="list" item="item" index="index"
             separator=",">
        #{item.id}
    </foreach>
    )
</update>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM