zoukankan      html  css  js  c++  java
  • 【工具】

    public class BeanPlusUtils extends BeanUtils {
        public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target) {
            return copyListProperties(sources, target, null);
        }
    
        public static <S, T> T copySingleProperties(S source, Supplier<T> target) {
            return copySingleProperties(source, target, null);
        }
    
        /**
         * 使用场景:Entity、Bo、Vo层数据的复制,因为BeanUtils.copyProperties只能给目标对象的属性赋值,却不能在List集合下循环赋值,因此添加该方法
         * 如:List<AdminEntity> 赋值到 List<AdminVo> ,List<AdminVo>中的 AdminVo 属性都会被赋予到值
         * S: 数据源类 ,T: 目标类::new(eg: AdminVo::new)
         */
        public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target,
                                                        ColaBeanUtilsCallBack<S, T> callBack) {
            if (CollectionUtils.isEmpty(sources)){
                return new ArrayList<>();
            }
            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 <S, T> T copySingleProperties(S source, Supplier<T> target, ColaBeanUtilsCallBack<S, T> callBack) {
            T t = target.get();
            copyProperties(source, t);
            if (callBack != null) {
                // 回调
                callBack.callBack(source, t);
            }
            return t;
        }
    
        @FunctionalInterface
        public interface ColaBeanUtilsCallBack<S, T> {
    
            void callBack(S t, T s);
        }
    
        /**
         * 将集合对象中的类型转换成另一种类型
         *
         * @param collection 集合
         * @param clazz      目标对象
         * @param <T>
         * @return
         */
        public static <T> Collection<T> covertObject(Collection<?> collection, Class<T> clazz) {
            Collection<T> newCollection = collection.stream().map(oldObject -> {
                T instance = null;
                try {
                    instance = clazz.newInstance();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                BeanUtils.copyProperties(oldObject, instance);
                return instance;
            }).collect(Collectors.toList());
            return newCollection;
        };
    }
  • 相关阅读:
    django页面分类和继承
    django前端从数据库获取请求参数
    pycharm配置django工程
    django 应用各个py文件代码
    CF. 1428G2. Lucky Numbers(背包DP 二进制优化 贪心)
    HDU. 6566. The Hanged Man(树形背包DP DFS序 重链剖分)
    小米邀请赛 决赛. B. Rikka with Maximum Segment Sum(分治 决策单调性)
    区间树 学习笔记
    CF GYM. 102861M. Machine Gun(主席树)
    2016-2017 ACM-ICPC East Central North America Regional Contest (ECNA 2016) (B, D, G, H)
  • 原文地址:https://www.cnblogs.com/lycsmzl/p/13391758.html
Copyright © 2011-2022 走看看