java批量插入sql
控制器
// 批量插入导出记录 int[] list = StringProcesser.toIntArray(idStr); List<ExportResource> ERLists = new ArrayList<>(); for (int id : list) { ExportResource exportResource = new ExportResource(); exportResource.setUserId(userId); exportResource.setResourceId(id); exportResource.setCreateTime(TimeProcesser.getUnixTime()); ERLists.add(exportResource); } exportResourceManager.save2Db(ERLists);
ExportResourceManager
@Autowired private IExportResourceDao iExportResourceDao; public void save2Db(List<ExportResource> exportResource){ iExportResourceDao.insertCollectList(exportResource); }
IExportResourceDao
@Insert({ "<script>", "insert into res_export_records(userId, resourceId, createTime) values ", "<foreach collection='exportResource' item='item' index='index' separator=','>", "(#{item.userId}, #{item.resourceId}, #{item.createTime})", "</foreach>", "</script>" }) int insertCollectList(@Param(value = "exportResource") List<ExportResource> exportResource);