• mybatis批量操作


    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"
  • 相关阅读:
    Murano Weekly Meeting 2016.05.24
    Murano Weekly Meeting 2016.05.17
    Murano Weekly Meeting 2016.05.10
    python3.4 x86_64-linux-gnu-gcc Error
    初学makefile
    解决Spring JdbcTemplate调用queryForObject()方法结果集为空时报异常
    svn命令行使用
    jquery选中checkbox
    jstl core and jstl fn
    linux上的shutdown命令
  • 原文地址:https://www.cnblogs.com/thiaoqueen/p/9176253.html
Copyright © 2020-2023  润新知