Commons-BeanUtils 基于Java 反射来完成对Bean的一些操做。经常使用于对于JavaBean进行各类操做,克隆对象,属性等等。比较简单。java
###包介绍~~~~apache
###API分类app
复制一个JavaBean的实例-- BeanUtils.cloneBean()测试
在一个JavaBean的两个实例之间复制属性-- BeanUtils.copyProperties()
BeanUtils.copyProperty()this
为一个JavaBean的实例设置成员变量(属性)值-- BeanUtils.populate()
BeanUtils.setProperty()
PropertyUtils.setSimpleProperty3d
从 一个一个JavaBean的实例中读取成员变量(属性)的值 -- BeanUtils.getArrayProperty()
BeanUtils.getIndexedProperty()
BeanUtils.getMappedProperty()
BeanUtils.getNestedProperty()
BeanUtils.getSimpleProperty()
BeanUtils.getProperty()
BeanUtils.describe()
PropertyUtils.getSimpleProperty
PropertyUtils.getIndexedPropertycode
###依赖:orm
<dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.3</version> </dependency>
###示例 对象定义对象
public class BeanutilCopyVo { private long id; private int age; private boolean sex; private float weight; private double height; private short classes; private String name; private Set<String> nicks; private Date birthDay; ... }
对象设值.接口
private static BeanutilCopyVo getVo() { BeanutilCopyVo vo = new BeanutilCopyVo(); vo.setAge(1); vo.setClasses((short) 2); vo.setHeight(3d); vo.setWeight(4f); vo.setName("name"); vo.setId(0l); vo.setSex(true); vo.setBirthDay(new Date()); Set<String> nicks = new HashSet<String>(); nicks.add("1"); nicks.add("2"); nicks.add("3"); vo.setNicks(nicks); return vo; }
测试
@Test public void testCopySame() { BeanutilCopyVo vo2 = new BeanutilCopyVo(); try { BeanUtils.copyProperties(vo2, vo); //拷贝整个对象 assertEquals(1, vo2.getAge()); assertEquals("1", BeanUtils.getProperty(vo2, "age")); //获取一个属性 BeanUtils.setProperty(vo2, "age", 2); //设置一个属性 assertEquals("2", BeanUtils.getProperty(vo2, "age")); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { //LogUtil.get(this.getClass()).error(e); } } @Test public void testCopyProperty() { BeanutilCopyVo vo2 = new BeanutilCopyVo(); try { BeanUtils.copyProperty(vo2, "age", "22"); assertEquals("22", BeanUtils.getProperty(vo2, "age")); ConvertUtils.register(new BeanutilDateConverter(), Date.class); //特殊属性时间的处理 BeanUtils.copyProperty(vo2, "birthDay", "2016-09-11"); assertNotNull(BeanUtils.getProperty(vo2, "birthDay")); // assertEquals("2016-09-11", BeanUtils.getProperty(vo2, "birthDay")); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { //LogUtil.get(this.getClass()).error(e); } }
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.beanutils.Converter; import com.zx.common.log.LogUtil; public class BeanutilDateConverter implements Converter { private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); /** * 要将字符串的value转换为java.util.Date类型的值 * @param targetClass 第一个参数 是你的目标类型 * @param value 要转换的值, * @return 把要转回的值,返回出去就ok了 */ @SuppressWarnings({"unchecked", "rawtypes"}) public Date convert(Class targetClass, Object value) { if (targetClass != Date.class) { return null; } try { if (value instanceof String) { String v = (String) value; return format.parse(v); } } catch (ParseException e) { LogUtil.get(this.getClass()).error(e); } return null; } }
2016-11-06 09:29:52 星期日