java项目两种方法实现大批量数据:存在就更新,不存在就新增,脏数据就删除。
文章目录
- java项目两种方法实现大批量数据:存在就更新,不存在就新增,脏数据就删除。
- 一、法一:存在就更新,不存在就新增,原本的脏数据就删除
- 二、法二:新增批次号,存两批数据,只做新增和删除。不做修改
一、法一:存在就更新,不存在就新增,原本的脏数据就删除
@Component
public class InsetUserUtils {@Resourceprivate ThreadPoolExecutor threadPoolExecutor;public void getNewUserInfo() {List<User> userList = userService.listInformation();if (!CollectionUtils.isEmpty(userList)) {Map<String, Integer> cache = userService.list(new LambdaQueryWrapper<User>()).stream().collect(Collectors.toMap(data -> data.getCode(), User::getId));List<TableRowStatistics> update = new ArrayList<>();List<TableRowUStatistics> save = new ArrayList<>();for (User user : userList) {Future<?> submit = threadPoolExecutor.submit(() -> {if (cache.containsKey(user.genCode())) {user.setId(cache.get(user.genCode()));add(update, user);} else {add(save, user);}});}if (ObjectUtil.isNotEmpty(save)) {userService.saveBatch(save);}if (ObjectUtil.isNotEmpty(update)) {userService.updateBatchById(update);}}public synchronized void add(List<User> list, User data) {list.add(data);if (list.size() > 200) {if (null == data.getId()) {userService.saveBatch(list);} else {userService.updateBatchById(list);}list.clear();}}}
二、法二:新增批次号,存两批数据,只做新增和删除。不做修改
@Component
public class InsetUserUtils {@Resourceprivate ThreadPoolExecutor threadPoolExecutor;public void getNewUserInfo() {String indexNewCode = UUIDUtils.getUUid();String rowOldIndexCode = getRowIndexCode();List<User> userList = userService.listInformation();if (!CollectionUtils.isEmpty(userList)) {List<TableRowUStatistics> save = new ArrayList<>();for (User user : userList) {Future<?> submit = threadPoolExecutor.submit(() -> {add(save, user);});}if (ObjectUtil.isNotEmpty(save)){userService.saveBatch(save);}TableRowUStatistics tableRow =tableRowUStatisticsService.getRowIndexCode();if (ObjectUtils.isNotEmpty(tableRow)&&StringUtils.isNotBlank(tableRow.getIndexCode())&& StringUtils.equalsIgnoreCase(tableRow.getIndexCode(), indexNewCode)){stringRedisTemplate.opsForValue().set("redisIndexCode", indexNewCode);ThreadPoolExecutor instance = ThreadPoolUtil.instance();instance.submit(() -> {if (StringUtils.isNotBlank(rowOldIndexCode)) {if (!StringUtils.equalsIgnoreCase(rowOldIndexCode, indexNewCode) ) {tableRowUStatisticsService.removeCatalogCode(rowOldIndexCode);}}});}}}public static String getRowIndexCode(){StringRedisTemplate stringRedisTemplate = SpringUtils.getBean(StringRedisTemplate.class);String rowBatchNumber = stringRedisTemplate.opsForValue().get("redisIndexCode");if (StringUtils.isNotBlank(rowBatchNumber)){return rowBatchNumber;}else {TableRowUStatisticsService tableRowUStatisticsService = SpringUtils.getBean(TableRowUStatisticsService.class);TableRowUStatistics tableRowUStatistics =tableRowUStatisticsService.getRowIndexCode();if (ObjectUtils.isNotEmpty(tableRowUStatistics)&&StringUtils.isNotBlank(tableRowUStatistics.getIndexCode())){return tableRowUStatistics.getIndexCode();}return "";}}public synchronized void add(List<User> list, User data) {list.add(data);if (list.size() > 200) {try{userService.saveBatch(list);}catch (Exception e){e.printStackTrace();}list.clear();}}
}