1. 程式人生 > >org.apache.commons.beanutils.BeanUtils 工具類詳解

org.apache.commons.beanutils.BeanUtils 工具類詳解

1、建立User類

package com.kd.zhouyuan.entity;


import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


public class User {
private String userName;
private String password;
private String[] hobby;
private Map map = new HashMap();
public User() {
super();
}

public User(String userName, String password, String[] hobby) {
super();
this.userName = userName;
this.password = password;
this.hobby = hobby;
this.map = map;
}

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[] getHobby() {
return hobby;
}


public void setHobby(String[] hobby) {
this.hobby = hobby;
}


public Map getMap() {
return map;
}


public void setMap(Map map) {
this.map = map;
}


@Override
public String toString() {
return "User [userName=" + userName + ", password=" + password
+ ", hobby=" + Arrays.toString(hobby) + ", map=" + map + "]";
}
}

二、測試類

package com.kd.zhouyuan.util;




import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


import org.apache.commons.beanutils.BeanUtils;


import com.kd.zhouyuan.entity.User;


/**
 * BeanUtils 使用
 * @author 269052
 *
 */
public class MyBeanUtil {
public static void main(String[] args) throws Exception {
User user= new User("hello","123456",new String[]{"籃球","排球"});
Map map = new HashMap();
map.put("name", "黎明");
user.setMap(map);
//System.out.println(user.getPassword());
// BeanUtils設定屬性
//BeanUtils.setProperty(user, "userName", "linxi");
// 獲取屬性
//System.out.println(BeanUtils.getProperty(user, "userName"));

// 複製一個類的屬性到另一個類
/*User user1 = new User("liming","liming");
System.out.println(user1.getUserName());
BeanUtils.copyProperties(user1, user);

System.out.println(BeanUtils.getProperty(user1, "userName"));
System.out.println(BeanUtils.getProperty(user1, "password"));*/



/**
* 1、克隆一個Bean
*/
/*User user1 = (User) BeanUtils.cloneBean(user);
System.out.println(user1.getUserName());*/

/**
* 2、複製一個物件的屬性到另外一個物件,全部複製
*/
/*User user2  = new User();
BeanUtils.copyProperties(user2,user);
System.out.println(user2.toString());*/


/**
* 3、將目標物件的屬性值複製到另一個物件中的屬性值,不會全部複製
* copyProperty(destBean,String name,origBean.屬性);
*/
/*User user3 = new User();
BeanUtils.copyProperty(user3, "userName", user.getUserName());
System.out.println(user3.toString());*/

/**
* 4、將目標物件轉為Map集合
*/
/*Map map = BeanUtils.describe(user);
System.out.println(map.get("userName"));*/

/**
* 5、返回一個物件的屬性型別為陣列的值
*/
/*String[] hobby = BeanUtils.getArrayProperty(user, "hobby");
System.out.println(Arrays.toString(hobby));*/

/**
* 6、
* //對於Indexed,則為“屬性名[索引值]”,注意這裡對於ArrayList和陣列都可以用一樣的方式進行操作。
*/
/*String index = BeanUtils.getIndexedProperty(user,"hobby",1);
System.out.println(index);*/

/**
* 7、
* //對於Map型別,則需要以“屬性名(key值)”的形式
*/
/*String name = BeanUtils.getMappedProperty(user, "map", "name");
System.out.println(name);*/

/**
* 8、獲取字串屬性值
*/
/*String userName = BeanUtils.getProperty(user, "userName");
System.out.println(userName);*/

/**
* 9、將物件轉為Map
*/
/*Map map = BeanUtils.describe(user);
System.out.println(map.get("userName"));
*/
/**
* 10、將Map裝為物件
*/
/*Map map1 = new HashMap();
map1.put("userName", "liming");
map1.put("password", "liming");
BeanUtils.populate(user, map1);

System.out.println(user.getUserName());*/
}
}