MyBatis批量修改

批量修改-xml代码

mybatis批量查询,批量新增就不聊了,今天看看批量修改。
直接上代码吧
xml文件中代码如下:


<update id="updateBatchById" parameterType="java.util.List">
    update
        employee
    set
        userName =
        <foreach collection="list" item="item" index="index" separator=" " open="case" close="end">
            when id = #{item.id} then #{item.userName}
        </foreach>
    , age =
        <foreach collection="list" item="item" index="index" separator=" " open="case" close="end">
            when id = #{item.id} then #{item.age}
        </foreach>
    where
        ID
    in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            #{item.id}
        </foreach>
</update>

运行出来的代码如下

    update
        employee
    set 
        userName =
    case
        when id = ? then ?
        when id = ? then ?
    end
        , age =
    case
        when id = ? then ?
        when id = ? then ?
    end
    where
        ID
    in
        (?,?)

mapper层代码


    /**
     *批量修改员工信息
     * @param employees
     */
    public Integer updateBatchById(List<Employee> employees);

posted @ 2019-11-05 16:06  橙一万  阅读(1624)  评论(0编辑  收藏  举报