BeanUtils工具包是由Apache公司所開發,主要是方便程序員對Bean類可以進行簡便的操做。java
BeanUtils一共分4個包:程序員
org.apache.commons.beanutilsapache
org.apache.commons.beanutils.converterside
org.apache.commons.beanutils.locale工具
org.apache.commons.beanutils.locale.converterscode
其中須要咱們特別關注的是這個org.apache.commons.beanutils包,其餘包都是起輔助做用的。其中上面兩個沒有針對本地化的任何處理,執行效率高。下面兩個對本地化有要求。orm
主要提供了對於JavaBean進行各類操做,好比對象,屬性複製等等。對象
BeanUtils設置屬性值的時候,若是屬性是基本數據類型,那麼BeanUtils會自動幫咱們進行數據類型的轉換,而且BeanUtils設置屬性的時候也是依賴於底層的getter和setter方法。若是設置的屬性值是其餘的引用數據類型,此時必需要註冊一個類型轉換器才能實現自動的轉換(參考下面的ConvertUtils)接口
主要方法 | 描述 |
---|---|
cloneBean(Object bean) | 對象的克隆 |
copyProperties(Object dest, Object orig) | 對象的拷貝 |
copyProperty(Object bean, String name, Object value) | 拷貝指定的屬性值到指定的bean |
setProperty(Object bean, String name, Object value) | 設置指定屬性的值 |
populate(Object bean, Map<String,? extends Object> properties) | 將map數據拷貝到javabean中(map的key要與javabean的屬性名稱一致) |
注:copyProperty與setProperty,平常使用時推薦copyProperty。若是咱們須要自定義實現populate()方法,那麼咱們能夠override setProperty()方法.開發
// JavaBean public class Animal { private String name; private int age; private String color; private String sex; public Animal() { } getXxx和setXxx省略…… }
/** * BeanUtils */ @Test public void test1() throws Exception { Map<String, Object> map = new HashMap<String, Object>(); map.put("name", "tuantuan"); map.put("age", 3); map.put("color", "black"); map.put("sex", "female"); // 將map數據拷貝到javabean中 Animal a1 = new Animal(); BeanUtils.populate(a1, map); System.out.println(a1); // Animal [name=tuantuan, age=3, color=black, sex=female] // 對象的拷貝 Animal a2 = new Animal(); BeanUtils.copyProperties(a2, a1); System.out.println(a2);// Animal [name=tuantuan, age=3, color=black, sex=female] // 拷貝指定的屬性 Animal a3 = new Animal(); BeanUtils.copyProperty(a3, "name", "yuanyuan"); System.out.println(a3); // Animal [name=yuanyuan, age=0, color=null, sex=null] // 設置指定的屬性 BeanUtils.setProperty(a3, "sex", "male"); System.out.println(a3); // Animal [name=yuanyuan, age=0, color=null, sex=male] // 克隆對象 Object object = BeanUtils.cloneBean(a3); System.out.println(object); // Animal [name=yuanyuan, age=0, color=null, sex=male] }
這個工具類的職能是在字符串和指定類型的實例之間進行轉換。 實際上,BeanUtils是依賴ConvertUtils來完成類型轉換,可是有時候咱們可能須要自定義轉換器來完成特殊需求的類型轉換;
主要方法 | 描述 |
---|---|
convert(Object value, Class<?> targetType) | 將給定的value轉換成指定的Class類型 |
// 將整型轉換成字符串 Object object = ConvertUtils.convert(123, String.class); String typeName = object.getClass().getTypeName(); System.out.println(typeName); // 將日期轉換成字符串 Object object2 = ConvertUtils.convert(new Date(), String.class); String typeName2 = object2.getClass().getTypeName(); System.out.println(typeName2); // 將字符串轉換成Double Object object3 = ConvertUtils.convert("123", Double.class); String typeName3 = object3.getClass().getTypeName(); System.out.println(typeName3);
自定義類型轉換:
自定義轉換器,實現Converter接口,重寫convert方法
class MyConverter implements Converter { private static SimpleDateFormat format; public MyConverter(String pattern) { format = new SimpleDateFormat(pattern); } @Override public Object convert(Class type, Object value) { if (value == null) { return null; } if (value instanceof String) { String tmp = (String) value; if (tmp.trim().length() == 0) { return null; } else { try { return format.parse(tmp); } catch (ParseException e) { e.printStackTrace(); } } } else { throw new ConversionException("not String"); } return value; } }
使用
MyConverter converter = new MyConverter("yyyy-MM-dd HH:mm:ss"); // 註冊該轉換器 ConvertUtils.register(converter, Date.class); Object object3 = ConvertUtils.convert("2019-03-13 14:04:00", Date.class); System.out.println(object3.getClass().getTypeName()); System.out.println(object3);
// BeanUtils設置屬性時,自動進行類型轉換 MyConverter converter = new MyConverter("yyyy-MM-dd HH:mm:ss"); // 註冊該轉換器 ConvertUtils.register(converter, Date.class); Animal a5 = new Animal(); BeanUtils.copyProperty(a5, "birth", "2019-03-13 14:04:00"); System.out.println(a5);// Animal [name=null, age=0, color=null, sex=null, birth=Wed March 13 14:04:00 CST 2019]
BeanUtils與PropertyUtils這兩個類幾乎有一摸同樣的功能,惟一的區別是:BeanUtils在對Bean賦值是會進行類型轉化。
舉例來講也就是在copyProperty時只要屬性名相同,就算類型不一樣,BeanUtils也能夠進行copy;而PropertyBean則可能會報錯!!固然2個Bean之間的同名屬性的類型必須是能夠轉化的,不然用BeanUtils同樣會報錯。若實現了org.apache.commons.beanutils.Converter接口則能夠自定義類型之間的轉化。因爲不作類型轉化,用PropertyUtils在速度上會有很大提升!
利用這個工具類,咱們對集合進行修改、查詢、過濾等操做
CollectionUtils屬於commons-collections包
String[] arrA = new String[]{"1", "2", "3"}; String[] arrB = new String[]{"1", "a", "b"}; List<String> listA = Arrays.asList(arrA); List<String> listB = Arrays.asList(arrB); // 判斷集合是否爲 空 System.out.println(CollectionUtils.isEmpty(listA));// false System.out.println(CollectionUtils.isEmpty(listB));// false // 判斷集合是否爲 不爲空 System.out.println(CollectionUtils.isNotEmpty(listA));// true System.out.println(CollectionUtils.isNotEmpty(listB));// true // 兩個集合的比較 System.out.println(CollectionUtils.isEqualCollection(listA, listB));// false // 集合的操做 // 取並集 System.out.println(CollectionUtils.union(listA, listB));// [1, a, 2, b, 3] // 取交集 System.out.println(CollectionUtils.intersection(listA, listB));// [1] // 取交集的補集 System.out.println(CollectionUtils.disjunction(listA, listB));// [a, 2, b, 3] // 取集合相減 System.out.println(CollectionUtils.subtract(listA, listB));// [2, 3] System.out.println(CollectionUtils.subtract(listB, listA));// [a, b] List<String> listC = new ArrayList<>(); listC.add("1"); listC.add("2"); listC.add("3"); String[] arrC = {"4", "5", "6"}; // 向集合中添加值 CollectionUtils.addAll(listC, arrC); System.out.println(listC);// [1, 2, 3, 4, 5, 6]