SoFunction
Updated on 2025-03-01

mybatis uses double-layer loop nesting method

mybatis uses double layer <foreach> loop nesting

There is a requirement to use mybatis' double-layer loop nested insertion data. Of course, you can use a single layer to loop insertion at the business code level, which will result in many more IO databases. If the concurrency volume is high, the performance will be very low;

So, here we use double-layer loop nesting at the mybatis level to reduce the performance consumption problems caused by database IO.

mapper interface

void updateSchDataByShiftAuto(UpdateSchDataByShiftParam param);

<update  parameterType="">

        <foreach collection="schRuleIdParams" item="item" separator=";">
            update ${tableName} set `shift_id` = #{},
            `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` = #{}
            <if test=" != null and () >0 ">
                and `sch_date` in
                <foreach collection="" item="flag" separator="," open="(" close=")">
                    #{flag}
                </foreach>
            </if>
        </foreach>

</update>

Entry entity class

@Data
public class UpdateSchDataByShiftParam {

    /**
      * mybatis loop condition set
      */
    private List&lt;SchRuleIdParam&gt; schRuleIdParams;

    /**
      * Table name
      */
    private String tableName;

    /**
      * Shift ID
      */
    private Long shiftId;

    /**
      * Working hours: 1 to and from get off work once a day, 2 to and from get off work twice a day
      */
    private Integer shiftFlag;

    /**
      * Working hours
      */
    private String hours;
    /**
      * Late calculation: How many minutes late is considered late, default is 1
      */
    private Integer lateCal;
    /**
      * Early retreat calculation: How many minutes to leave early is considered early retreat, the default is 1
      */
    private Integer leaveEarlyCal;
    /**
      * Calculation of absenteeism: How many minutes late is considered absenteeism, the default is 121
      */
    private Integer absCalLate;
    /**
      * Calculation of absenteeism: How many minutes to leave early is considered absenteeism, the default is 121
      */
    private Integer absCalLeaveEarly;

    /**
      * Working 1: Working hours
      */
    private String goTimeOne;
    /**
      * Working 1: Working hours Valid check-in range start time
      */
    private String goTimeOneStartTime;
    /**
      * Working 1: Working hours Valid check-in range end time
      */
    private String goTimeOneEndTime;
    /**
      * Working 1: Working hours Do you have to check in? 0 You have to check in, 1 does not have to check in
      */
    private Integer goTimeOneStatus;
    /**
      * Get off work 1: Get off work hours
      */
    private String offTimeOne;
    /**
      * Get off work 1: Get off work time Valid check-in range start time
      */
    private String offTimeOneStartTime;
    /**
      * Get off work 1: Get off work time Valid check-in range end time
      */
    private String offTimeOneEndTime;
    /**
      * Get off work 1: Get off work hours Do you have to check in: 0 must check in, 1 does not have to check in
      */
    private Integer offTimeOneStatus;
    /**
      * Working 2: Working hours
      */
    private String goTimeTwo;
    /**
      * Working 2: Working hours Valid check-in range start time
      */
    private String goTimeTwoStartTime;
    /**
      * Working 2: Working hours Valid check-in range end time
      */
    private String goTimeTwoEndTime;
    /**
      * Working 2: Working hours Do you have to check in? 0 You have to check in, 1 does not have to check in
      */
    private Integer goTimeTwoStatus;
    /**
      * Get off work 2: Get off work hours
      */
    private String offTimeTwo;
    /**
      * Get off work 2: Get off work time Valid check-in range start time
      */
    private String offTimeTwoStartTime;
    /**
      * Get off work 2: Get off work time Valid check-in range end time
      */
    private String offTimeTwoEndTime;
    /**
      * Get off work 2: Get off work hours Do you have to check in: 0 You have to check in, 1 does not have to check in
      */
    private Integer offTimeTwoStatus;
    /**
      * Is the end time of the valid check-in range the second day: 0 No, 1 Yes
      */
    private Integer endTimeStatus;
    /**
      * Current date
      */
    private String nowTime;
    /**
      * Creator ID
      */
    private Long createId;
    /**
      * Name of the founder
      */
    private String createName;


}

Let me explain here that there are basic field attributes and collection field attributes in the entity class.

When looping, use the set field in the entity class as the outermost loop body. The entities in the entity class's collection also have a layer set, so there is a double-layer set inside.

When mapping the entity class, mybatis will automatically map SQL fields. As long as the basic fields of the entity class are different from the fields in the loop body, the basic fields of the entity class can also be mapped in the loop body.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.