zoukankan      html  css  js  c++  java
  • 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);
    		}
    

      

  • 相关阅读:
    序列号Pickle模块
    随机数Random模块
    selenium保存cookies 实例02
    selenium读取浏览器已有Cookies 实例01
    selenium的简单演示程序
    java连接sftp服务器读取压缩包的文件(例:读取zip中的csv文件返回数组)
    java实现连接sftp服务器并下载文件到本地
    在idea中实现热部署
    java使用IText将数据导出为pdf文件(数据为excel表格样式)
    使用poi解析Excel文件转化数组形式的集合(List<String[] list)
  • 原文地址:https://www.cnblogs.com/yun965861480/p/10938726.html
Copyright © 2011-2022 走看看