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