将行数据转换成Java(POJO)对象
2020-12-13 05:45
标签:rop time roc 转换 runtime ext utils src prope 工作中经常会遇到将行数据转换成Java(POJO)对象的场景,其中关于字段校验和类型转换的处理繁琐而冗余,对于有代码洁癖的人着实不能忍。这里分享下自己封装的工具代码,也许能够帮助你更简单地完成此类任务。 先将以下五个文件加入你豪华午餐(项目??)中 FieldItem: FieldProcessor: FieldValidator: ConvertException: RowConverter: 下面我们就可以愉快的写代码了: 目标类User: 测试类Test: 看上去是不是好多了?? 将行数据转换成Java(POJO)对象 标签:rop time roc 转换 runtime ext utils src prope 原文地址:https://www.cnblogs.com/lichmama/p/11148518.html/**
*
* @author lichmama
*
*/
public class FieldItem {
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public FieldProcessor getProcessor() {
return processor;
}
public void setProcessor(FieldProcessor processor) {
this.processor = processor;
}
public void setValidator(FieldValidator validator) {
this.validator = validator;
}
public FieldValidator getValidator() {
return validator;
}
public boolean validate() {
return validator.validate(this);
}
public Object process() throws Exception {
return processor.process(this);
}
private int index;
private String name;
private Object value;
private FieldValidator validator = new FieldValidator() {
@Override
public boolean validate(FieldItem field) {
return true;
}
};
private FieldProcessor processor = new FieldProcessor() {
@Override
public Object process(FieldItem field) {
return field.getValue();
}
};
public FieldItem() {
}
public FieldItem(int index, String name) {
this.index = index;
this.name = name;
}
public FieldItem(int index, String name, FieldValidator validator) {
this.index = index;
this.name = name;
this.validator = validator;
}
public FieldItem(int index, String name, FieldProcessor processor) {
this.index = index;
this.name = name;
this.processor = processor;
}
public FieldItem(int index, String name, FieldValidator validator, FieldProcessor processor) {
this.index = index;
this.name = name;
this.validator = validator;
this.processor = processor;
}
}
/**
* 扩展接口:可针对字段做个性化处理
*
* @author lichmama
*
*/
public interface FieldProcessor {
Object process(FieldItem field) throws Exception;
}
/**
* 校验接口:可针对字段做个性化校验
*
* @author lichmama
*
*/
public interface FieldValidator {
boolean validate(FieldItem field);
}
/**
* 转换异常
*
* @author lichmama
*
*/
public class ConvertException extends RuntimeException {
public ConvertException(String msg) {
super(msg);
}
public ConvertException(Throwable throwable) {
super(throwable);
}
public ConvertException(String msg, Throwable throwable) {
super(msg, throwable);
}
}
/**
* 行转换器:用于将行数据转换成bean
*
* @author lichmama
*
*/
public class RowConverter {
private Map
public class User implements Serializable {
private int id;
private String name;
private String gender;
private int age;
private String birthday;
// 省略getter、setter
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", gender=" + gender + ", age=" + age + ", birthday=" + birthday
+ "]";
}
}
public class Test {
public static void main(String[] args) throws Exception {
String[] fieldNames = { "id", "name", "gender", "age", "birthday" };
Object[] row = { 1, "lichmama", "man", 17, new Date() };
RowConverter converter = new RowConverter(fieldNames);
testValidator(row, converter);
testProcessor(row, converter);
}
/**
* 字段个性化校验
*
* @param row
* @param converter
* @throws Exception
*/
public static void testValidator(Object[] row, RowConverter converter) throws Exception {
converter.setFieldValidator("age", (FieldItem field) -> {
int age = (int) field.getValue();
return age >= 18; // 未成年禁止入内??
});
User user = converter.convert(row, User.class);
}
/**
* 字段个性化处理
*
* @param row
* @param converter
* @throws Exception
*/
public static void testProcessor(Object[] row, RowConverter converter) throws Exception {
converter.setFieldProcessor("birthday", (FieldItem field) -> {
return new SimpleDateFormat("yyyy-MM-dd").format(field.getValue()); // 将日期转换成格式化字符串
});
User user = converter.convert(row, User.class);
}
}