一 简介
1 程序中对javabean的操作很频繁, 所以apache提供了一套开源的api,方便对javabean的操作!即BeanUtils组件。
BeanUtils组件, 作用是简化javabean的操作!
2 使用组件
BeanUtils组件 maven下载:
commons-beanutils
commons-beanutils
1.8.3
logging组件(日志支持包) maven下载:
commons-logging
commons-logging
1.1.3
如果缺少日志包将会报错
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.commons.beanutils.ConvertUtilsBean.(ConvertUtilsBean.java:157)
at org.apache.commons.beanutils.BeanUtilsBean.(BeanUtilsBean.java:117)
at org.apache.commons.beanutils.BeanUtilsBean$1.initialValue(BeanUtilsBean.java:68)
二 基本用法
方法1: 对象属性的拷贝
BeanUtils.copyProperty(student, "name", "jack");
BeanUtils.setProperty(student, "password", 123);
方法2: 对象的拷贝
BeanUtils.copyProperties(newStudent, student);
方法3: map数据拷贝到javabean中
【注意:map中的key要与javabean的属性名称一致】
BeanUtils.populate(studentMap, map);
import entity.Student;
import org.apache.commons.beanutils.BeanUtils;
import java.util.Map;
import java.util.HashMap;
/**
* Created by pc on 2017/7/25.
*/
public class BeanUtilsTest {
public static void main(String [] args){
try {
//a.基本操作
Student student=new Student();
//student.setId(1);
//student.setName("Jack");
//student.setPassword("123");
//b.BeanUtils组件实现对象属性拷贝
BeanUtils.copyProperty(student,"id",1);
BeanUtils.setProperty(student,"name","Tom");
//总结1: 对于基本数据类型,会自动进行类型转换
//c.对象的拷贝
Student newStudent =new Student();
BeanUtils.copyProperties(newStudent,student);
//d.map数据,拷贝对象
Student studentMap=new Student();
Map map = new HashMap();
map.put("id",02);
map.put("name","Wang");
map.put("password","123");
//注意:map中的key要与javabean的属性名称一致
BeanUtils.populate(studentMap,map);
//测试
System.out.println(studentMap.getName());
System.out.println(studentMap.getId());
System.out.println(studentMap.getPassword());
} catch (Exception e) {
throw new RuntimeException();
}
}
}
实例,日期类型的拷贝
package utils;
import entity.Student;
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.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by pc on 2017/7/25.
*/
public class BeanUtilsTime {
public static void main(String [] args) throws Exception{
//模拟表单
int id=1;
String name="Jack";
String password="123";
String brith="1997-01-02";
//对象
Student student=new Student();
//注册类型转换器:1.自定义方法
ConvertUtils.register(new Converter() {
//转换的内部实现方法,需要重写
public Object convert(Class type, Object value) {
//判断
if (type!= Date.class){
return null;
}
if (value == null || "".equals(value.toString().trim())){
return null;
}
try {
//字符串转日期
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
return sdf.parse(value.toString());
}catch (Exception e){
throw new RuntimeException(e);
}
}
},Date.class);
// 把表单提交的数据,封装到对象中
BeanUtils.copyProperty(student,"id",id);
BeanUtils.copyProperty(student,"name",name);
BeanUtils.copyProperty(student,"password",password);
BeanUtils.copyProperty(student,"brith",brith);
System.out.println(student);
}
public void test() throws Exception {
// 模拟表单数据
int id=1;
String name="Jack";
String password="123";
String brith="1997-01-02";
// 对象
Student student1 = new Student();
// 注册日期类型转换器:2, 使用组件提供的转换器工具类
ConvertUtils.register(new DateLocaleConverter(), Date.class);
// 把表单提交的数据,封装到对象中
BeanUtils.copyProperty(student1, "id", id);
BeanUtils.copyProperty(student1, "name", name);
BeanUtils.copyProperty(student1, "password", password);
BeanUtils.copyProperty(student1, "birth", brith);
//------ 测试------
System.out.println(student1);
}
}
三 应用(javaBean操作封装)
package utils;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
/**
* Created by pc on 2017/7/25.
*/
public class WebUtils {
public static T copyToBean(HttpServletRequest request,Class tClass){
try {
//创建对象
T t=tClass.newInstance();
//获取所有表单元素的名称
Enumeration enumeration=request.getParameterNames();
//遍历
while (enumeration.hasMoreElements()){
//获取表单元素名称
String name=enumeration.nextElement();
//获取名称对应的值
String value=request.getParameter(name);
//把指定属性名称对应的值进行拷贝
BeanUtils.copyProperty(t,name,value);
}
return t;
}catch (Exception e){
throw new RuntimeException(e);
}
}
}
测试:Student student= WebUtils.copyToBean(request,Student.class);
封装方法二
public static T copyToBean(HttpServletRequest request, Class clazz) {
try {
// (注册日期类型转换器)
// 创建对象
T t = clazz.newInstance();
BeanUtils.populate(t, request.getParameterMap());
return t;
} catch (Exception e) {
throw new RuntimeException(e);
}
}