boolean转int类型
工具类
import java.lang.reflect.Field;
public class ConverterUtil {
public static <T, U> void convertBooleanToInt(T source, U target) {
Class<?> sourceClass = source.getClass();
Class<?> targetClass = target.getClass();
Field[] sourceFields = sourceClass.getDeclaredFields();
for (Field field : sourceFields) {
if (field.getType() == boolean.class || field.getType() == Boolean.class) {
String fieldName = field.getName();
String getterName = "is" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
try {
Field targetField = targetClass.getDeclaredField(fieldName);
Class<?> targetFieldType = targetField.getType();
if (targetFieldType == int.class || targetFieldType == Integer.class) {
boolean isAccessible = field.isAccessible();
field.setAccessible(true);
boolean fieldValue = (boolean) field.get(source);
field.setAccessible(isAccessible);
int intValue = fieldValue ? 1 : 0;
boolean isTargetAccessible = targetField.isAccessible();
targetField.setAccessible(true);
targetField.set(target, intValue);
targetField.setAccessible(isTargetAccessible);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
}
}
}
}
}
string 转long
@mapping mapstruct
@Named("stringToLong")
default long stringToLong(String value) {
return Long.parseLong(value);
}
@Mapping(source = "format.id",target = "id",qualifiedByName = "stringToLong")