BeanUtils.copyProperties()-个人总结
来源:互联网 发布:行知职业技术学校 编辑:程序博客网 时间:2024/05/22 12:45
实体类
package com.zdc.entity;import java.math.BigDecimal;import java.util.Date;public class Employee {private String id;private String name;private Date birthday;private Integer age;private Boolean gender;private Double myDouble;private Short myShort;private char mychar;private float myfloat;private Object myObj;private Date myDate;private BigDecimal myBigDecimal;public Employee(){}@Overridepublic String toString() {return "Employee [id=" + id + ", name=" + name + ", birthday="+ birthday + ", age=" + age + ", gender=" + gender+ ", myDouble=" + myDouble + ", myShort=" + myShort+ ", mychar=" + mychar + ", myfloat=" + myfloat + ", myObj="+ myObj + ", myDate=" + myDate + ", myBigDecimal="+ myBigDecimal + "]";}public Employee(String id, String name, Date birthday, Integer age,Boolean gender, Double myDouble, Short myShort) {super();this.id = id;this.name = name;this.birthday = birthday;this.age = age;this.gender = gender;this.myDouble = myDouble;this.myShort = myShort;}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 Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Boolean getGender() {return gender;}public void setGender(Boolean gender) {this.gender = gender;}public Double getMyDouble() {return myDouble;}public void setMyDouble(Double myDouble) {this.myDouble = myDouble;}public Short getMyShort() {return myShort;}public void setMyShort(Short myShort) {this.myShort = myShort;}public char getMychar() {return mychar;}public void setMychar(char mychar) {this.mychar = mychar;}public float getMyfloat() {return myfloat;}public void setMyfloat(float myfloat) {this.myfloat = myfloat;}public Object getMyObj() {return myObj;}public void setMyObj(Object myObj) {this.myObj = myObj;}public Date getMyDate() {return myDate;}public void setMyDate(Date myDate) {this.myDate = myDate;}public BigDecimal getMyBigDecimal() {return myBigDecimal;}public void setMyBigDecimal(BigDecimal myBigDecimal) {this.myBigDecimal = myBigDecimal;}}
Srping 测试 和总结
package com.zdc.test;import java.lang.reflect.InvocationTargetException;import java.util.Date;import org.apache.commons.beanutils.PropertyUtils;import org.springframework.beans.BeanUtils;import com.zdc.entity.Employee;/** * 关于使用BeanUtils.copyProperties 的总结 * 1. 引入的包不同,使用方法不同 * 提供BeanUtils包 本人暂时知道的有两个 : Spring提供 和 apache 提供 * Spring提供的 BeanUtils 不需要考虑空值问题 * apache 提供的需要考虑空值 尤其是 java.util.Date, java.math.BigDecimal 等类型。如果是空值得话,就会抛出异常 * 因此需要 使用BeanUtilsBean 进行转换处理 * 2. BeanUtils.copyProperties(这里指的是apache 提供的) 与 PropertyUtils.copyProperties() 比较 * PropertyUtils.copyProperties() 仅仅对属性和名称进行转换 ,如果类型匹配不成功,就会抛出异常 * 效率方面 PropertyUtils.copyProperties() 的效率要比 BeanUtils.copyProperties() 强的多 * 3.灵活度上,个人认为apache 提供的稍微好一点,只要继承BeanUtilsBean 就可以实现对一般需要的处理 * * * * @author * */public class copyBeanUtilsBySpring {public static void main(String[] args) {Employee sourceEmployee=new Employee("122", "zhangsan",new Date(), 23, true, 3.3, (short)1);Employee newEmployee=new Employee();Employee propertyUtilsEmployee=new Employee();BeanUtils.copyProperties(sourceEmployee, newEmployee);System.out.println(newEmployee.toString());try {PropertyUtils.copyProperties(propertyUtilsEmployee, sourceEmployee);System.out.println(propertyUtilsEmployee.toString());} catch (IllegalAccessException | InvocationTargetException| NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
apache提供
package com.zdc.test;import java.lang.reflect.InvocationTargetException;import java.math.BigDecimal;import java.util.Date;import org.apache.commons.beanutils.BeanUtilsBean;import com.zdc.entity.Employee;public class copyJavaBean { public static void main(String[] args) {Employee sourceEmployee=new Employee("122", "zuodengchao",new Date(), 23, true, 3.3, (short)1);Employee newEmployee=new Employee();BeanUtilsBean beanUtils=new BeanUtilsBean();beanUtils.getConvertUtils().register(new org.apache.commons.beanutils .converters.BigDecimalConverter(null), BigDecimal.class);beanUtils.getConvertUtils().register(new org.apache.commons.beanutils.converters.DateConverter(null), java.util.Date.class);try {beanUtils.copyProperties(newEmployee, sourceEmployee);} catch (IllegalAccessException | InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(newEmployee.toString());System.out.println("执行成功");}}
0 0
- BeanUtils.copyProperties()-个人总结
- beanUtils.copyProperties
- BeanUtils.copyProperties
- BeanUtils.copyProperties
- BeanUtils.copyProperties()
- BeanUtils.copyProperties
- BeanUtils.copyProperties&&PropertyUtils.copyProperties
- BeanUtils.copyProperties()与PropertyUtils.copyProperties()
- BeanUtils.copyProperties() 用法
- BeanUtils.copyProperties() 用法
- BeanUtils.copyProperties()的用法
- BeanUtils.copyProperties()方法
- BeanUtils.copyProperties()的用法
- BeanUtils.copyProperties()的用法
- BeanUtils.copyProperties()的用法
- BeanUtils.copyProperties()的用法
- BeanUtils.copyProperties() 用法
- BeanUtils.copyProperties()的用法
- 模仿qq界面实现(WTL)
- java查询文件大小写路径问题
- 进程和线程之由来
- 网校平台,点播录播直播社区一样不能少!
- form表单的两种提交方式,submit和button的用法
- BeanUtils.copyProperties()-个人总结
- 和最大连续子数组
- TPS和事务响应时间的关系、计算公式
- maven install时报:Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME
- JDK1.8环境下导入项目到Eclipse中报错,提示要移除“@Override” 注解,报错原因与解决方法
- python精句收集
- Codeforces Round #390 (Div. 2)D Fedor and coupons
- Spring的《XML显式装配bean》- XML自动装配
- android下载新版本并更新(DownLoadManager、HttpURLConnection)