4025b29bd476001c10b28e19bbde48da
来自「用java编写的程序」· 代码 · 共 66 行
TXT
66 行
package webbook.beanutils.example;
import java.util.Map;
import java.util.HashMap;
import java.util.GregorianCalendar;
import org.apache.commons.beanutils.BeanUtils;
public class BeanUtilsExample1 {
private User prepareData() {
Profile profile = new Profile();
profile.setEmail("javase@v512.com");
profile.setBirthDate(new GregorianCalendar(2040, 9, 10).getTime());
Map<String, String> phone = new HashMap<String, String>();
phone.put("home", "11011011");
phone.put("office", "82826905");
profile.setPhone(phone);
Address[] address = { new Address("中国", "北京", "100120", "天安门北大街888号"),
new Address("中国", "广州", "100120", "石牌村666号") };
profile.setAddress(address);
User user = new User();
user.setUserId(new Long(123456789));
user.setUsername("刘伟");
user.setPassword("ingodwetrust");
user.setProfile(profile);
return user;
}
public static void main(String[] args) {
BeanUtilsExample1 example = new BeanUtilsExample1();
User user = example.prepareData();
try {
System.out.println(BeanUtils.getProperty(user, "userId"));
System.out.println(BeanUtils.getProperty(user, "username"));
System.out.println(BeanUtils.getProperty(user, "password"));
System.out.println(BeanUtils.getProperty(user, "profile.email"));
System.out.println(BeanUtils.getProperty(user, "profile.birthDate"));
System.out.println(BeanUtils.getProperty(user, "profile.phone(home)"));
System.out.println(BeanUtils.getProperty(user, "profile.phone(office)"));
System.out.println(BeanUtils.getProperty(user, "profile.address[0].city"));
System.out.println(BeanUtils.getProperty(user, "profile.address[0].detail"));
System.out.println();
User user2 = new User();
BeanUtils.copyProperties(user2, user);
System.out.println(BeanUtils.getProperty(user, "username"));
System.out.println(BeanUtils.getProperty(user, "profile.birthDate"));
System.out.println(BeanUtils.getProperty(user, "profile.phone(home)"));
System.out.println(BeanUtils.getProperty(user, "profile.address[0].detail"));
BeanUtils.setProperty(user2, "username", "张利国");
BeanUtils.setProperty(user2, "profile.birthDate", new GregorianCalendar(1900, 2, 5));
BeanUtils.setProperty(user2, "profile.address[0]", new Address("中国", "深圳", "600600", "深北大道111号"));
System.out.println();
System.out.println(BeanUtils.getProperty(user2, "username"));
System.out.println(BeanUtils.getProperty(user2, "profile.birthDate"));
System.out.println(BeanUtils.getProperty(user2, "profile.address[0].city"));
System.out.println(BeanUtils.getProperty(user2, "profile.address[0].detail"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?