BeanUtils:
- BeanUtils的简单使用
- BeanUtils的populate方法的使用
- BeanUtils自定义工具类
BeanUtils的简单使用
package bull01.BeanUtils;
import java.util.Arrays;
public class User implements java.io.Serializable {
private String userName;
private String userId;
private String userPasswd;
private String userHobby[];
private int userAge;
public User() {
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserPasswd() {
return userPasswd;
}
public void setUserPasswd(String userPasswd) {
this.userPasswd = userPasswd;
}
public String[] getUserHobby() {
return userHobby;
}
public void setUserHobby(String[] userHobby) {
this.userHobby = userHobby;
}
public int getUserAge() {
return userAge;
}
public void setUserAge(int userAge) {
this.userAge = userAge;
}
@Override
public String toString() {
return "User [userName=" + userName + ", userId=" + userId
+ ", userPasswd=" + userPasswd + ", userHobby="
+ Arrays.toString(userHobby) + ", userAge=" + userAge + "]";
}
}
package bull01.BeanUtils;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;
public class BeanUtilsDemo_01 {
@Test
public void method2() throws Exception, InvocationTargetException {
User users = new User();
BeanUtils.setProperty(users,"userId","ID002");
BeanUtils.setProperty(users,"userName", "kobe");
BeanUtils.setProperty(users, "userPasswd", "123");
System.out.println(users);
String userName = BeanUtils.getProperty(users, "userName");
System.out.println(userName);
}
}
结果:
User [userName=kobe, userId=ID002, userPasswd=123, userHobby=null, userAge=0]
kobe
BeanUtils的populate方法的使用
package bull01.BeanUtils;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;
public class BeanUtilsDemo_02 {
@Test
public void method() throws Exception, InvocationTargetException {
Map map = new HashMap();
map.put("userName", new String[]{"张三"});
map.put("userId", new String[]{"李四"});
map.put("userPasswd", new String[]{"王五"});
map.put("userHobby", new String[]{"打篮球","踢足球"});
map.put("userAge", new String[]{"10"});
User users = new User();
BeanUtils.populate(users, map);
System.out.println(users);
}
}
结果:
User [userName=张三, userId=李四, userPasswd=王五, userHobby=[打篮球, 踢足球], userAge=10]
BeanUtils自定义工具类
package bull01.MyBeanUtils;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;
public class MyBeanUtilsDemo_01 {
@Test
public static User populate(Class beanClass,Map properties) {
try {
User obj = beanClass.newInstance();
BeanUtils.populate(obj , properties);
return obj;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
package bull01.MyBeanUtils;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import bull01.BeanUtils.User;
public class TestTool {
@Test
public void method() {
Map map = new HashMap();
map.put("userName", new String[]{"kobe"});
map.put("userId", new String[]{"Id002"});
map.put("userPasswd", new String[]{"9246538"});
map.put("userHobby", new String[]{"打篮球","踢足球"});
map.put("userAge", new String[]{"10"});
User user = MyBeanUtilsDemo_01.populate(User.class,map);
System.out.println(user);
}
}
结果:
User [userName=kobe, userId=Id002, userPasswd=9246538, userHobby=[打篮球, 踢足球], userAge=10]