1.批量插入
public int saveGroupMember(int groupId,int[] memIds) { List<GroupMemberPo> groupMemberPoList = new ArrayList<GroupMemberPo>(); for(int i=0;i<memIds.length;i++){ GroupMemberPo groupMemberPo = new GroupMemberPo(); groupMemberPo.setGroupId(groupId); groupMemberPo.setMemberId(memIds[i]); groupMemberPo.setUpdateTime(new Date()); groupMemberPoList.add(groupMemberPo); } return this.groupMemberDao.insert(groupMemberPoList); }
int insert(List<GroupMemberPo> list);
<insert id="insert" parameterType="java.util.ArrayList"> insert into T_ROTA_R_GROUP_MEMBER(group_id,member_id,update_time) VALUES <foreach collection ="list" item="groupMemberPo" separator =","> (#{groupMemberPo.groupId}, #{groupMemberPo.memberId}, #{groupMemberPo.updateTime}) </foreach> </insert>
2.批量更新
下面的方式2效率更高,参考 批量更新数据两种方法效率对比
方式1:https://www.cnblogs.com/Jason-Xiang/p/6558334.html
方式2:
<update id="deleteByDateAndOrgCode" parameterType="java.util.ArrayList"> <foreach collection ="list" item="rotaRecordPo" separator =";"> UPDATE T_ROTA_RECORD set is_delete = 1 ,update_time = #{rotaRecordPo.updateTime} where duty_date = #{rotaRecordPo.dutyDate} and org_code = #{rotaRecordPo.orgCode} and is_delete = 0 </foreach> </update>
3.foreach 传入数组
int deleteByPrimaryKey(int[] ids);
<update id="deleteByPrimaryKey" parameterType="java.util.ArrayList"> update T_ROTA_R_GROUP_MEMBER set is_delete= 1,update_time=(select now()) where id in <foreach collection="array" item="item" separator="," open="(" close=")"> #{item} </foreach> </update>
collection 项填"array",item 项填"item"