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; + } + } + + }