Mybatis批量修改联合主键数据

最近遇上需要批量修改有联合主键的表数据,网上找了很多文章,最终都没找到比较合适的方法,有些只能支持少量数据批量修改,超过十几条就不行了。

最终自己摸索总结了两种方式可以批量修改数据。

第一种:

复制代码
 <update id="updateMoreEmpOrg" parameterType="java.util.List">
        update hr_emp_org
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="ISMAN = CASE EMPID" suffix="end,">
                <foreach collection="empOrgList" item="item" index="index">
                    <if test="item.isman != null">
                        when EMPID = #{item.empid} then #{item.isman}
                    </if>
                </foreach>
            </trim>
            <trim prefix="UPDATETIME = CASE EMPID" suffix="end,">
                <foreach collection="empOrgList" item="item" index="index">
                    <if test="item.updatetime != null">
                        when EMPID = #{item.empid} then #{item.updatetime}
                    </if>
                </foreach>
            </trim>

            <trim prefix="hr_status =case EMPID" suffix="end,">
                <foreach collection="empOrgList" item="item" index="index">
                    <if test="item.hrStatus != null">
                        when #{item.EMPID} then #{item.hrStatus}
                    </if>
                </foreach>
            </trim>
        </trim>
        where
        EMPID in
        <foreach collection="empOrgList" item="item" open="(" separator="," close=")">
            #{item.empid}
        </foreach>
        and ORGID in
        <foreach collection="empOrgList" item="item" open="(" separator="," close=")">
            #{item.orgid}
        </foreach>
    </update>
复制代码

直接结果集来两个in查询,最终可以满足。

第二种:

复制代码
 <update id="updateMoreEmpPosition" parameterType="java.util.List">
        update hr_emp_position
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="ISMAN =case" suffix="end,">
                <foreach collection="empPositionList" item="item" index="index">
                    <if test="item.isman != null">
                        when EMPID = #{item.empid} and POSITIONID = #{item.positionid} then #{item.isman}
                    </if>
                </foreach>
            </trim>
            <trim prefix="CREATETIME =case" suffix="end,">
                <foreach collection="empPositionList" item="item" index="index">
                    <if test="item.createtime != null">
                        when EMPID = #{item.empid} and POSITIONID = #{item.positionid} then #{item.createtime}
                    </if>
                </foreach>
            </trim>
            <trim prefix="UPDATETIME =case" suffix="end,">
                <foreach collection="empPositionList" item="item" index="index">
                    <if test="item.updatetime != null">
                        when EMPID = #{item.empid} and POSITIONID = #{item.positionid} then #{item.updatetime}
                    </if>
                </foreach>
            </trim>
            <trim prefix="hr_status =case" suffix="end,">
                <foreach collection="empPositionList" item="item" index="index">
                    <if test="item.hrStatus != null">
                        when EMPID = #{item.empid} and POSITIONID = #{item.positionid} then #{item.hrStatus}
                    </if>
                </foreach>
            </trim>
        </trim>
        where
        EMPID in
        <foreach collection="empPositionList" item="item" open="(" separator="," close=")">
            #{item.empid}
        </foreach>
    </update>
复制代码

修改条件中trim里面 case后面不填对比字段,在if里面进行对比判断。

posted @   猪思马记  阅读(294)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示