From 152781b05131e48bf6e94d71cc72dd54af52a3fb Mon Sep 17 00:00:00 2001 From: houzhongjian <houzhongyi@126.com> Date: 星期四, 10 四月 2025 14:13:29 +0800 Subject: [PATCH] 恢复iailab-framework --- iailab-framework/iailab-common/src/main/java/com/iailab/framework/common/util/object/BeanUtils.java | 69 ++++++++++++++++++++++++++++++++++ 1 files changed, 69 insertions(+), 0 deletions(-) diff --git a/iailab-framework/iailab-common/src/main/java/com/iailab/framework/common/util/object/BeanUtils.java b/iailab-framework/iailab-common/src/main/java/com/iailab/framework/common/util/object/BeanUtils.java new file mode 100644 index 0000000..80c1beb --- /dev/null +++ b/iailab-framework/iailab-common/src/main/java/com/iailab/framework/common/util/object/BeanUtils.java @@ -0,0 +1,69 @@ +package com.iailab.framework.common.util.object; + +import cn.hutool.core.bean.BeanUtil; +import com.iailab.framework.common.pojo.PageResult; +import com.iailab.framework.common.util.collection.CollectionUtils; + +import java.util.List; +import java.util.function.Consumer; + +/** + * Bean 工具类 + * + * 1. 默认使用 {@link cn.hutool.core.bean.BeanUtil} 作为实现类,虽然不同 bean 工具的性能有差别,但是对绝大多数同学的项目,不用在意这点性能 + * 2. 针对复杂的对象转换,可以搜参考 AuthConvert 实现,通过 mapstruct + default 配合实现 + * + * @author iailab + */ +public class BeanUtils { + + public static <T> T toBean(Object source, Class<T> targetClass) { + return BeanUtil.toBean(source, targetClass); + } + + public static <T> T toBean(Object source, Class<T> targetClass, Consumer<T> peek) { + T target = toBean(source, targetClass); + if (target != null) { + peek.accept(target); + } + return target; + } + + public static <S, T> List<T> toBean(List<S> source, Class<T> targetType) { + if (source == null) { + return null; + } + return CollectionUtils.convertList(source, s -> toBean(s, targetType)); + } + + public static <S, T> List<T> toBean(List<S> source, Class<T> targetType, Consumer<T> peek) { + List<T> list = toBean(source, targetType); + if (list != null) { + list.forEach(peek); + } + return list; + } + + public static <S, T> PageResult<T> toBean(PageResult<S> source, Class<T> targetType) { + return toBean(source, targetType, null); + } + + public static <S, T> PageResult<T> toBean(PageResult<S> source, Class<T> targetType, Consumer<T> peek) { + if (source == null) { + return null; + } + List<T> list = toBean(source.getList(), targetType); + if (peek != null) { + list.forEach(peek); + } + return new PageResult<>(list, source.getTotal()); + } + + public static void copyProperties(Object source, Object target) { + if (source == null || target == null) { + return; + } + BeanUtil.copyProperties(source, target, false); + } + +} \ No newline at end of file -- Gitblit v1.9.3