Java如何优雅的处理插入数据,数据库有相同的数据则更新,没有的话则插入

欢迎大家关注我的公众号【老周聊架构】,Java后端主流技术栈的原理、源码分析、架构以及各种互联网高并发、高性能、高可用的解决方案。

一、前言

相信小伙伴们在做后台开发的时候,经常要用到CRUD。经常用CRUD并不丢人,关键是怎么把CRUD用到更简洁、更极致,这样的一直下去的话,其实你慢慢的也成为了你眼中的那些大神们。我这里是用到了 JDK1.8的一些特性,不了解 JDK1.8的新特性的话,可以先简单了解一下。

好了,废话不多说,我直接上代码了。

二、代码实现

package com.riemann.springbootdemo.service.impl;

import com.google.common.collect.Maps;
import com.riemann.springbootdemo.dao.PoiShopDao;
import com.riemann.springbootdemo.model.PoiShopEntity;
import com.riemann.springbootdemo.service.PoiShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author riemann
 * @date 2020/02/14 21:00
 */
public class PoiShopServiceImpl implements PoiShopService {

    @Autowired
    private PoiShopDao poiShopDao;

    @Override
    public Integer updataOrInsert(List<PoiShopEntity> shopList) {
        int amount = 0;
        Map<String, List> dataMap = validateShopData(shopList);
        for (String key : dataMap.keySet()) {
            if ("insert".endsWith(key)) {
                amount = poiShopDao.uploadShopData(dataMap.get(key));
            } else {
                amount = poiShopDao.updateBatchShopData(dataMap.get(key));
            }
        }
        return amount;
    }

    private Map<String, List> validateShopData(List<PoiShopEntity> shopEntityList) {
        HashMap<String, List> result = Maps.newHashMap();
        List<PoiShopEntity> shopList = poiShopDao.selectSelectiveList(shopEntityList);
        // 取差集
        List<PoiShopEntity> shopInsertList = shopList.stream().
                filter(item -> findShopData(item.getPoiId(), item.getSource(), shopList) == -1)
                .collect(Collectors.toList());
        // 取并集
        List<PoiShopEntity> shopUpdataList = shopList.stream().
                filter(item -> findShopData(item.getPoiId(), item.getSource(), shopList) > -1)
                .collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(shopInsertList)) {
            result.put("insert", shopInsertList);
        } else {
            result.put("update", shopUpdataList);
        }
        return result;
    }

    private int findShopData(String poiId, Integer source, List<PoiShopEntity> shopList) {
        int res = -1;
        for (int i = 0; i < shopList.size(); i++) {
            if (poiId.equals(shopList.get(i).getPoiId()) && source.intValue() == shopList.get(i).getSource()) {
                res = i;
                break;
            }
        }
        return res;
    }
}

是不是看着比较简洁一些了?如果你又更好的方式实现,可以留言分享一下。

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老周聊架构

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值