转自: http://blog.youkuaiyun.com/qq_35448976/article/details/77816420
1、注意点
1.1、使用BeanUtils组件必须引入BeanUtils核心包,还需要引入日志支持包
1.2、BeanUtils的copyProperty与setProperty的实现原理-反射
1.3、对于基本基本数据类型,会自动进行类型转换
1.4、BeanUtils的populate拷贝map数据到对象中时,map中的key要与javabean的属 性名称一致
1.5、可以与servelt混用,request.getParameterMap()方法会的map集合,再用BeanUtils 的组件直接将map对象保存到javabean中
2、BeanUtils组件
2.1、简介
程序中对javabean的操作很频繁, 所以apache提供了一套开源的api,方便对javabean的操作!即BeanUtils组件。
2.2、BeanUtils组件作用
简化javabean的操作!
用户可以从www.apache.org下载BeanUtils组件,然后再在项目中引入jar文件!
2.3、使用BenUtils组件:
1. 引入commons-beanutils-1.8.3.jar核心包
2. 引入日志支持包: commons-logging-1.1.3.jar
2.4、如果缺少日志jar文件,报错:
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.commons.beanutils.ConvertUtilsBean.<init>(ConvertUtilsBean.java:157)
at org.apache.commons.beanutils.BeanUtilsBean.<init>(BeanUtilsBean.java:117)
at org.apache.commons.beanutils.BeanUtilsBean$1.initialValue(BeanUtilsBean.java:68)
at
3、实例, 基本用法
方法1: 对象属性的拷贝
BeanUtils.copyProperty(admin, "userName", "jack");
BeanUtils.setProperty(admin, "age", 18);
方法2: 对象的拷贝
BeanUtils.copyProperties(newAdmin, admin);
方法3: map数据拷贝到javabean中
【注意:map中的key要与javabean的属性名称一致】
BeanUtils.populate(adminMap, map);
-
- @Test
- public void test1() throws Exception {
-
- Admin admin = new Admin();
-
-
-
- BeanUtils.copyProperty(admin, "userName", "jack");
- BeanUtils.setProperty(admin, "age", 18);
-
-
- Admin newAdmin = new Admin();
- BeanUtils.copyProperties(newAdmin, admin);
-
- Admin adminMap = new Admin();
- Map<String,Object> map = new HashMap<String,Object>();
- map.put("userName", "Jerry");
- map.put("age", 29);
-
- BeanUtils.populate(adminMap, map);
-
- System.out.println(adminMap.getUserName());
- System.out.println(adminMap.getAge());
- }
4、实例, 日期类型的拷贝
需要注册日期类型转换器,2种方式参见下面代码:
- public class App {
-
-
- @Test
- public void test1() throws Exception {
-
- Admin admin = new Admin();
-
-
-
- BeanUtils.copyProperty(admin, "userName", "jack");
- BeanUtils.setProperty(admin, "age", 18);
-
-
- Admin newAdmin = new Admin();
- BeanUtils.copyProperties(newAdmin, admin);
-
- Admin adminMap = new Admin();
- Map<String,Object> map = new HashMap<String,Object>();
- map.put("userName", "Jerry");
- map.put("age", 29);
-
- BeanUtils.populate(adminMap, map);
-
- System.out.println(adminMap.getUserName());
- System.out.println(adminMap.getAge());
- }
-
- @Test
- public void test2() throws Exception {
-
- String name = "jack";
- String age = "20";
- String birth = " ";
-
- Admin admin = new Admin();
-
- 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;
- }
- try {
-
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- return sdf.parse(value.toString());
- } catch (ParseException e) {
- throw new RuntimeException(e);
- }
- }
- },Date.class);
-
- BeanUtils.copyProperty(admin, "userName", name);
- BeanUtils.copyProperty(admin, "age", age);
- BeanUtils.copyProperty(admin, "birth", birth);
-
- System.out.println(admin);
- }
-
- @Test
- public void test3() throws Exception {
-
- String name = "jack";
- String age = "20";
- String birth = null;
-
- Admin admin = new Admin();
-
- ConvertUtils.register(new DateLocaleConverter(), Date.class);
-
- BeanUtils.copyProperty(admin, "userName", name);
- BeanUtils.copyProperty(admin, "age", age);
- BeanUtils.copyProperty(admin, "birth", birth);
-
- System.out.println(admin);
- }
- }
5、应用
- public class WebUtils {
-
- @Deprecated
- public static <T> T copyToBean_old(HttpServletRequest request, Class<T> clazz) {
- try {
-
- T t = clazz.newInstance();
-
- Enumeration<String> enums = request.getParameterNames();
-
- while (enums.hasMoreElements()) {
-
- String name = enums.nextElement();
-
- String value = request.getParameter(name);
-
- BeanUtils.copyProperty(t, name, value);
- }
- return t;
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
-
-
- public static <T> T copyToBean(HttpServletRequest request, Class<T> clazz) {
- try {
-
-
- T t = clazz.newInstance();
- BeanUtils.populate(t, request.getParameterMap());
- return t;
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- }