标签:logger 方便 sql mybatis tar invoke cut art foreach
之前在做项目时,使用mybatis,批量执行sql,这里简单写下步骤
jdbc:mysql://127.0.0.1:3307/mvs-report?allowMultiQueries=true
<insert id="batchInsert" parameterType="java.util.List" > insert into rpt_fault (FAULT_ID, FAULT_NAME) values <foreach collection="list" item="item" separator=","> (#{item.faultId,jdbcType=INTEGER}, #{item.faultName,jdbcType=VARCHAR}) </foreach> </insert>
1 private static final int ADD_MAX = 200; 2 private static final int DELETE_MAX = 500; 3 public static void executeBatchAdd(Object mapper, List<?> list) throws Exception { 4 if (Objects.isNull(list) || list.isEmpty()) 5 return; 6 7 try { 8 Method method = mapper.getClass().getMethod("batchInsert", List.class); 9 int size = list.size(); 10 if (size > ADD_MAX) { 11 int num = (size) / ADD_MAX + 1; 12 int start = 0, end = 0; 13 for (int i = 1; i <= num; i++) { 14 start = (i - 1) * ADD_MAX; 15 end = (i * ADD_MAX) > size ? size : (i * ADD_MAX); 16 method.invoke(mapper, list.subList(start, end)); 17 } 18 } else 19 method.invoke(mapper, list); 20 } catch (Exception e) { 21 logger.error("批量插入任务执行失败:", e.getMessage(), e); 22 throw e; 23 } finally { 24 list.clear(); 25 } 26 }
标签:logger 方便 sql mybatis tar invoke cut art foreach
原文地址:http://www.cnblogs.com/songyz/p/mybatis.html