package com.iailab.module.data.common.utils;
|
|
import org.springframework.stereotype.Component;
|
|
import java.lang.reflect.Field;
|
@Component
|
public class Conversion {
|
public static void convert(Object entity, Object dto) {
|
Class<?> entityClass = entity.getClass();
|
Class<?> dtoClass = dto.getClass();
|
Field[] entityFields = entityClass.getDeclaredFields();
|
Field[] dtoFields = dtoClass.getDeclaredFields();
|
try {
|
for (Field entityField : entityFields) {
|
entityField.setAccessible(true);
|
Object value = entityField.get(entity);
|
String name = entityField.getName();
|
String dtoFieldName = name.substring(0, 1).toUpperCase() + name.substring(1);
|
for (Field dtoField : dtoFields) {
|
dtoField.setAccessible(true);
|
if (dtoField.getName().equals(dtoFieldName)) {
|
dtoField.set(dto, value);
|
break;
|
}
|
}
|
}
|
}catch (Exception e){
|
|
}
|
}
|
}
|