Objective
Before you have learned the Apache Commons beanutils package propertyutils and dynamic beans, the next will learn the remaining several tools, the personal feel is very practical, especially collectionutils;
Beanutils
A brief introduction to the use of the next two methods, populate and Copyproperties,
Populate can help us copy the value of the key value in the map to the bean's attribute value;
Copyproperties, as the name implies, helps us copy the attributes of one bean to another bean, note that a shallow copy
The following example:
/** File Name:BeanUtilsTest.java * Description: * Author:http://www.cnblogs.com/chenpi/* Create date:2017 May 30*/ Packageapache.commons.beanutils.example.utils;Importjava.lang.reflect.InvocationTargetException;ImportJava.util.HashMap;ImportJava.util.Map;Importorg.apache.commons.beanutils.BeanUtils;ImportApache.commons.beanutils.example.pojo.User;/** * * @author http://www.cnblogs.com/chenpi/ * @versionMay 30, 2017*/ Public classbeanutilstest{ Public Static voidMain (string[] args)throwsillegalaccessexception, invocationtargetexception {Map<string, object> map =NewHashmap<string, object>(); Map.put ("Name", "001"); //map.put ("Address", "Hz");Map.put ("id", "100"); Map.put ("State",false); Map.put ("Others", "others"); User u=NewUser (); Beanutils.populate (U, map); SYSTEM.OUT.PRINTLN (U); User U1=NewUser (); Beanutils.copyproperties (U1, u); System.out.println (U1); }}
Convertutils
In fact, beanutils is dependent onConvertUtils来完成实际山的类型转换,但是有时候我们可能需要自定义转换器来完成特殊需求的类型转换;
To customize a type converter step:
1, define an implementation class implementation converter interface
2. Call the Convertutils.register method to register the converter
如下是一个实例,我们会在字符串转换的时候,加上一个前缀:
/** File Name:CustomConverters.java * Description: * Author:http://www.cnblogs.com/chenpi/* Create date:2017 May 30*/ Packageapache.commons.beanutils.example.utils;Importjava.lang.reflect.InvocationTargetException;ImportJava.util.HashMap;ImportJava.util.Map;Importorg.apache.commons.beanutils.BeanUtils;Importorg.apache.commons.beanutils.ConvertUtils;ImportOrg.apache.commons.beanutils.Converter;ImportApache.commons.beanutils.example.pojo.User;/** * * @author http://www.cnblogs.com/chenpi/ * @versionMay 30, 2017*/ Public classcustomconverters{ Public Static voidMain (string[] args)throwsillegalaccessexception, InvocationTargetException {convertutils.register (NewStringconverter (), String.class); Map<string, string> map =NewHashmap<string, string>(); Map.put ("Name", "001"); Map.put ("Address", "Hz"); Map.put ("id", "100"); Map.put ("State", "false"); User u=NewUser (); Beanutils.populate (U, map); SYSTEM.OUT.PRINTLN (U); }}classStringconverterImplementsConverter {/** * * * @seeOrg.apache.commons.beanutils.converter#convert (Java.lang.Class, java.lang.Object) *@paramtype *@paramValue *@return */@SuppressWarnings ("Unchecked") @Override Public<T> T Convert (class<t>type, Object value) { if(String.class. Isinstance (value)) { return(T) ("# # #" +value); }Else{ return(T) value; } } }
Here's one thing to note, like BeanUtils
, ConvertUtils
andPropertyUtils工具类都是共享同一个转换器的,这样子虽然用起来很方便,但有时候显得不够灵活,实际上BeanUtils
, ConvertUtils
和 PropertyUtils都有一个对应的可实例化的类,即BeanUtilsBean、ConvertUtilsBean、PropertyUtilsBean;
They function with BeanUtils
, ConvertUtils
andPropertyUtils类似,区别是它们可以实例化,而且每个实例都可以拥有自己的类型转换器;
Collectionutils
As the name implies, the collection tool class, except that it operates in the collection of beans,
With this tool class, we can batch modify, query, filter The Bean in the collection, and even copy a property of all the beans in the collection to another collection, a bit of Java 8 new features Streams feeling
The following example:
/** File Name:CollectionUtilsTest.java * Description: * Author:http://www.cnblogs.com/chenpi/* Create date:2017 May 30*/ Packageapache.commons.beanutils.example.utils;Importjava.util.ArrayList;Importjava.util.Collection;Importjava.util.List;Importorg.apache.commons.beanutils.BeanPropertyValueChangeClosure;Importorg.apache.commons.beanutils.BeanPropertyValueEqualsPredicate;ImportOrg.apache.commons.beanutils.BeanToPropertyValueTransformer;Importorg.apache.commons.collections.CollectionUtils;ImportApache.commons.beanutils.example.pojo.User;/** * * @author http://www.cnblogs.com/chenpi/ * @versionMay 30, 2017*/ Public classcollectionutilstest{ Public Static voidMain (string[] args) {List<User> userlist =NewArraylist<user>(); User U1=NewUser (); U1.setid (1l); U1.setname ("Chenpi1"); U1.setstate (true); User U2=NewUser (); U2.setid (2l); U2.setname ("Chenpi2"); User U3=NewUser (); U2.setid (3l); U2.setname ("Chenpi3"); U2.setstate (true); Userlist.add (U1); Userlist.add (U2); Userlist.add (U3); //Batch Modify CollectionBeanpropertyvaluechangeclosure closure =NewBeanpropertyvaluechangeclosure ("name", "UpdateName"); Collectionutils.foralldo (userlist, closure); for(User tmp:userlist) {System.out.println (Tmp.getname ()); } beanpropertyvalueequalspredicate predicate=NewBeanpropertyvalueequalspredicate ("state", boolean.true); //Filter CollectionCollectionutils.filter (userlist, predicate); for(User tmp:userlist) {SYSTEM.OUT.PRINTLN (TMP); } //Create transformerBeantopropertyvaluetransformer transformer =NewBeantopropertyvaluetransformer ("id" ); //transfer all of your user IDs from the collection to another collectionCollection<?> idlist =collectionutils.collect (userlist, transformer); for(Object id:idlist) {System.out.println (ID); } }}
Resources
http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.3/apidocs/org/apache/commons/beanutils/ Package-summary.html
Source
Https://github.com/peterchenhdu/apache-commons-beanutils-example
Apache Commons Beanutils III (Beanutils, Convertutils, collectionutils ... )