mybatis注解批量添加

mybatis注解使用批量添加

mybatis注解进行批量新增的时候

1.mapper 接口

  @InsertProvider(type = SourcePackageImportSqlProvinder.class, method = "batchinsert")
    Integer batchInsertRecommend(@Param("recommendList") List<PartnerResourcesRecommend> recommendList);

2 sql 拼接

public String batchinsert(Map<String, Object> map) {
		List<PartnerPackageSourceCate> partnerPackageSourceCates = (List<PartnerPackageSourceCate>) map.get("list");
		StringBuilder sb = new StringBuilder();
		sb.append("insert into table_name ");
		sb.append(
				"(unique_code,partner_id,partner_pid,package_number,cid,recources_id,type,create_time,back_uid,resources_name,resources_keyword,torder) ");
		sb.append("values ");
		MessageFormat mf = new MessageFormat(
				"(#'{'list[{0}].unique_code}, #'{'list[{0}].partner_id}, #'{'list[{0}].partner_pid},#'{'list[{0}].package_number},#'{'list[{0}].cid},"
						+ "#'{'list[{0}].recources_id},#'{'list[{0}].type},#'{'list[{0}].create_time},#'{'list[{0}].back_uid},#'{'list[{0}].resources_name},"
						+ "#'{'list[{0}].resources_keyword},#'{'list[{0}].torder})");
		for (int i = 0; i < partnerPackageSourceCates.size(); i++) {
			sb.append(mf.format(new Object[] { i +""}));
			if (i < partnerPackageSourceCates.size() - 1) {
				sb.append(",");
			}
		}
		return sb.toString();
	}

注意

1.在list集合超过1000条后 mf.format(new Object[] { i } 会拼接成 list[{1,000}] 会报错字符串所以超出范围, 要在 i 拼接上空字符串

你可能感兴趣的:(mybatis)