zoukankan      html  css  js  c++  java
  • mybatis批量删除、插入

    继承

    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

    在service层的写法示例:

    package com.autosys.parts.service.impl;
    
    import com.alibaba.fastjson.JSONObject;
    import com.autosys.common.exception.ServiceException;
    import com.autosys.common.util.Constants;
    import com.autosys.parts.mapper.PartsMapper;
    import com.autosys.parts.model.*;
    import com.autosys.parts.service.IPartsService;
    import com.autosys.utils.ConstantUtils;
    import com.autosys.utils.StrUtils;
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import java.util.*;
    
    /**
     * @Auther: QuanFeng.Liu
     * @Date: 2019/12/04 14:17
     * @Description:
     */
    @Slf4j
    @Service
    public class PartsService extends ServiceImpl<PartsMapper, BesiParts> implements IPartsService {
    
        @Autowired
        PartsMapper partsMapper;
    
        public void saveParts(List<Besi2Raw> besi2List, List<BesiParts> partList) {
            Map<String, Object> besi2Map = new HashMap<>();
            Map<String, Object> besiMap = new HashMap<>();
            besi2List.forEach(item -> {
                besi2Map.put(item.getPartCode() + "_" + item.getPlantCode() + "_" + item.getModelCode() + "_" + item.getRegPlantCode(), item);
            });
            partList.forEach(item -> {
                besiMap.put(item.getPartNo() + "_" + item.getPlantCode() + "_" + item.getModelCode() + "_" + item.getRegPlant(), item);
            });
    
            List<BesiParts> addPartList = new ArrayList<>();
            List<String> minPartList = new ArrayList<>();
    
            besi2List.forEach(item -> {
                String key = item.getPartCode() + "_" + item.getPlantCode() + "_" + item.getModelCode() + "_" + item.getRegPlantCode();
                if(!besiMap.containsKey(key)) {
                    BesiParts besiParts = new BesiParts();
                    besiParts.setPartNo(item.getPartCode());
                    besiParts.setModelCode(item.getModelCode());
                    besiParts.setPartName(item.getPartName());
                    besiParts.setPartNameEn(item.getPartNameEn());
                    besiParts.setPowerFlag(item.getPowerFlag());
                    besiParts.setPurcasingCode(item.getPurcasingCode());
                    besiParts.setRegPlant(item.getRegPlantCode());
                    besiParts.setUsePlant(item.getUsePlant());
                    besiParts.setProduceCode(item.getProduceCode());
                    besiParts.setPlantCode(item.getPlantCode());
                    besiParts.setNewFlag(Constants.Flag_Yes);
                    besiParts.setStatus(Constants.Status_Valid);
                    besiParts.setCreateBy(Constants.Ver_Type_B2);
                    besiParts.setPartType(ConstantUtils.Parts_Yipei);
                    besiParts.setCreateTime(new Date());
                    addPartList.add(besiParts);
                }
            });
    
            if(addPartList.size() > 0) {
                partsMapper.updPartNewFlag();
                saveBatch(addPartList);
            }
    
            partList.forEach(item -> {
                String key = item.getPartNo() + "_" + item.getPlantCode() + "_" + item.getModelCode() + "_" + item.getRegPlant();
                if(!besi2Map.containsKey(key)) {
                    minPartList.add(item.getId());
                }
            });
            if(minPartList.size() > 0) {
                removeByIds(minPartList);
            }
        }
    }
  • 相关阅读:
    HDU
    HDU-1166 敌兵布阵 (基础线段树)
    Matrices with XOR property (暴力)
    CF-825E Minimal Labels (反向拓扑)
    CodeForces-1144E Median String (模拟)
    操作文件和目录
    文件读写
    装饰器
    数据结构和算法
    Scrapy shell调试返回403错误
  • 原文地址:https://www.cnblogs.com/xiaofengfree/p/12144754.html
Copyright © 2011-2022 走看看