疯狂的狮子li
2021-12-15 3f97d19381449b09898a9462e9289904696736c4
ruoyi-common/src/main/java/com/ruoyi/common/core/mybatisplus/core/ServicePlusImpl.java
@@ -1,20 +1,21 @@
package com.ruoyi.common.core.mybatisplus.core;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.ReflectionKit;
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.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -23,11 +24,14 @@
/**
 * IServicePlus 实现类
 *
 * @param <M> Mapper类
 * @param <T> 数据实体类
 * @param <V> vo类
 * @author Lion Li
 */
@Slf4j
@SuppressWarnings("unchecked")
public class ServicePlusImpl<M extends BaseMapperPlus<T>, T, K> extends ServiceImpl<M, T> implements IServicePlus<T, K> {
public class ServicePlusImpl<M extends BaseMapperPlus<T>, T, V> extends ServiceImpl<M, T> implements IServicePlus<T, V> {
   @Autowired
   protected M baseMapper;
@@ -45,31 +49,26 @@
      return entityClass;
   }
   protected Class<T> mapperClass = currentMapperClass();
   protected Class<M> mapperClass = currentMapperClass();
   protected Class<K> voClass = currentVoClass();
   protected Class<V> voClass = currentVoClass();
   public Class<K> getVoClass() {
   public Class<V> getVoClass() {
      return voClass;
   }
   @Override
   protected Class<T> currentMapperClass() {
      return (Class<T>) this.getResolvableType().as(ServicePlusImpl.class).getGeneric(0).getType();
   protected Class<M> currentMapperClass() {
      return (Class<M>) ReflectionKit.getSuperClassGenericType(this.getClass(), ServicePlusImpl.class, 0);
   }
   @Override
   protected Class<T> currentModelClass() {
      return (Class<T>) this.getResolvableType().as(ServicePlusImpl.class).getGeneric(1).getType();
      return (Class<T>) ReflectionKit.getSuperClassGenericType(this.getClass(), ServicePlusImpl.class, 1);
   }
   protected Class<K> currentVoClass() {
      return (Class<K>) this.getResolvableType().as(ServicePlusImpl.class).getGeneric(2).getType();
   }
   @Override
   protected ResolvableType getResolvableType() {
      return ResolvableType.forClass(ClassUtils.getUserClass(getClass()));
   protected Class<V> currentVoClass() {
      return (Class<V>) ReflectionKit.getSuperClassGenericType(this.getClass(), ServicePlusImpl.class, 2);
   }
   /**
@@ -119,110 +118,109 @@
   /**
    * 单sql批量插入( 全量填充 )
    * 适用于无脑插入
    */
   @Override
   public boolean saveAll(Collection<T> entityList) {
      ArrayList<T> list = new ArrayList<>();
      for (T t : entityList) {
         try {
            //获取属性注解的value值
            Field f = t.getClass().getDeclaredField("id");
            f.setAccessible( true );//设置可以范围private
            Object o = f.get(t);//获取出id的值
            System.out.println(o);
            if (o == null) {
               //如果id为null,插入
               list.add(t);
            } else {
               //否则更新
               baseMapper.updateById(t);
            }
      if (CollUtil.isEmpty(entityList)) {
         return false;
      }
      return baseMapper.insertAll(entityList) == entityList.size();
   }
         } catch (Exception e) {
            e.printStackTrace();
   /**
    * 全量保存或更新 ( 按主键区分 )
    */
   @Override
   public boolean saveOrUpdateAll(Collection<T> entityList) {
      if (CollUtil.isEmpty(entityList)) {
         return false;
      }
      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);
         }
      }
      return baseMapper.insertAll(list) == list.size();
      if (CollUtil.isNotEmpty(updateList) && updateBatchById(updateList)) {
         row += updateList.size();
      }
        if (CollUtil.isNotEmpty(addList)) {
            row += baseMapper.insertAll(addList);
        }
      return row == entityList.size();
   }
   /**
    * 根据 ID 查询
    *
    * @param id 主键ID
    */
   @Override
   public K getVoById(Serializable id, CopyOptions copyOptions) {
      T t = getBaseMapper().selectById(id);
      return BeanCopyUtils.oneCopy(t, copyOptions, voClass);
   public V getVoById(Serializable id) {
        return getBaseMapper().selectVoById(id, voClass);
   }
   /**
    * 查询(根据ID 批量查询)
    *
    * @param idList 主键ID列表
    */
   @Override
   public List<K> listVoByIds(Collection<? extends Serializable> idList, CopyOptions copyOptions) {
      List<T> list = getBaseMapper().selectBatchIds(idList);
      if (list == null) {
         return null;
      }
      return BeanCopyUtils.listCopy(list, copyOptions, voClass);
   public List<V> listVoByIds(Collection<? extends Serializable> idList) {
        return getBaseMapper().selectVoBatchIds(idList, voClass);
   }
   /**
    * 查询(根据 columnMap 条件)
    *
    * @param columnMap 表字段 map 对象
    */
   @Override
   public List<K> listVoByMap(Map<String, Object> columnMap, CopyOptions copyOptions) {
      List<T> list = getBaseMapper().selectByMap(columnMap);
      if (list == null) {
         return null;
      }
      return BeanCopyUtils.listCopy(list, copyOptions, voClass);
   public List<V> listVoByMap(Map<String, Object> columnMap) {
        return getBaseMapper().selectVoByMap(columnMap, voClass);
   }
   /**
    * 根据 Wrapper,查询一条记录 <br/>
    * <p>结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")</p>
    *
    * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
    */
   @Override
   public K getVoOne(Wrapper<T> queryWrapper, CopyOptions copyOptions) {
      T t = getOne(queryWrapper, true);
      return BeanCopyUtils.oneCopy(t, copyOptions, voClass);
   public V getVoOne(Wrapper<T> queryWrapper) {
        return getBaseMapper().selectVoOne(queryWrapper, voClass);
   }
   /**
    * 查询列表
    *
    * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
    */
   @Override
   public List<K> listVo(Wrapper<T> queryWrapper, CopyOptions copyOptions) {
      List<T> list = getBaseMapper().selectList(queryWrapper);
      if (list == null) {
         return null;
      }
      return BeanCopyUtils.listCopy(list, copyOptions, voClass);
   public List<V> listVo(Wrapper<T> queryWrapper) {
        return getBaseMapper().selectVoList(queryWrapper, voClass);
   }
   /**
    * 翻页查询
    *
    * @param page         翻页对象
    * @param queryWrapper 实体对象封装操作类
     * @deprecated 3.6.0 移除 请使用 {@link #pageVo(IPage, Wrapper)}
    */
   @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 = BeanCopyUtils.listCopy(result.getRecords(), copyOptions, voClass);
    @Deprecated
   public PagePlus<T, V> pageVo(PagePlus<T, V> page, Wrapper<T> queryWrapper) {
      PagePlus<T, V> result = getBaseMapper().selectPage(page, queryWrapper);
      List<V> volist = BeanCopyUtils.copyList(result.getRecords(), voClass);
      result.setRecordsVo(volist);
      return result;
   }
    /**
     * 翻页查询
     *
     * @param page         翻页对象
     * @param queryWrapper 实体对象封装操作类
     */
    public <P extends IPage<V>> P pageVo(IPage<T> page, Wrapper<T> queryWrapper) {
        return getBaseMapper().selectVoPage(page, queryWrapper, voClass);
    }
}