Commons BeanUtils的基本用法

//1.Commons beanutils要用到的三个包,一个主要包commons-beanutils-1.8.0.jar和两个依赖包commons-logging-1.1.1.jar和commons-logging-api-1.1.1.jar

public class BeanUtilsExample {

	public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		Profile profile = new Profile();
		Map<String,String> map = new HashMap<String,String>();
		map.put("home", "123");
		map.put("tel", "12574");
		profile.setPhone(map);
		profile.setBrithDate(new Date());
		profile.setEmail("[email protected]");
		Address[] address = {new Address("pzh","xj"),new Address("km","rmdl")};
		profile.setAddress(address);
		
		User user = new User();
		user.setId(100);
		user.setUsername("username");
		user.setPassword("password");
		user.setProfile(profile);
		//一般类型
		prt(BeanUtils.getProperty(user, "id"));
		//引用类型的一般类型
		prt(BeanUtils.getProperty(user, "profile.brithDate"));
		//Map类型
		prt(BeanUtils.getProperty(user, "profile.phone(home)"));
		//数组和List,Set类型
		prt(BeanUtils.getProperty(user, "profile.address[0].city"));
		
		User user2 = new User();
		//复制Bean,但如果Bean里面引用类型的不能复制
		BeanUtils.copyProperties(user2, user);		
		
		
	}
	
	public static void prt(Object object){
		System.out.println(object);
	}

}



JavaBean
public class Address implements Serializable {
	private String city;
	private String addr;
	public Address(){
	
	}
	public Address(String city,String addr){
		this.city = city;
		this.addr = addr;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
}
public class Profile implements Serializable {
	private Map<String,String> phone;
	private Address[] address;
	private Date brithDate;
	private String email;
	public Map<String, String> getPhone() {
		return phone;
	}
	public void setPhone(Map<String, String> phone) {
		this.phone = phone;
	}
	public Address[] getAddress() {
		return address;
	}
	public void setAddress(Address[] address) {
		this.address = address;
	}
	public Date getBrithDate() {
		return brithDate;
	}
	public void setBrithDate(Date brithDate) {
		this.brithDate = brithDate;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
}
public class User implements Serializable {
	private int id;
	private String username;
	private String password;
	private Profile profile;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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 Profile getProfile() {
		return profile;
	}
	public void setProfile(Profile profile) {
		this.profile = profile;
	}
}
//LazyDynaBeanExample的用法,动态的创建JAVABEAN
public class LazyDynaBeanExample {

	public static void main(String[] args) {
		LazyDynaBean user = new LazyDynaBean();
		LazyDynaBean profile = new LazyDynaBean();
		//设置一般属性
		user.set("userId", 123456);
		//设置对象
		user.set("profile", profile);
		//设置对象的数组,List
		profile.set("address", 0, "a");
		//设置对象的Map
		profile.set("tel","key", "value");	
		
	}

}

//BeanUitls还封装了对JDBC连结结果集ResultSet
//ResultSetDynaClass和RowSetDynaClass
http://java.chinaitlab.com/JDBCJDO/39229.html

你可能感兴趣的:(html,bean,qq,jdbc)