Java-No.13 Apache Commons工具集整理
程序员文章站
2022-06-21 18:27:32
...
Apache Commons工具类整理如下:
1、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 + "]";
}
}
2、betwixt:xml转bean,bean转xml
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;
}
}
3、codec:加密解密
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));
}
}
转载于:https://my.oschina.net/shma1664/blog/516250
推荐阅读
-
Apache Commons DbUtils工具包使用介绍
-
Apache Commons工具类 CSV文件读写
-
Apache Commons 工具类介绍及简单使用
-
Apache commons lang NumberUtils、ArrayUtils和RandomUtils工具类
-
咖啡汪日志——实际工作中好用的集合工具类(org.apache.commons.collections4.CollectionUtils)
-
org.apache.commons.lang3.RandomStringUtils工具类
-
org.apache.commons.lang3.RandomStringUtils工具类
-
????《吐血整理》-*程序员工具集
-
Java-No.13 Apache Commons工具集整理
-
Apache工具库——Apache Commons的使用