mybatis使用双层<foreach>循环嵌套方式
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字段,只要实体类的基本字段与循环体里面的字段不相同,就可以在循环体里面也映射实体类的基本字段。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
基于spring boot 1.5.4 集成 jpa+hibernate+jdbcTemplate(详解)
下面小编就为大家带来一篇基于spring boot 1.5.4 集成 jpa+hibernate+jdbcTemplate(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-06-06
SpringBoot 图书管理系统(删除、强制登录、更新图书)详细代码
在企业开发中,通常不采用delete语句进行物理删除,而是使用逻辑删除,逻辑删除通过修改标识字段来表示数据已被删除,方便数据恢复,本文给大家介绍SpringBoot 图书管理系统实例代码,感兴趣的朋友跟随小编一起看看吧2024-09-09
springboot使用logback文件查看错误日志过程详解
这篇文章主要介绍了springboot使用logback文件查看错误日志过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2019-09-09


最新评论