package cn.hmm.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.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;
//要建立一个文件,再导入第三方的jar包(beanutils,logging)
public class Demo1 {
public static void main(String[] args) throws Exception{
// TODO 自动生成的方法存根
//test1();
//test2();
//test3();
//test4();
test5();
}
@Test
public void test1() throws Exception{
Person p = (Person) Class.forName("cn.hmm.beanutils.Person").newInstance();
// p = new Person();
BeanUtils.setProperty(p,"name","hmm");
System.out.println(p.getName());
}
public static void test2() throws IllegalAccessException,InvocationTargetException{
String name = "hmm";
String password = "a1234";
String age = "24";
int i = Integer.parseInt(age);
System.out.println("i="+i);
Person p = new Person();
BeanUtils.setProperty(p,"name", name);
BeanUtils.setProperty(p,"password", password);
BeanUtils.setProperty(p,"age", age);
System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
}
public static void test3() throws IllegalAccessException,InvocationTargetException{
//为了让日期赋到bean的birthday属性上,我们让BeanUtils注册一个日期转换器
String name = "hmm";
String password = "a1234";
String age = "24";
String birthday = "1990-07-23";
int i = Integer.parseInt(age);
System.out.println("i="+i);
//自己写转换器
ConvertUtils.register(new Converter() {
//@Override
public Object convert(Class type, Object value) {
if(value==null){
return null;
}
if(!(value instanceof String)){
throw new ConversionException("只支持String类型的转换");
}
String str = (String)value;
if(str.trim().equals("")){
return null;
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try{
return df.parse(str);
}catch(ParseException e){
throw new RuntimeException(e);
}
}
}
, Date.class);
Person p = new Person();
BeanUtils.setProperty(p,"name", name);
BeanUtils.setProperty(p,"password", password);
BeanUtils.setProperty(p,"age", age);
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
Date date = p.getBirthday();
System.out.println(date.toString());
}
public static void test4() throws IllegalAccessException,InvocationTargetException{
//为了让日期赋到bean的birthday属性上,我们使用BeanUtils写好的DateLocalConvert
String name = "hmm";
String password = "a1234";
String age = "24";
String birthday = "1990-07-23";
int i = Integer.parseInt(age);
System.out.println("i="+i);
ConvertUtils.register(new DateLocaleConverter(),Date.class);
Person p = new Person();
BeanUtils.setProperty(p,"name", name);
BeanUtils.setProperty(p,"password", password);
BeanUtils.setProperty(p,"age", age);
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
Date date = p.getBirthday();
System.out.println(date.toString());
}
public static void test5() throws IllegalAccessException,InvocationTargetException{
Map<String,String> map = new HashMap<String,String>();
map.put("name", "aaa");
map.put("password","123");
map.put("age", "23");
map.put("birthday", "1990-07-23");
ConvertUtils.register(new DateLocaleConverter(),Date.class);
Person bean = new Person();
BeanUtils.populate(bean, map);
System.out.println(bean.getName());
System.out.println(bean.getPassword());
System.out.println(bean.getAge());
Date date = bean.getBirthday();
System.out.println(date.toString());
}
}
package cn.hmm.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.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;
//要建立一个文件,再导入第三方的jar包(beanutils,logging)
public class Demo1 {
public static void main(String[] args) throws Exception{
// TODO 自动生成的方法存根
//test1();
//test2();
//test3();
//test4();
test5();
}
@Test
public void test1() throws Exception{
Person p = (Person) Class.forName("cn.hmm.beanutils.Person").newInstance();
// p = new Person();
BeanUtils.setProperty(p,"name","hmm");
System.out.println(p.getName());
}
public static void test2() throws IllegalAccessException,InvocationTargetException{
String name = "hmm";
String password = "a1234";
String age = "24";
int i = Integer.parseInt(age);
System.out.println("i="+i);
Person p = new Person();
BeanUtils.setProperty(p,"name", name);
BeanUtils.setProperty(p,"password", password);
BeanUtils.setProperty(p,"age", age);
System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
}
public static void test3() throws IllegalAccessException,InvocationTargetException{
//为了让日期赋到bean的birthday属性上,我们让BeanUtils注册一个日期转换器
String name = "hmm";
String password = "a1234";
String age = "24";
String birthday = "1990-07-23";
int i = Integer.parseInt(age);
System.out.println("i="+i);
//自己写转换器
ConvertUtils.register(new Converter() {
//@Override
public Object convert(Class type, Object value) {
if(value==null){
return null;
}
if(!(value instanceof String)){
throw new ConversionException("只支持String类型的转换");
}
String str = (String)value;
if(str.trim().equals("")){
return null;
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try{
return df.parse(str);
}catch(ParseException e){
throw new RuntimeException(e);
}
}
}
, Date.class);
Person p = new Person();
BeanUtils.setProperty(p,"name", name);
BeanUtils.setProperty(p,"password", password);
BeanUtils.setProperty(p,"age", age);
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
Date date = p.getBirthday();
System.out.println(date.toString());
}
public static void test4() throws IllegalAccessException,InvocationTargetException{
//为了让日期赋到bean的birthday属性上,我们使用BeanUtils写好的DateLocalConvert
String name = "hmm";
String password = "a1234";
String age = "24";
String birthday = "1990-07-23";
int i = Integer.parseInt(age);
System.out.println("i="+i);
ConvertUtils.register(new DateLocaleConverter(),Date.class);
Person p = new Person();
BeanUtils.setProperty(p,"name", name);
BeanUtils.setProperty(p,"password", password);
BeanUtils.setProperty(p,"age", age);
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
Date date = p.getBirthday();
System.out.println(date.toString());
}
public static void test5() throws IllegalAccessException,InvocationTargetException{
Map<String,String> map = new HashMap<String,String>();
map.put("name", "aaa");
map.put("password","123");
map.put("age", "23");
map.put("birthday", "1990-07-23");
ConvertUtils.register(new DateLocaleConverter(),Date.class);
Person bean = new Person();
BeanUtils.populate(bean, map);
System.out.println(bean.getName());
System.out.println(bean.getPassword());
System.out.println(bean.getAge());
Date date = bean.getBirthday();
System.out.println(date.toString());
}
}
Person类:
[java] view plaincopyprint?package cn.hmm.beanutils;
import java.util.Date;
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAb(){
return null;
}
private String password;
private int age;
private Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
BeanUtils
最新推荐文章于 2023-04-24 10:41:29 发布