什么是BeanUtils工具
BeanUtils工具是一种方便我们对JavaBean进行操作的工具,是Apache组织下的产品。
BeanUtils工具一般可以方便javaBean的哪些操作?
1)beanUtils 可以便于对javaBean的属性进行赋值。
2)beanUtils 可以便于对javaBean的对象进行赋值。
3)beanUtils可以将一个MAP集合的数据拷贝到一个javabean对象中。
BeanUtils的使用
使用beanUtils按照以下步骤~
前提:约定前提: 参数名称 需要和javabean的属性名称保持一致!!!!
导包:导入commons-beanutils-1.9.3 包
与 commons-logging-1.2包
package com.lpy.jdbc.beanUtil;
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 java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class Demo1 {
public static void main(String[] args) throws Exception {
//t1();
//t2();
ConvertUtils.register(new DateLocaleConverter(),java.util.Date.class);
Map<String,String> map=new HashMap<>();
map.put("name","lpy");
map.put("id","4");
map.put("gender","true");
map.put("score","88.5");
map.put("birth","2018-04-11");
Object s=Class.forName("com.lpy.jdbc.beanUtil.Student").newInstance();
BeanUtils.copyProperties(s,map);
System.out.println(s);
}
private static void t2() throws InstantiationException, IllegalAccessException, ClassNotFoundException, InvocationTargetException {
Student s=new Student();
s.setId(1);
s.setGender(true);
s.setName("lpy");
s.setScore(55.5);
s.setBirth(new Date());
Object s2=Class.forName("com.lpy.jdbc.beanUtil.Student").newInstance();
BeanUtils.copyProperties(s2,s);
System.out.println(s2);
}
private static void t1() throws InstantiationException, IllegalAccessException, ClassNotFoundException, InvocationTargetException {
Student s = new Student();
s.setId(1);
s.setGender(true);
s.setName("lpy");
s.setScore(55.5);
s.setBirth(new Date());
//Student s2=new Student();
Object s2 = Class.forName("com.lpy.jdbc.beanUtil.Student").newInstance();
//把属性从s拷贝到s2,不适用get/set
//自定义日期类型转换器
ConvertUtils.register(new Converter() {
@Override
public Object convert(Class type, Object value) {
if (type != Date.class) return null;
if (value == null || "".equals(value.toString().trim())) return null;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日hh:mm:ss");
Date date = null;
try {
date = dateFormat.parse((String) value);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}, Date.class);
BeanUtils.copyProperty(s2, "id", "2");
BeanUtils.copyProperty(s2, "name", s.getName());
BeanUtils.copyProperty(s2, "gender", s.isGender());
BeanUtils.copyProperty(s2, "score", s.getScore());
BeanUtils.copyProperty(s2, "birth", "2018年04月11日14:58:30");
System.out.println(s2);
}
}
补充:更新通讯录代码中对于获取元素并封装到对象中的代码。
package com.contact.util;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
public class WebUtil {
public static <T>T copyRequestToBean(HttpServletRequest request,Class<T> clazz) {
T t=null;
try {
Map map = request.getParameterMap();
t = clazz.newInstance();
BeanUtils.copyProperties(t, map);
} catch (Exception e) {
}
return t;
}
}