From 7d8dbac52e805c6bdd54b4fb8a1996d757852e2c Mon Sep 17 00:00:00 2001 From: chen-xin-zhi <3588068430@qq.com> Date: Thu, 20 Mar 2025 07:48:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0https?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/common/CommonService.java | 11 +++++++ .../common/impl/CommonServiceImpl.java | 29 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/main/java/com/cultural/heritage/service/common/CommonService.java b/src/main/java/com/cultural/heritage/service/common/CommonService.java index 3edc28e..d19b02a 100644 --- a/src/main/java/com/cultural/heritage/service/common/CommonService.java +++ b/src/main/java/com/cultural/heritage/service/common/CommonService.java @@ -113,5 +113,16 @@ public interface CommonService { + /** + * 复制属性并返回新的目标对象 + * + * @param source 源对象 + * @param targetClass 目标对象的类型 + * @param 源对象类型 + * @param 目标对象类型 + * @return 目标对象 + */ + T copyProperties(S source, Class targetClass); + } diff --git a/src/main/java/com/cultural/heritage/service/common/impl/CommonServiceImpl.java b/src/main/java/com/cultural/heritage/service/common/impl/CommonServiceImpl.java index 76f48d5..4148913 100644 --- a/src/main/java/com/cultural/heritage/service/common/impl/CommonServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/common/impl/CommonServiceImpl.java @@ -235,4 +235,33 @@ public class CommonServiceImpl implements CommonService { } + + + + /** + * 复制属性并返回新的目标对象 + * + * @param source 源对象 + * @param targetClass 目标对象的类型 + * @param 源对象类型 + * @param 目标对象类型 + * @return 目标对象 + */ + public T copyProperties(S source, Class targetClass) { + try { + if (source == null || targetClass == null) { + return null; + } + // 创建目标对象 + T target = targetClass.getDeclaredConstructor().newInstance(); + // 复制属性 + BeanUtils.copyProperties(source, target); + return target; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + }