close

BeanUtils是一種非常實用的JavaBean,在apache底下的一個組件,我稍微介紹一下它的實用性,等等用程式碼來代表就清楚了,首先需要先把 commons-beanutils-1.8.3.jar 和 commons-logging-1.1.1.jar 導入,再將bean屬性建置好

public class User {

private String username;

private String password;

private String attribute;

private Long userId;

private Profile profile;

//以下get,set
}

public class Profile {

private String email;

private Date birthDate;

private Address[] address;

private Map<string, string=""> phone;

private HashMap telephone;

//以下get,set
}

public class Address {

private String country;

private String city;

private String addr;

private String postcode;

public Address(String country, String city, String addr, String postcode) {
this.country = country;
this.city = city;
this.addr = addr;
this.postcode = postcode;
}

//以下get,set
}


import java.lang.reflect.InvocationTargetException;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;

import BeanUtils.Address;
import BeanUtils.Profile;
import BeanUtils.User;

public class BeanUtilExample {

//建立假資料
public User prepareDate() throws IllegalAccessException, InvocationTargetException {
Profile profile = new Profile();

profile.setEmail("pp@gmail.com");
profile.setBirthDate(new GregorianCalendar(2012, 06, 16).getTime());

Map<string, string=""> phone = new HashMap<string, string="">();
phone.put("home", "(03)XXXXXXX");
phone.put("office", "(02)1234-5678");
phone.put("mobile", "09XX-XXX-XXX");
profile.setPhone(phone);

Address addr1 = new Address("台灣省", "桃園縣中壢市", "XX路", "100001");
Address addr2 = new Address("台灣省", "台北市中山區", "XXX路", "100002");
Address[] address = { addr1, addr2 };
profile.setAddress(address);

User user = new User();
user.setUserId(new Long(123456789));
user.setUsername("puma");
user.setPassword("987654321");
user.setAttribute("男");
user.setProfile(profile);

return user;
}

public static void main(String[] args) {

BeanUtilExample example = new BeanUtilExample();

try {
User user = example.prepareDate();
//取值方式
System.out.println(BeanUtils.getProperty(user, "userId"));
System.out.println(PropertyUtils.getProperty(user, "userId"));
System.out.println(BeanUtils.getProperty(user, "username"));
System.out.println(BeanUtils.getProperty(user, "password"));
System.out.println(BeanUtils.getProperty(user, "attribute"));
System.out.println(BeanUtils.getProperty(user, "profile"));
System.out.println(BeanUtils.getProperty(user, "profile.email"));
System.out.println(BeanUtils.getProperty(user, "profile.birthDate"));
System.out.println(BeanUtils.getProperty(user, "profile.phone" ));
System.out.println(BeanUtils.getProperty(user, "profile.phone(mobile)" ));
System.out.println(BeanUtils.getProperty(user, "profile.phone(office)" ));
System.out.println(BeanUtils.getProperty(user, "profile.phone(home)" ));
System.out.println(BeanUtils.getProperty(user, "profile.address[0].country" ));
System.out.println(BeanUtils.getProperty(user, "profile.address[0].city" ));
System.out.println(BeanUtils.getProperty(user, "profile.address[0].postcode" ));

System.out.println("--------------------------------------------------------------------");

User user2 = new User();
BeanUtils.copyProperties(user2, user); //將user這物件轉至user2
System.out.println(BeanUtils.getProperty(user2, "userId" )); //輸出user類的userId的值
System.out.println(PropertyUtils.getProperty(user2, "userId" ));
System.out.println(BeanUtils.getProperty(user2, "username" ));
System.out.println(BeanUtils.getProperty(user2, "password" ));
System.out.println(BeanUtils.getProperty(user2, "profile" ));
System.out.println(BeanUtils.getProperty(user2, "profile.email" ));
System.out.println(BeanUtils.getProperty(user2, "profile.birthDate" ));
System.out.println(BeanUtils.getProperty(user2, "profile.phone" ));
System.out.println(BeanUtils.getProperty(user2, "profile.phone(mobile)" ));
System.out.println(BeanUtils.getProperty(user2, "profile.phone(office)" ));
System.out.println(BeanUtils.getProperty(user2, "profile.phone(home)" ));
System.out.println(BeanUtils.getProperty(user2, "profile.address[0].country" ));
System.out.println(BeanUtils.getProperty(user2, "profile.address[0].city" ));
System.out.println(BeanUtils.getProperty(user2, "profile.address[0].postcode" ));

System.out.println("--------------------------------------------------------------------");

Profile profile = new Profile();
HashMap map = new HashMap();

map.put( "1", "13880808080" );
map.put( "2", "13550505050" );
BeanUtils.setProperty( profile, "telephone", map );
System.out.println(BeanUtils.getProperty( profile, "telephone(1)" ));

} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

 
 
 
結果
 
123456789
123456789
puma
987654321
BeanUtils.Profile@66388993
pp@gmail.com
Mon Jul 16 00:00:00 CST 2012
{office=(02)1234-5678, home=(03)XXXXXXX, mobile=09XX-XXX-XXX}
09XX-XXX-XXX
(02)1234-5678
(03)XXXXXXX
台灣省
桃園縣中壢市
100001
--------------------------------------------------------------------
123456789
123456789
puma
987654321
BeanUtils.Profile@66388993
pp@gmail.com
Mon Jul 16 00:00:00 CST 2012
{office=(02)1234-5678, home=(03)XXXXXXX, mobile=09XX-XXX-XXX}
09XX-XXX-XXX
(02)1234-5678
(03)XXXXXXX
台灣省
桃園縣中壢市
100001
--------------------------------------------------------------------
15136125313
 





 















其它文章

arrow
arrow

    PG Levin Li 發表在 痞客邦 留言(0) 人氣()