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);
    }
}
View Code
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);
    }
}
View Code
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);
    }
}
View Code
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);
    }
}
View Code

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);
    }
}
View Code
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);
    }
}
View Code
Student[id=1,name=xiaoming,stuNo=<null>,address=beijing,good=true]

 

转载于:https://www.cnblogs.com/tenWood/p/10517471.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值