BeanUtils工具的使用
beanutils的下载地址:http://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi
该压缩包有测试的代码:
我们就使用其中一个来作为测试使用
接下来就对beanutils进行测试:
数据模型 user.java:
1 package learning_something; 2 3 public class User { 4 private String username; 5 private String email; 6 private String password; 7 private String name; 8 private String phone; 9 private String address; 10 private boolean isadmin; 11 private boolean isvalidate; 12 public String getUsername() { 13 return username; 14 } 15 public void setUsername(String username) { 16 this.username = username; 17 } 18 public String getEmail() { 19 return email; 20 } 21 public void setEmail(String email) { 22 this.email = email; 23 } 24 public String getPassword() { 25 return password; 26 } 27 public void setPassword(String password) { 28 this.password = password; 29 } 30 public String getName() { 31 return name; 32 } 33 public void setName(String name) { 34 this.name = name; 35 } 36 public String getPhone() { 37 return phone; 38 } 39 public void setPhone(String phone) { 40 this.phone = phone; 41 } 42 public String getAddress() { 43 return address; 44 } 45 public void setAddress(String address) { 46 this.address = address; 47 } 48 public boolean isIsadmin() { 49 return isadmin; 50 } 51 public void setIsadmin(boolean isadmin) { 52 this.isadmin = isadmin; 53 } 54 public boolean isIsvalidate() { 55 return isvalidate; 56 } 57 public void setIsvalidate(boolean isvalidate) { 58 this.isvalidate = isvalidate; 59 } 60 public User(String username, String email, String password, String name, String phone, String address, 61 boolean isadmin, boolean isvalidate) { 62 super(); 63 this.username = username; 64 this.email = email; 65 this.password = password; 66 this.name = name; 67 this.phone = phone; 68 this.address = address; 69 this.isadmin = isadmin; 70 this.isvalidate = isvalidate; 71 } 72 73 74 public User(String username, String email, String password, String name, String phone, String address) { 75 76 this.username = username; 77 this.email = email; 78 this.password = password; 79 this.name = name; 80 this.phone = phone; 81 this.address = address; 82 this.isadmin = false; 83 this.isvalidate = false; 84 } 85 public User() { 86 super(); 87 } 88 89 90 91 92 93 94 }
bean中主要的类是BeanUtils,如图:
BeanUtils类的作用:把一个Map集合内的数据进行组装到一个Javabean中,Javabean指的就是数据模型
运行时出现的错误:
说明还需要下载一个logging的包,是一个作为输出的包
下载地址:http://commons.apache.org/proper/commons-logging/download_logging.cgi
加入该包
数据模型user.java没有进行重写方法出现的结果:
进行重写了之后:
第一行显示结果表示没有使用BeanUtils,第二行是使用了BeanUtils出现的结果:
package learning_something; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; import org.apache.commons.beanutils.BeanUtils; public class TestBeanUtils { public static void main(String[] args) throws Exception { Map<String,Object> map=new HashMap<String,Object>(); map.put("username", "perfect"); map.put("email", "jiguiyan1998@qq.com"); map.put("password", "123"); User u=new User(); System.out.println(u); BeanUtils.copyProperties(u, map); System.out.println(u); } }
注意:写的属性一定得需与数据模型中的相同,要不然会出现不能对应的错误,因为它找不到所需的属性。