- BeanUtils.setProperty(bean, name, value):为指定bean实例的属性设值,等同于bean.setXXX()方法;其中bean是指你将要设置的对象,name指的是将要设置的属性(写成”属性名”),value(想要设置的属性值);
- BeanUtils.copyProperties(bean, name, value):与上边的setProperty方法功能相同;
- ConvertUtils.register(Converter converter , Class clazz):类型转换器注册方法,当需要将String数据转换成引用数据类型(自定义数据类型时,例如Date类型),需要使用此方法实现转换;
- BeanUtils.populate(bean,Map):将Map集合中的数据注入到JavaBean的属性中去,其中Map中的key必须与目标对象中的属性名相同,否则不能实现拷贝;
- BeanUtils.copyProperties(newObject,oldObject):实现对象之间的拷贝。
1 package bean;
2
3 public class UserBean {
4 private int userId;
5 private String userName;
6 private String userPass;
7
8 public int getUserId() {
9 return userId;
10 }
11
12 public void setUserId(int userId) {
13 this.userId = userId;
14 }
15
16 public String getUserName() {
17 return userName;
18 }
19
20 public void setUserName(String userName) {
21 this.userName = userName;
22 }
23
24 public String getUserPass() {
25 return userPass;
26 }
27
28 public void setUserPass(String userPass) {
29 this.userPass = userPass;
30 }
31
32 }
1 import java.lang.reflect.InvocationTargetException;
2 import java.util.HashMap;
3 import java.util.Map;
4
5 import org.apache.commons.beanutils.BeanUtils;
6 import org.apache.commons.beanutils.PropertyUtils;
7
8 public class BeanUtil {
9
10 public static void main(String[] args) throws NoSuchMethodException {
11 UserBean user = new UserBean();
12 Map<String, Object> prop = new HashMap<String, Object>();
13 prop.put("userId", "8");
14 prop.put("userName", "mapProp");
15 prop.put("userPass", "root");
16 try {
17 // 将Map集合中的数据注入到JavaBean的属性中去,其中Map中的key必须与目标对象中的属性名相同,否则不能实现拷贝;
18 BeanUtils.populate(user, prop);
19 // 输出结果,观察是否成功向user中注入数据.
20 System.out.println(user.getUserId() + ":" + user.getUserName() + ":" + user.getUserPass());
21 } catch (IllegalAccessException | InvocationTargetException e) {
22 e.printStackTrace();
23 }
24 }
25 }
1 import java.lang.reflect.InvocationTargetException;
2 import java.util.HashMap;
3 import java.util.Map;
4
5 import org.apache.commons.beanutils.BeanUtils;
6 import org.apache.commons.beanutils.PropertyUtils;
7
8 public class BeanUtil {
9
10 public static void main(String[] args) throws NoSuchMethodException {
11 UserBean user = new UserBean();
12 try {
13 BeanUtils.setProperty(user, "userName", "anmin");
14 BeanUtils.setProperty(user, "userId", 5);
15 BeanUtils.setProperty(user, "userPass", "password");
16 // 用BeanUtils.getProperty获得数据
17 System.out.println(BeanUtils.getProperty(user, "userId") + ":"
18 + BeanUtils.getProperty(user, "userName") + ":" + BeanUtils.getProperty(user, "userPass") );
19 } catch (IllegalAccessException | InvocationTargetException e) {
20 e.printStackTrace();
21 }
22 }
23 }
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
public class BeanUtil {
public static void main(String[] args) throws NoSuchMethodException {
UserBean user = new UserBean();
try {
//BeanUtils以字符串的形式set userId的值 不会出错
BeanUtils.setProperty(user, "userId", "5");
System.out.println(BeanUtils.getProperty(user, "userId"));
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
public class BeanUtil {
public static void main(String[] args) throws NoSuchMethodException {
UserBean user = new UserBean();
try {
//PropertyUtils以字符串的形式set userId的值 报错
PropertyUtils.setProperty(user, "userId", "9");
System.out.println(PropertyUtils.getProperty(user, "userId"));
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
1 import java.lang.reflect.InvocationTargetException;
2 import java.util.HashMap;
3 import java.util.Map;
4
5 import org.apache.commons.beanutils.BeanUtils;
6 import org.apache.commons.beanutils.PropertyUtils;
7
8 public class BeanUtil {
9
10 public static void main(String[] args) throws NoSuchMethodException {
11 UserBean user = new UserBean();
12 try {
13 //PropertyUtils以字符串的形式set userId的值 报错 必须按其原本的数据类型为其赋值
14 PropertyUtils.setProperty(user, "userId", 9);
15 System.out.println(PropertyUtils.getProperty(user, "userId"));
16 } catch (IllegalAccessException | InvocationTargetException e) {
17 e.printStackTrace();
18 }
19 }
20 }