Apache Commons工具類整理以下:java
一、BeanUtils算法
package com.shma.common.beanutils; import java.lang.reflect.InvocationTargetException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.Converter; import org.apache.commons.beanutils.locale.converters.DateLocaleConverter; import org.junit.Test; /** * JavaBean操做工具類 * @author admin * */ public class BeanUtilsTest { @Test public void testProperty01() throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException { Class<?> clazz = Class.forName("com.shma.common.beanutils.Person"); Person person = (Person) clazz.newInstance(); BeanUtils.setProperty(person, "name", "張三"); //使用默認的日期格式化 ConvertUtils.register(new DateLocaleConverter(), Date.class); BeanUtils.setProperty(person, "date", "1989-12-07"); System.out.println(person); } public Person testProperty02() throws IllegalAccessException, InvocationTargetException { Person person = new Person(); BeanUtils.setProperty(person, "id", 1); BeanUtils.setProperty(person, "name", "李四"); ConvertUtils.register(new Converter() { @Override public Date convert(Class clazz, Object value) { if(value == null) return null; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = null; try { date = sdf.parse(String.valueOf(value)); } catch (ParseException e) { e.printStackTrace(); } return date; } }, Date.class); BeanUtils.setProperty(person, "date", "2015-10-13 11:53:54"); System.out.println(person); return person; } @Test public void testClone() throws Exception { Person person = testProperty02(); Person newPerson = (Person) BeanUtils.cloneBean(person); System.out.println(newPerson); System.out.println(newPerson == person); } @Test public void testMap2Bean() throws Exception { Map<String, Object> dataMap = new HashMap<String, Object>(); dataMap.put("id", 2); dataMap.put("name", "王五"); dataMap.put("date", new SimpleDateFormat("yyyy-MM-dd").format(new Date())); // map轉bean Person person = new Person(); ConvertUtils.register(new DateLocaleConverter(), Date.class); BeanUtils.populate(person, dataMap); System.out.println(person); // bean轉map Person person2 = testProperty02(); Map<String, String> newDataMap = BeanUtils.describe(person2); System.out.println(newDataMap); } } package com.shma.common.beanutils; import java.util.Date; public class Person { private int id; private String name; private Date date; public Person() { super(); } public Person(int id, String name, Date date) { super(); this.id = id; this.name = name; this.date = date; } 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 Date getDate() { return date; } public void setDate(Date date) { this.date = date; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", date=" + date + "]"; } }
二、betwixt:xml轉bean,bean轉xmlapache
package com.shma.common.betwixt; import java.beans.IntrospectionException; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import org.apache.commons.betwixt.io.BeanReader; import org.apache.commons.betwixt.io.BeanWriter; import org.junit.Test; import org.xml.sax.SAXException; public class BetwixtTest { @Test public void bean2Xml() throws IOException, SAXException, IntrospectionException { StringWriter sWriter = new StringWriter(); sWriter.write("<?xml version='1.0' encoding='UTF-8' ?>\n"); BeanWriter beanWriter = new BeanWriter(sWriter); beanWriter.getXMLIntrospector().getConfiguration() .setAttributesForPrimitives(false); beanWriter.getBindingConfiguration().setMapIDs(false); beanWriter.write("person", new Person("馬韶華", 26)); System.out.println(sWriter.toString()); /** * <?xml version='1.0' encoding='UTF-8' ?> <person> <age>26</age> <name>馬韶華</name> </person> * */ sWriter.flush(); sWriter.close(); } @Test public void xml2Bean() { String xml = "<?xml version='1.0' encoding='UTF-8' ?><person><age>26</age><name>馬韶華</name></person>"; StringReader sReader = new StringReader(xml); BeanReader beanReader = new BeanReader(); beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false); beanReader.getBindingConfiguration().setMapIDs(false); } } package com.shma.common.betwixt; public class Person { private String name; private int age; public Person() { super(); } public Person(String name, int age) { super(); this.name = name; this.age = age; } 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; } }
三、codec:加密解密ide
package com.shma.common.codec; import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.digest.DigestUtils; import org.junit.Test; public class CodecUtilsTest { @Test public void testMd5() { String key = "123456"; System.out.println(DigestUtils.md5(key)); System.out.println(DigestUtils.md5(key.getBytes())); System.out.println(DigestUtils.md5Hex(key)); System.out.println(DigestUtils.md5Hex(key.getBytes())); } @Test public void testSHA() { String key = "123456"; System.out.println(DigestUtils.shaHex(key)); System.out.println(DigestUtils.shaHex(key.getBytes())); System.out.println(DigestUtils.sha(key)); System.out.println(DigestUtils.sha(key.getBytes())); } /** * 加密解密算法:BASE64 */ @Test public void base64() { String key = "abc"; byte[] bytes = Base64.encodeBase64(key.getBytes(), true); String newKey = new String(bytes); System.out.println(newKey); byte[] currKey = Base64.decodeBase64(newKey.getBytes()); System.out.println(new String(currKey)); } }