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;}@Overridepublic 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.populateStudent 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;}@Overridepublic 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.copypropertisStudentVo 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;}@Overridepublic 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;}@Overridepublic 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;}@Overridepublic 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

BeanUtils使用相关推荐

  1. BeanUtils威力和代价

    2019独角兽企业重金招聘Python工程师标准>>> BeanUtils: 威力和代价(转载综合) Apache Jakarta Commons项目非常有用.我曾在许多不同的项目上 ...

  2. 别再用 BeanUtils 了,这款 PO VO DTO 转换神器不香么?

    欢迎关注方志朋的博客,回复"666"获面试宝典 来源:toutiao.com/i6891531055631696395 老铁们是不是经常为写一些实体转换的原始代码感到头疼,尤其是实 ...

  3. 为什么不推荐使用BeanUtils属性转换工具

    点击关注公众号,Java干货及时送达  作者:明明如月学长 blog.csdn.net/w605283073/article/details/107371462 1 背景 之前在专栏中讲过" ...

  4. 我在 Spring 的 BeanUtils 踩到的那些坑,千万不要犯!

    点击关注公众号,Java干货及时送达 转自:绝色天龙 链接:http://www.jianshu.com/p/357b55852efc 背景: 最近项目中在和第三方进行联调一个接口,我们这边发送htt ...

  5. 为什么阿里巴巴禁止使用Apache Beanutils进行属性的copy?

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 在日常开发中,我们经常需要给对象进行赋值,通常会调用其se ...

  6. 还在用 BeanUtils来做对象转换吗?快试试 MapStruct吧

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 作者:阿进的写字台 https://www.cnblogs ...

  7. 丢弃掉那些BeanUtils工具类吧,MapStruct真香!!!

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 在前几天的文章<为什么阿里巴巴禁止使用Apache Bean ...

  8. BeanUtils 是用 Spring 的还是 Apache 的好?

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 来源 | urlify.cn/vUfIry 前言 在我们实际项目开 ...

  9. 两难!到底用Apache BeanUtils还是Spring BeanUtils?

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 在我们实际项目开发过程中,我们经常需要将不同的两个对象实例进行属性 ...

  10. 用Spring的BeanUtils前,建议你先了解这几个坑!

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 来源:http://h5ip.cn/q846 背景 最近项目中在和 ...

最新文章

  1. cmake跨平台编译之判断操作系统平台、32位64位系统
  2. t110ii装系统_DELL T110 II如何安装server2003操作系统
  3. 统计信息自动收集任务失效原因排查
  4. 链表反转python
  5. SAP License:SAP与ORACLE到底谁更强?
  6. 怎样剪立体灯笼_教你怎样做新年DIY剪纸拉花灯笼
  7. 央企如何做好数字化转型战略规划
  8. 实用的Win10各个类型精品软件集锦
  9. JTT808、JTT1078、TJSATL主动安全踩坑记录
  10. 护照扫描仪的应用环境解读 SDK数据
  11. html图片慢慢消失的事件,[Web前端]用javascript实现默认图片替代未显示的图片
  12. android 火车购票功能,基于Android的火车票售票系统的设计与实现.doc
  13. 计算机网络嗅探实验,网络嗅探与欺骗实验
  14. 电脑 显示 无可用电源选项 怎么办
  15. uniapp-Speech语音识别(百度)
  16. 入门 Teams Toolkit
  17. 飞鸽传书linux运行,Linux下如何安装IPtux飞鸽传书
  18. 查询水果价格 (15分)
  19. 浙江省地勘测绘类职称评审的一般路线-整理时间20201106
  20. python画circos图_​用Python把图做的好看点:用Matplotlib画个Circos和弦图

热门文章

  1. 「仅凭照片就能判断一个人是否犯罪」?这样的研究能发表,LeCun、MIT谷歌等机构的1700名研究者怒了...
  2. 说人话,搜代码,Facebook发布神经代码搜索数据集+benchmark
  3. 波士顿动力机器狗测评来了!售价堪比豪车,避障、导航、舞蹈样样都行,买不起还能租...
  4. 华为算力最强AI芯片商用:2倍于英伟达V100!开源AI框架,对标TensorFlow和PyTorch...
  5. 大小端判断和网络字节序
  6. 城市WiFi好看还应该好用
  7. .NET Core程序中使用User Secrets存储敏感数据
  8. R数据可视化--ggplot2定位之坐标系详解
  9. 【Alpha】Daily Scrum Meeting第七次
  10. Linux下的LVM创建以及Linux快照卷