| | |
| | | package com.ruoyi.common.core.mybatisplus.core; |
| | | |
| | | import cn.hutool.core.bean.copier.CopyOptions; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.Wrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.TableInfo; |
| | | import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Assert; |
| | | import com.baomidou.mybatisplus.core.toolkit.ClassUtils; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.common.core.page.PagePlus; |
| | | import com.ruoyi.common.utils.BeanCopyUtils; |
| | | import com.ruoyi.common.utils.reflect.ReflectUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.core.ResolvableType; |
| | | |
| | | import java.io.Serializable; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | |
| | | /** |
| | | * 单sql批量插入( 全量填充 ) |
| | | * 适用于无脑插入 |
| | | */ |
| | | @Override |
| | | public boolean saveAll(Collection<T> entityList) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 全量保存或更新 ( 按主键区分 ) |
| | | */ |
| | | @Override |
| | | public boolean saveOrUpdateAll(Collection<T> entityList) { |
| | | TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass); |
| | | Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!"); |
| | | String keyProperty = tableInfo.getKeyProperty(); |
| | | Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!"); |
| | | List<T> addList = new ArrayList<>(); |
| | | List<T> updateList = new ArrayList<>(); |
| | | int row = 0; |
| | | for (T entity : entityList) { |
| | | Object id = ReflectUtils.invokeGetter(entity, keyProperty); |
| | | if (ObjectUtil.isNull(id)) { |
| | | addList.add(entity); |
| | | } else { |
| | | updateList.add(entity); |
| | | } |
| | | } |
| | | if (updateBatchById(updateList)) { |
| | | row += updateList.size(); |
| | | } |
| | | row += baseMapper.insertAll(addList); |
| | | return row == entityList.size(); |
| | | } |
| | | |
| | | /** |
| | | * 根据 ID 查询 |
| | | * |
| | | * @param id 主键ID |