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); } }
作者:
KMSFan
出处:http://www.cnblogs.com/kmsfan
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
欢迎大家加入KMSFan之家,以及访问我的优酷空间!
出处:http://www.cnblogs.com/kmsfan
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
欢迎大家加入KMSFan之家,以及访问我的优酷空间!