diff --git a/src/main/java/io/github/talelin/latticy/common/util/BeanCopyUtil.java b/src/main/java/io/github/talelin/latticy/common/util/BeanCopyUtil.java new file mode 100644 index 00000000..92607f9e --- /dev/null +++ b/src/main/java/io/github/talelin/latticy/common/util/BeanCopyUtil.java @@ -0,0 +1,138 @@ +package io.github.talelin.latticy.common.util; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.BeanUtils; +import org.springframework.util.CollectionUtils; +import org.springframework.util.ReflectionUtils; + +import java.util.*; +import java.util.function.Supplier; +import java.util.stream.Collectors; + +/** + * @author Gadfly + */ +@Slf4j +public class BeanCopyUtil extends BeanUtils { + public static void copyNonNullProperties(Object source, Object target) { + String[] properties = Arrays.stream(ReflectionUtils.getDeclaredMethods(source.getClass())) + .map(method -> { + if (method.getName().startsWith("get")) { + Object fieldValue = null; + try { + fieldValue = method.invoke(source); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + if (fieldValue == null) { + String fieldName = method.getName().substring(3); + return com.baomidou.mybatisplus.core.toolkit.StringUtils.firstToLowerCase(fieldName); + } + } + return null; + }) + .filter(Objects::nonNull) + .toArray(String[]::new); + copyProperties(source, target, properties); + } + + /** + * 集合数据的拷贝 + * + * @param source: 数据源类 + * @param target: 目标类::new(eg: UserVO::new) + * @return 拷贝后的集合 + */ + public static T copyProperties(S source, Supplier target) { + T t = target.get(); + copyProperties(source, t); + return t; + } + + /** + * 集合数据的拷贝 + * + * @param sources: 数据源类 + * @param target: 目标类::new(eg: UserVO::new) + * @return 拷贝后的集合 + */ + public static List copyListProperties(List sources, Supplier target) { + return copyListProperties(sources, target, null); + } + + /** + * 带回调函数的集合数据的拷贝(可自定义字段拷贝规则) + * + * @param sources: 数据源类 + * @param target: 目标类::new(eg: UserVO::new) + * @param callBack: 回调函数 + * @return 拷贝后的集合 + */ + public static List copyListProperties(List sources, Supplier target, + BeanCopyUtilCallBack callBack) { + if (CollectionUtils.isEmpty(sources)) { + return new ArrayList<>(); + } + List list = new ArrayList<>(sources.size()); + for (S source : sources) { + T t = target.get(); + copyProperties(source, t); + list.add(t); + if (null != callBack) { + // 回调 + callBack.callBack(source, t); + } + } + return list; + } + + public static T copySingleProperties(S source, Supplier target, BeanCopyUtilCallBack callBack) { + T t = target.get(); + copyProperties(source, t); + if (null != callBack) { + // 回调 + callBack.callBack(source, t); + } + return t; + } + + /** + * 将集合对象中的类型转换成另一种类型 + * + * @param collection 集合 + * @param clazz 目标对象 + * @return 转换后的集合 + */ + public static Collection covertObject(Collection collection, Class clazz, + BeanCopyUtilCallBack callBack) { + if (CollectionUtils.isEmpty(collection)) { + return new ArrayList<>(); + } + return collection.stream().map(oldObject -> { + T instance = null; + try { + instance = clazz.getDeclaredConstructor().newInstance(); + copyProperties(oldObject, instance); + if (null != callBack) { + // 回调 + callBack.callBack(oldObject, instance); + } + } catch (Exception e) { + log.error(e.getMessage(), e); + } + return instance; + }).collect(Collectors.toList()); + } + + @FunctionalInterface + public interface BeanCopyUtilCallBack { + + /** + * 定义默认回调方法 + * + * @param t target + * @param s source + */ + void callBack(S s, T t); + } +} diff --git a/src/main/java/io/github/talelin/latticy/service/impl/UserServiceImpl.java b/src/main/java/io/github/talelin/latticy/service/impl/UserServiceImpl.java index 5dde38bd..5db15eef 100644 --- a/src/main/java/io/github/talelin/latticy/service/impl/UserServiceImpl.java +++ b/src/main/java/io/github/talelin/latticy/service/impl/UserServiceImpl.java @@ -12,6 +12,7 @@ import io.github.talelin.latticy.common.configuration.LoginCaptchaProperties; import io.github.talelin.latticy.common.enumeration.GroupLevelEnum; import io.github.talelin.latticy.common.mybatis.Page; +import io.github.talelin.latticy.common.util.BeanCopyUtil; import io.github.talelin.latticy.common.util.CaptchaUtil; import io.github.talelin.latticy.dto.user.ChangePasswordDTO; import io.github.talelin.latticy.dto.user.RegisterDTO; @@ -114,20 +115,7 @@ public UserDO updateUserInfo(UpdateInfoDTO dto) { user.setUsername(dto.getUsername()); } } - - // todo 增加工具类实现忽略 null 的 BeanCopy,简化这段代码 - if (dto.getUsername() != null) { - user.setUsername(dto.getUsername()); - } - if (dto.getAvatar() != null) { - user.setAvatar(dto.getAvatar()); - } - if (dto.getEmail() != null) { - user.setEmail(dto.getEmail()); - } - if (dto.getNickname() != null) { - user.setNickname(dto.getNickname()); - } + BeanCopyUtil.copyNonNullProperties(dto, user); this.baseMapper.updateById(user); return user;