• mybatis 批量新增-批量修改-批量删除操作


    mapper.xml

    <!-- 批量新增 -->
        <insert id="saveBatch" parameterType="java.util.List">
    	 insert into t_gceb_login_log(
    	  id ,
    	  user_id ,
    	  user_name ,
    	  mip ,
    	  jsession_id_4a ,
    	  create_by ,
    	  create_by_name ,
    	  update_by ,
    	  update_by_name ,
    	  create_time ,
    	  update_time ,
    	  data_version ,
    	  attribute1 ,
    	  attribute2 ,
    	  attribute3 ,
    	  attribute4 ,
    	  attribute5 ,
    	  attribute6 
    	 )
    	 values
    	 <foreach collection="list" item="item" index="index" separator="," >
    	 (
    	   #{item.id},
    	   #{item.userId},
    	   #{item.userName},
    	   #{item.mip},
    	   #{item.jsessionId4a},
    	   #{item.createBy},
    	   #{item.createByName},
    	   #{item.updateBy},
    	   #{item.updateByName},
    	   #{item.createTime},
    	   #{item.updateTime},
    	   #{item.dataVersion},
    	   #{item.attribute1},
    	   #{item.attribute2},
    	   #{item.attribute3},
    	   #{item.attribute4},
    	   #{item.attribute5},
    	   #{item.attribute6}
    	 ) 
    	 </foreach>
    </insert>
    
    	<!-- 批量修改 -->
        <update id="updateBatch" parameterType="java.util.List">
            update t_gceb_login_log
            <trim prefix="set" suffixOverrides=",">
            	<trim prefix="userId =case" suffix="end,">
                    <foreach collection="list" item="item">
                        <if test="item.userId!=null">
                            when id=#{item.id} then #{item.userId}
                        </if>
                    </foreach>
                </trim>
            	<trim prefix="attribute1 =case" suffix="end,">
                    <foreach collection="list" item="item">
                        <if test="item.attribute1!=null">
                            when id=#{item.id} then #{item.attribute1}
                        </if>
                    </foreach>
                </trim>
    	    </trim>
    	    <where>
    	    	<foreach collection="list" separator="or" item="item">
    	    		id=#{item.id}
    	    	</foreach>
    	    </where>
        </update>
        
        <delete id="logBatchRemove"  parameterType = "java.util.List">
         delete from t_gceb_login_log 
         <where>
         	id in
         	<foreach collection="list"  item="item" open="(" separator="," close=")"  >
    	   	#{item}
    	   	</foreach>
    	   </where>
    	</delete> 

    注意点:批量操作也是有性能问题的,一个批次建议200条

    List<List<LoginLog>> batchList = ListUtils.partition(loginLogList, 200);
    		for (List<LoginLog> array: batchList) {
    			int sum = this.sqlSessionTemplate.update("loginLog.updateBatch", array);
    			LOGGER.debug("批量更新记录数:{}", sum);
    		}
    

      

  • 相关阅读:
    mac远程登录linux
    漏洞反连检测相关知识
    脆弱性检测一些概念知识
    istio 503处理
    DeploymentController
    DaemonSetController
    keycloak集成gitlab
    NamespaceController
    Caused by: javax.cache.CacheException: Multiple CachingProviders have been configured when only a single CachingProvider is expected
    禅道升级(inux一键安装包升级)
  • 原文地址:https://www.cnblogs.com/yun965861480/p/10938726.html
Copyright © 2020-2023  润新知