有的时候我们需要将一个bean的属性赋值到另一个bean的属性中,比如用户提交过来的表单bean需要将该bean中的属性值赋值到对应的数据库bean,我们通常会以这样的方式做:
1 User user = new User("1", "Jon_China", "123456", "1992-11-17", "127.0.0.1"); 2 TargetUser target = new TargetUser(); 3 target.setId(user.getId()); 4 target.setUserName(user.getUserName()); 5 target.setPassword(user.getPassword());
当bean的属性过多时是不是感觉“嘿打脑壳”。不要紧,Apache提供bean的copy工具BeanUtil.
当然是用也是比较简单的,楼主一步一步说明:
1、先搞两个bean
package com.jon.china.beancopy; //假设是用户提交过来的bean public class User { private String Id; private String userName; private String password; private String birthday; private String loginIp; public String getId() { return Id; } public void setId(String id) { Id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getLoginIp() { return loginIp; } public void setLoginIp(String loginIp) { this.loginIp = loginIp; } public User(String id, String userName, String password, String birthday, String loginIp) { Id = id; this.userName = userName; this.password = password; this.birthday = birthday; this.loginIp = loginIp; } }
定义目标bean,目的将User属性值复制到TargetUser中,需要注意的是属性birthday类型的变化。
package com.jon.china.beancopy; import java.util.Date; public class TargetUser { private String Id; private String userName; private String password; private Date birthday; private String loginIp; public String getId() { return Id; } public void setId(String id) { Id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getLoginIp() { return loginIp; } public void setLoginIp(String loginIp) { this.loginIp = loginIp; } }
2、由于birthday需要将String转换为Date类型,这里定义个转换器,实现Converter接口:
package com.jon.china.beancopy; import java.text.ParseException; import java.text.SimpleDateFormat; import org.apache.commons.beanutils.Converter; public class BeanConvert implements Converter { /** * 日期类型转换器 */ @Override public Object convert(Class type, Object value) { if(value==null){ return null; } String v = (String) value; if("".equals(v.trim())){ return null; } SimpleDateFormat sf = new SimpleDateFormat("yyyy-mm-dd"); try { return sf.parse(v); } catch (ParseException e) { throw new RuntimeException(e); } } }
3、初始化User,注册转换器,开始转换:
package com.jon.china.beancopy; import java.util.Date; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.ConvertUtils; public class Test { public static void main(String[] args) { User user = new User("1", "Jon_China", "123456", "1992-11-17", "127.0.0.1"); TargetUser target = new TargetUser(); //注册日期转换器 ConvertUtils.register(new BeanConvert(), Date.class); try { BeanUtils.copyProperties(target, user);//开始复制bean } catch (Exception e) { throw new RuntimeException(e); } System.out.println(target.getLoginIp()); } }
4、成功,结束。
源码地址:https://github.com/LJunChina/BeanCopy
时间: 2024-10-29 00:19:06