• BeanCopyUtil


    import com.baomidou.mybatisplus.extension.service.IService;
    import org.apache.commons.collections4.CollectionUtils;
    import org.springframework.beans.BeanUtils;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.function.Supplier;
    
    
    
    /**
     * @author
     * @date
     */
    public class BeanCopyUtil extends BeanUtils {
       private static final int ZERO = 0;
        /**
         * 集合数据的拷贝
         *
         * @param sources: 数据源类
         * @param target:  目标类::new(eg: UserVO::new)
         * @return
         */
        public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target) {
            return copyListProperties(sources, target, null);
        }
    
    
        /**
         * 带回调函数的集合数据的拷贝(可自定义字段拷贝规则)
         *
         * @param sources:  数据源类
         * @param target:   目标类::new(eg: UserVO::new)
         * @param callBack: 回调函数
         * @return
         */
        public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target, BeanCopyUtilCallBack<S, T> callBack)  {
            List<T> list = new ArrayList<>(sources.size());
            for (S source : sources) {
                T t = target.get();
                copyProperties(source, t);
                list.add(t);
                if (callBack != null) {
                    // 回调
                    callBack.callBack(source, t);
                }
            }
            return list;
        }
    
        public static void saveBatch(List list, IService iService) {
            if (CollectionUtils.isNotEmpty(list)) {
                int pageSize = 1000;
                int pageNum = 1;
                int total = list.size();
                int pages = total / pageSize;
                if (total % pageSize != ZERO) {
                    pages++;
                }
                while (pages >= pageNum) {
                    //默认1000,为什么用1000?相信应该比较合理,故我们也用1000
                    iService.saveBatch(list.subList(BeanCopyUtil.getStartIndex(pageNum, pageSize), BeanCopyUtil.getLastIndex(pageNum, pageSize, total)));
                    pageNum++;
                }
            }
        }
    
        public static void saveOrUpdateBatch(List list, IService iService) {
            if (CollectionUtils.isNotEmpty(list)) {
                int pageSize = 1000;
                int pageNum = 1;
                int total = list.size();
                
                int pages = total / pageSize;
                if (total % pageSize != ZERO) {
                    pages++;
                }
                while (pages >= pageNum) {
                    //默认1000,为什么用1000?相信应该比较合理,故我们也用1000
                    iService.saveOrUpdateBatch(list.subList(BeanCopyUtil.getStartIndex(pageNum, pageSize), BeanCopyUtil.getLastIndex(pageNum, pageSize, total)));
                    pageNum++;
                }
            }
        }
    
        /**
         * 计算集合起始位置与结束位置
         */
        public static int getStartIndex(int pageNum, int pageSize) {
            return (pageNum - 1) * pageSize;
        }
    
        public static int getLastIndex(int pageNum, int pageSize, int total) {
            int num = pageNum * pageSize;
            return Math.min(num, total);
        }
    }
    
    @FunctionalInterface
    public interface BeanCopyUtilCallBack<S, T> {
        /**
         * 定义默认回调方法
         *
         * @param t
         * @param s
         */
        void callBack(S t, T s) ;
    }
  • 相关阅读:
    [洛谷][P1503][鬼子进村][Treap]
    [noi 2004] 郁闷的出纳员
    bzoj 3224,tyvj 1728普通平衡树
    Treap
    [模拟赛]棘手的操作
    bzoj 4551[Tjoi2016&Heoi2016]树
    bzoj2527 [Poi2011]Meteors
    bzoj4152 [AMPPZ2014]The Captain
    bzoj4516 [Sdoi2016]生成魔咒
    bzoj4547 小奇的集合
  • 原文地址:https://www.cnblogs.com/tiancai/p/16138904.html
Copyright © 2020-2023  润新知