举个例子,下面这个bean
注意==@Data注解 =====>>>>> Lombok的
下面的bean仅提供了部分属性,看看就行
@Data
@Document(collection = "xxx")
public class ItemExam implements Serializable {
/**
* 参加考试人员 默认这个班所有学生
*/
private ArrayList<UserDto> members;
/**
* 成绩可见性 true false【字符串】
*/
private String markVisiblity;
/**
* 试卷可见性 true false 【字符串】
*/
private String testAllVisibility;
/**
* 开启作弊 true false【字符串】
*/
private String formalExam;
}
如果你不想
Logs logs = new Logs();
logs.setxxxx();
logs.setxxxx();
logs.setxxxx(); 一个个的设置的话
虽然下面这种办法也有点麻烦吧,哈哈
仅供参考,就是使用BeanUtils.populate方法
要求你的bean属性要有对应的set方法哦
封装成工具类了:
public class WebBeanUtils {
// 泛型
// 到时候你用什么对象都可以了,所以用了泛型,不局限于某个对象
public static <T> T copyParamToBean(Map properties, T bean) {
try {
// 将所有参数注入到指定对象中去
// 需要对线改的set方法存在 且名字 要一致才行
BeanUtils.populate(bean, properties);
} catch (Exception e) {
e.printStackTrace();
}
return bean;
}
}
Map<String, Object> properties = new HashedMap();
properties.put("testAllVisibility", exam.getTestAllVisibility());
// 试卷成绩可见性
properties.put("markVisiblity", exam.getMarkVisiblity());
// 开启作弊
properties.put("formalExam", formalExam);
// 参加考试学生
ArrayList<UserDto> students = course.getStudents();
properties.put("members", students);
// 参考人数
properties.put("memberSize", students.size())
// 最后这样子就全部注入进去了
WebBeanUtils.copyParamToBean(properties, new ItemExam());
一定要有setter方法哦!否则不会成功!
写完帮到你们!