import java.io.Serializable;
public class SelectModel implements Serializable {
private static final long serialVersionUID = 1L;
private Integer value;
private String label;
private String isValid;
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getIsValid() {
return isValid;
}
public void setIsValid(String isValid) {
this.isValid = isValid;
}
}
protected SelectModel toSelectModel(Object obj, String valueName, String labelName, String... fields) {
SelectModel selectModel = new SelectModel();
Class<?> tClass = obj.getClass();
try {
Method methodV = tClass.getMethod("get" + StringUtils.capitalize(valueName), null);
Object value = methodV.invoke(obj, null);
selectModel.setValue((Integer) Optional.ofNullable(value).orElse(0));
Method methodL = tClass.getMethod("get" + StringUtils.capitalize(labelName), null);
Object label = methodL.invoke(obj, null);
selectModel.setLabel(Optional.ofNullable(label).orElse("").toString());
if (fields != null && fields.length > 0) {
BeanWrapper srcWrap = PropertyAccessorFactory.forBeanPropertyAccess(obj);
BeanWrapper trgWrap = PropertyAccessorFactory.forBeanPropertyAccess(selectModel);
Arrays.stream(fields).forEach(f -> trgWrap.setPropertyValue(f, srcWrap.getPropertyValue(f)));
}
return selectModel;
} catch (Exception e) {
logger.error(e.getMessage(), e);
logger.error("系统异常", e);
}
return selectModel;
}
protected List<SelectModel> toSelectModelList(List data, String valueName, String labelName, String... fields) {
if (data != null) {
List<SelectModel> list = new ArrayList<>();
for (Object obj : data) {
list.add(toSelectModel(obj, valueName, labelName, fields));
}
return list;
}
return null;
}
List<SelectModel> list = toSelectModelList(pageListModel.getList(), "areaCode", "areaName", "isValid");
toSelectModel(item, "itemValue", "itemContent")
标签:return,String,value,label,转化,obj,任意,public,字典
From: https://www.cnblogs.com/lovedaodao/p/17361601.html