随笔 - 4  文章 - 2  评论 - 0  阅读 - 10287 
  • service类通过 SqlHelper.saveOrUpdateBatch 实现通过自定义的 唯一索引 进行 批量保存更新


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.apache.ibatis.binding.MapperMethod;
import org.springframework.stereotype.Service;

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

import cn.com.redboard.modules.mi.base.entity.MesProdMiProc;
import cn.com.redboard.modules.mi.base.mapper.MesProdMiProcMapper;
import cn.com.redboard.modules.mi.base.service.IMesProdMiProcService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @Description: MI工艺流程Service
* @Author: wqc
* @Date: 2022-03-01
* @Version: V1.0
*/
@Service
public class MesProdMiProcServiceImpl extends ServiceImpl<MesProdMiProcMapper, MesProdMiProc> implements IMesProdMiProcService {

@Override
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
public void sync(List<MesProdMiProc> prodMiProcList) {

// 每2000条语句,进行一次sqlSession.flushStatements()
SqlHelper.saveOrUpdateBatch(this.entityClass, this.mapperClass, this.log, prodMiProcList, 2000, (sqlSession, entity) -> {
MapperMethod.ParamMap param = new MapperMethod.ParamMap();

Object erpProcessId = ReflectionKit.getFieldValue(entity, ${otherUniqueIdx});
LambdaQueryWrapper<MesProdMiProc> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(MesProdMiProc::getErpProcessId, erpProcessId);
// 自定义查询条件
param.put("ew", queryWrapper);

// 判断记录是否存在,存在则更新,否则插入
return StringUtils.checkValNull(erpProcessId) || CollectionUtils.isEmpty(sqlSession.selectList(this.getSqlStatement(SqlMethod.SELECT_LIST), param));
}, (sqlSession, entity) -> {
MapperMethod.ParamMap param = new MapperMethod.ParamMap();
// 需要更新的当前记录实体
param.put("et", entity);

LambdaQueryWrapper<MesProdMiProc> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(MesProdMiProc::getErpProcessId, ReflectionKit.getFieldValue(entity, ${otherUniqueIdx}));
            // 自定义查询条件
            param.put("ew", queryWrapper);

sqlSession.update(this.getSqlStatement(SqlMethod.UPDATE), param);
});

}

}

 

  • SqlHelper.saveOrUpdateBatch具体源码

 

 


  • SqlHelper.executeBatch具体源码

     

     


 

 

posted on   Xs007  阅读(7143)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示