mybatis使用双层<foreach> 循环嵌套

有个需求,要用到mybatis的双层循环嵌套插入数据,当然,可以使用单层,在业务代码层面循环插入,那样会多出很多次IO数据库,如果并发量高了,性能将会很低;所以,这里我们在mybatis层面使用双层循环嵌套来减少数据库IO带来的性能消耗问题。

mapper接口:

void updateSchDataByShiftAuto(UpdateSchDataByShiftParam param);

mapper.xml:

<update id="updateSchDataByShiftAuto" parameterType="net.crisps.hr.time.param.UpdateSchDataByShiftParam">

        <foreach collection="schRuleIdParams" item="item" separator=";">
            update ${tableName} set `shift_id` = #{item.newSchRuleId},
            `shift_flag` = #{shiftFlag},`hours` = #{hours},`late_cal` = #                     {lateCal},`leave_early_cal` = #{leaveEarlyCal},
            `abs_cal_late` = #{absCalLate},`abs_cal_leave_early` = #{absCalLeaveEarly},`go_time_one` = #{goTimeOne},
            `go_time_one_start_time` = #{goTimeOneStartTime},`go_time_one_end_time` = #{goTimeOneEndTime},
            `go_time_one_status` = #{goTimeOneStatus},`off_time_one` = #{offTimeOne},`off_time_one_start_time` = #{offTimeOneStartTime},
            `off_time_one_end_time` = #{offTimeOneEndTime},`off_time_one_status` = #{offTimeOneStatus},`go_time_two` = #{goTimeTwo},
            `go_time_two_start_time` = #{goTimeTwoStartTime},`go_time_two_end_time` = #{goTimeTwoEndTime},`go_time_two_status` = #{goTimeTwoStatus},
            `off_time_two` = #{offTimeTwo},`off_time_two_start_time` = #{offTimeTwoStartTime},creater_id = #{createId},creater_name = #{createName},create_time = #{nowTime},
            `off_time_two_end_time` = #{offTimeTwoEndTime},`off_time_two_status` = #{offTimeTwoStatus},`end_time_status` = #{endTimeStatus}
            where
            `shift_id` = #{item.oldSchRuleId}
            <if test="item.dates != null and item.dates.size() >0 ">
                and `sch_date` in
                <foreach collection="item.dates" item="flag" separator="," open="(" close=")">
                    #{flag}
                </foreach>
            </if>
        </foreach>

</update>

入参实体类:

@Data
public class UpdateSchDataByShiftParam {

    /**
     * mybatis循环条件集合
     */
    private List<SchRuleIdParam> schRuleIdParams;

    /**
     * 表名
     */
    private String tableName;

    /**
     * 班次ID
     */
    private Long shiftId;

    /**
     * 上班时段:1一天一次上下班,2一天二次上下班
     */
    private Integer shiftFlag;

    /**
     * 工作时长
     */
    private String hours;
    /**
     * 迟到计算:迟到几分钟算迟到,默认为 1
     */
    private Integer lateCal;
    /**
     * 早退计算:早退几分钟算早退,默认为 1
     */
    private Integer leaveEarlyCal;
    /**
     * 旷工计算:迟到几分钟算旷工,默认为121
     */
    private Integer absCalLate;
    /**
     * 旷工计算:早退几分钟算旷工,默认为121
     */
    private Integer absCalLeaveEarly;

    /**
     * 上班1:上班时间
     */
    private String goTimeOne;
    /**
     * 上班1:上班时间 有效打卡范围开始时间
     */
    private String goTimeOneStartTime;
    /**
     * 上班1:上班时间 有效打卡范围结束时间
     */
    private String goTimeOneEndTime;
    /**
     * 上班1:上班时间 是否必须打卡:0必须打卡,1不是必须打卡
     */
    private Integer goTimeOneStatus;
    /**
     * 下班1:下班时间
     */
    private String offTimeOne;
    /**
     * 下班1:下班时间 有效打卡范围开始时间
     */
    private String offTimeOneStartTime;
    /**
     * 下班1:下班时间 有效打卡范围结束时间
     */
    private String offTimeOneEndTime;
    /**
     * 下班1:下班时间 是否必须打卡:0必须打卡,1不是必须打卡
     */
    private Integer offTimeOneStatus;
    /**
     * 上班2:上班时间
     */
    private String goTimeTwo;
    /**
     * 上班2:上班时间 有效打卡范围开始时间
     */
    private String goTimeTwoStartTime;
    /**
     * 上班2:上班时间 有效打卡范围结束时间
     */
    private String goTimeTwoEndTime;
    /**
     * 上班2:上班时间 是否必须打卡:0必须打卡,1不是必须打卡
     */
    private Integer goTimeTwoStatus;
    /**
     * 下班2:下班时间
     */
    private String offTimeTwo;
    /**
     * 下班2:下班时间 有效打卡范围开始时间
     */
    private String offTimeTwoStartTime;
    /**
     * 下班2:下班时间 有效打卡范围结束时间
     */
    private String offTimeTwoEndTime;
    /**
     * 下班2:下班时间 是否必须打卡:0必须打卡,1不是必须打卡
     */
    private Integer offTimeTwoStatus;
    /**
     * 有效打卡范围结束时间是否是第二日:0否,1是
     */
    private Integer endTimeStatus;
    /**
     * 当前日期
     */
    private String nowTime;
    /**
     * 创建人ID
     */
    private Long createId;
    /**
     * 创建人姓名
     */
    private String createName;


}

这里说明一下,实体类中有基本字段属性,还有集合字段属性。循环时用实体类中的集合字段当最外层循环体,实体类的集合里面的实体还有一个层集合,这样mapper.xml里面就有双层集合。实体类映射时,mybatis会自动映射SQL字段,只要实体类的基本字段与循环体里面的字段不相同,就可以在循环体里面也映射实体类的基本字段。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值