其他分享
首页 > 其他分享> > mybatis-plus 3.4.3.1 进行批量 saveOrUpdate

mybatis-plus 3.4.3.1 进行批量 saveOrUpdate

作者:互联网



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);
});

}

}

 

 

 


 

 

标签:saveOrUpdate,mybatisplus,baomidou,param,entity,3.4,plus,import,com
来源: https://www.cnblogs.com/Icwq007/p/16055076.html