| | |
| | | package com.ruoyi.common.core.mybatisplus.core; |
| | | |
| | | import cn.hutool.core.bean.copier.BeanCopier; |
| | | import cn.hutool.core.bean.copier.CopyOptions; |
| | | import cn.hutool.core.util.ReflectUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.Wrapper; |
| | | 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 lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.core.ResolvableType; |
| | |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * IServicePlus 实现类 |
| | |
| | | return super.updateBatchById(entityList, batchSize); |
| | | } |
| | | |
| | | /** |
| | | * 单sql批量插入( 全量填充 无视数据库默认值 ) |
| | | * 适用于无脑插入 |
| | | */ |
| | | @Override |
| | | public boolean saveBatch(Collection<T> entityList) { |
| | | return saveBatch(entityList, DEFAULT_BATCH_SIZE); |
| | |
| | | @Override |
| | | public K getVoById(Serializable id, CopyOptions copyOptions) { |
| | | T t = getBaseMapper().selectById(id); |
| | | return oneVoCopy(t, copyOptions); |
| | | return BeanCopyUtils.oneCopy(t, copyOptions, voClass); |
| | | } |
| | | |
| | | /** |
| | |
| | | if (list == null) { |
| | | return null; |
| | | } |
| | | return listVoCopy(list, copyOptions); |
| | | return BeanCopyUtils.listCopy(list, copyOptions, voClass); |
| | | } |
| | | |
| | | /** |
| | |
| | | if (list == null) { |
| | | return null; |
| | | } |
| | | return listVoCopy(list, copyOptions); |
| | | return BeanCopyUtils.listCopy(list, copyOptions, voClass); |
| | | } |
| | | |
| | | /** |
| | |
| | | @Override |
| | | public K getVoOne(Wrapper<T> queryWrapper, CopyOptions copyOptions) { |
| | | T t = getOne(queryWrapper, true); |
| | | return oneVoCopy(t, copyOptions); |
| | | return BeanCopyUtils.oneCopy(t, copyOptions, voClass); |
| | | } |
| | | |
| | | /** |
| | |
| | | if (list == null) { |
| | | return null; |
| | | } |
| | | return listVoCopy(list, copyOptions); |
| | | return BeanCopyUtils.listCopy(list, copyOptions, voClass); |
| | | } |
| | | |
| | | /** |
| | |
| | | @Override |
| | | public PagePlus<T, K> pageVo(PagePlus<T, K> page, Wrapper<T> queryWrapper, CopyOptions copyOptions) { |
| | | PagePlus<T, K> result = getBaseMapper().selectPage(page, queryWrapper); |
| | | List<K> volist = listVoCopy(result.getRecords(), copyOptions); |
| | | List<K> volist = BeanCopyUtils.listCopy(result.getRecords(), copyOptions, voClass); |
| | | result.setRecordsVo(volist); |
| | | return result; |
| | | } |
| | | |
| | | private K oneVoCopy(T t, CopyOptions copyOptions) { |
| | | K k = ReflectUtil.newInstanceIfPossible(voClass); |
| | | return BeanCopier.create(t, k, copyOptions).copy(); |
| | | } |
| | | |
| | | private List<K> listVoCopy(List<T> list, CopyOptions copyOptions) { |
| | | return list.stream() |
| | | .map(any -> BeanCopier.create(any, ReflectUtil.newInstanceIfPossible(voClass), copyOptions).copy()) |
| | | .collect(Collectors.toList()); |
| | | } |
| | | |
| | | } |