BeanUtils使用

1.BeanUtils.populate

可以把一个map中的属性拷贝到实体javaBean,例子:

Student:

package com.cy.model;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle; public class Student{
private String id;
private String name;
private String stuNo;
private String address; public Student(){ }
public Student(String id, String name, String stuNo, String address) {
this.id = id;
this.name = name;
this.stuNo = stuNo;
this.address = address;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getStuNo() {
return stuNo;
} public void setStuNo(String stuNo) {
this.stuNo = stuNo;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
package com.cy.test;

import com.cy.model.Student;
import org.apache.commons.beanutils.BeanUtils;
import java.util.HashMap;
import java.util.Map; public class BeanUtilTest { public static void main(String[] args) throws Exception {
//1.BeanUtils.populate
Student s1 = new Student();
Map<String, String> map = new HashMap<String, String>();
map.put("id", "1");
map.put("name", "zhangsan");
BeanUtils.populate(s1, map);
System.out.println(s1);
}
}
Student[id=1,name=zhangsan,stuNo=<null>,address=<null>]

2.BeanUtils.copypropertis(Object dest, Object orig)

将源实体属性拷贝到目标dest属性中。

StudentVo:

package com.cy.vo;

import com.cy.model.Student;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle; public class StudentVo {
private String name;
private String stuNo; public StudentVo(){ } public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getStuNo() {
return stuNo;
} public void setStuNo(String stuNo) {
this.stuNo = stuNo;
} @Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
package com.cy.test;

import com.cy.model.Student;
import com.cy.vo.StudentVo;
import org.apache.commons.beanutils.BeanUtils; import java.util.HashMap;
import java.util.Map; public class BeanUtilTest { public static void main(String[] args) throws Exception {
Student s = new Student();
s.setId("1");
s.setName("xiaoming");
s.setStuNo("001"); //2.BeanUtils.copypropertis
StudentVo sv = new StudentVo();
BeanUtils.copyProperties(sv, s);
System.out.println(sv);
}
}
StudentVo[name=xiaoming,stuNo=001]

3.但是如果两个类的属性名不一样,就不能复制属性了。如:

StudentDto:

package com.cy.dto;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle; public class StudentDto {
private String student_id;
private String name;
private String stu_no;
private String address; public String getStudent_id() {
return student_id;
} public void setStudent_id(String student_id) {
this.student_id = student_id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getStu_no() {
return stu_no;
} public void setStu_no(String stu_no) {
this.stu_no = stu_no;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
package com.cy.test;

import com.cy.dto.StudentDto;
import com.cy.model.Student;
import com.cy.vo.StudentVo;
import org.apache.commons.beanutils.BeanUtils; import java.util.HashMap;
import java.util.Map; public class BeanUtilTest { public static void main(String[] args) throws Exception {
StudentDto st = new StudentDto();
st.setStudent_id("1");
st.setStu_no("001");
st.setName("xiaoming");
st.setAddress("beijing"); Student s = new Student();
BeanUtils.copyProperties(s, st);
System.out.println(s);
}
}
Student[id=<null>,name=xiaoming,stuNo=<null>,address=beijing]

4.属性类型不一样,也是可以转化成功的:

StudentDto:

package com.cy.dto;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle; public class StudentDto {
private Integer id;
private String name;
private String stu_no;
private String address;
private Boolean good; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public Boolean getGood() {
return good;
} public void setGood(Boolean good) {
this.good = good;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getStu_no() {
return stu_no;
} public void setStu_no(String stu_no) {
this.stu_no = stu_no;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}

Student:

package com.cy.model;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle; public class Student{
private String id;
private String name;
private String stuNo;
private String address;
private String good; public Student(){ } public String getGood() {
return good;
} public void setGood(String good) {
this.good = good;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getStuNo() {
return stuNo;
} public void setStuNo(String stuNo) {
this.stuNo = stuNo;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
package com.cy.test;

import com.cy.dto.StudentDto;
import com.cy.model.Student;
import com.cy.vo.StudentVo;
import org.apache.commons.beanutils.BeanUtils; import java.util.HashMap;
import java.util.Map; public class BeanUtilTest { public static void main(String[] args) throws Exception {
StudentDto st = new StudentDto();
st.setId(1);
st.setStu_no("001");
st.setName("xiaoming");
st.setAddress("beijing");
st.setGood(true); Student s = new Student();
BeanUtils.copyProperties(s, st);
System.out.println(s);
}
}
Student[id=1,name=xiaoming,stuNo=<null>,address=beijing,good=true]
上一篇:COGS182 [USACO Jan07] 均衡队形[RMQ]


下一篇:linux下安装rabbitmq 集群