开发者

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

开发者 https://www.devze.com 2024-09-24 10:40 出处:网络 作者: 星狗、
目录myBATis使用双层<foreach>循环嵌套mapper接口mapper.XML入参实体类总结mybatis使用双层<foreach>循环嵌套
目录
  • myBATis使用双层<foreach>循环嵌套
    • mapper接口
    • mapper.XML
    • 入参实体类
  • 总结

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

    有个需求,要用到mybatis的双层循环嵌套插入数据,当然,可以使用单层,在业务代码层面循环插入,那样会php多出很多次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}http://www.devze.com,
                `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:上班时间
         */
      http://www.devze.com  private String goTimeTwo;
        /**
         * 上班2:上班时间 有效打卡范围开始时间
         */
        private String goTimeTwoS编程客栈tartTime;
        /**
         * 上班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里面就有双层集合。

    实体类映射时,mybjavascriptatis会自动映射SQL字段,只要实体类的基本字段与循环体里面的字段不相同,就可以在循环体里面也映射实体类的基本字段。

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。

    0

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    关注公众号