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 星期日