PropertyUtils的使用

前言:本博客内容由张孝祥Java高新技术整理而来


在上节:对javaBean简单的内省操作 中我们写了两个静态的方法,用来获取和赋值javaBean属性。其实这些apache早已经帮我们封装好了,我们直接使用即可。


准备工作:两个jar包

1.commons-beanutils.jar

2.commons-logging.jar


直接上实例


po实体类


package com.dao.chu.movie;

public class Student {

	private int id;
	
	private String name;
	
	private int age;

	public Student() {
	}

	public Student(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	
}


vo实体类


package com.dao.chu.movie;

public class StudentVo {

	private int id;
	
	private String name;
	
	private int age;
	
	private float grade;
	
	@Override
	public String toString() {
		return "StudentVo [id=" + id + ", name=" + name + ", age=" + age
				+ ", grade=" + grade + "]";
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public float getGrade() {
		return grade;
	}

	public void setGrade(float grade) {
		this.grade = grade;
	}
	
	
	
	
}


测试类

package com.dao.chu.movie;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;

public class PropertyUtilTest {

@SuppressWarnings("unchecked")
public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, NoSuchMethodException {
		
		Student student = new Student(1, "张三", 18);
		StudentVo studentVo = new StudentVo();
		
		//设置属性 同方法  BeanUtils.setProperty
		PropertyUtils.setProperty(student, "name", "张三三");
		//获取属性  同方法  BeanUtils.getProperty
		Object name = PropertyUtils.getProperty(student, "name");
		System.out.println("getName is:"+name);
		
		//vo po转换
		System.out.println(studentVo);
		PropertyUtils.copyProperties(studentVo, student);
		System.out.println("vo is:"+studentVo);
		
		//po转换为map 同BeanUtils.describe
		Map<String, Object> map = new HashMap<String, Object>();
		map=PropertyUtils.describe(studentVo);
		System.out.println("map is:"+map.toString());
		
		//map赋值 
		PropertyUtils.setProperty(map, "father", "张五五");
		System.out.println("now map is:"+map.toString());
	}
}


运行结果:

getName is:张三三
StudentVo [id=0, name=null, age=0, grade=0.0]
vo is:StudentVo [id=1, name=张三三, age=18, grade=0.0]
map is:{grade=0.0, name=张三三, id=1, class=class com.dao.chu.movie.StudentVo, age=18}
now map is:{grade=0.0, father=张五五, name=张三三, id=1, class=class com.dao.chu.movie.StudentVo, age=18}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值