关闭页面特效

DTO转DOMAIN动态转换类。

package dtotransfer.util; import dtotransfer.annotation.DomainField; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; public class DtoFieldConveter { public static Object fill(Object source, Class<?> targetClass) throws Exception { Class<?> sourceCls = source.getClass(); Field[] sourceFields = sourceCls.getDeclaredFields(); Field[] targetFields = targetClass.getDeclaredFields(); Object targetObject = targetClass.newInstance(); for (Field sourceField : sourceFields) { Annotation[] sourceFieldAnnotations = sourceField.getAnnotations(); for (Annotation sourceFieldAnno : sourceFieldAnnotations) { if (sourceFieldAnno.annotationType() == DomainField.class) { DomainField anno = (DomainField) sourceFieldAnno; if (anno.targetClass() == targetClass) { for (Field targetField : targetFields) { if (targetField.getName().equals(anno.targetProperty())) { Method sourceMethod = sourceCls.getMethod("get" + sourceField.getName() .substring(0, 1).toUpperCase() + sourceField.getName().substring(1)); Method targetMethod = targetClass.getMethod("set" + targetField.getName() .substring(0, 1).toUpperCase() + targetField.getName().substring(1),sourceField.getType()); Object sourceFieldValue = sourceMethod.invoke(source); targetMethod.invoke(targetObject, sourceFieldValue); // targetField.setAccessible(true); // targetField.set(targetObject, sourceField.get(source)); } } } } } } return targetObject; } }
package dtotransfer.annotation; import java.lang.annotation.Retention; import java.lang.annotation.Target; import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.RetentionPolicy.RUNTIME; @Target({FIELD}) @Retention(RUNTIME) public @interface DomainField { Class<?> targetClass(); String targetProperty(); // String [] targetProperties(); }

把DTO实体和DOMAIN实体映射起来,字段名称可不同。

 

package dtotransfer.dto; import dtotransfer.annotation.DomainField; import dtotransfer.domain.AdminActivity; public class AddTenantInfoRequestDTO { @DomainField(targetClass = AdminActivity.class, targetProperty = "description") private String tenantName; @DomainField(targetClass = AdminActivity.class, targetProperty = "ip") private String businessName; private String principal; private String principalPhone; public String getTenantName() { return tenantName; } public void setTenantName(String tenantName) { this.tenantName = tenantName; } public String getBusinessName() { return businessName; } public void setBusinessName(String businessName) { this.businessName = businessName; } public String getPrincipal() { return principal; } public void setPrincipal(String principal) { this.principal = principal; } public String getPrincipalPhone() { return principalPhone; } public void setPrincipalPhone(String principalPhone) { this.principalPhone = principalPhone; } }
package dtotransfer.util; import dtotransfer.domain.AdminActivity; import dtotransfer.dto.AddTenantInfoRequestDTO; public class DtoDomainMapping { public static void main(String []args) throws Exception { AddTenantInfoRequestDTO requestDTO=new AddTenantInfoRequestDTO(); requestDTO.setBusinessName("yang"); requestDTO.setTenantName("127.0.0.1"); Object object= DtoFieldConveter.fill(requestDTO,AdminActivity.class); } }

 


__EOF__

作  者ღKawaii
出  处https://www.cnblogs.com/kmsfan/p/10312908.html
关于博主:一个普通的小码农,为了梦想奋斗
版权声明:署名 - 非商业性使用 - 禁止演绎,协议普通文本 | 协议法律文本
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!

posted @   yangliwen  阅读(1668)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
历史上的今天:
2016-01-24 redis命令1
0
0
关注
跳至底部
document.getElementById("homeTopTitle").innerText="ღKawaii";
点击右上角即可分享
微信分享提示