---
title: "apache-commons-beanutils学习"
author: "prodream"
date: "2010-04-30T17:54:06Z"
tags: []
categories: ["编程语言"]
slug: "apache-commons-beanutils"
draft: false
lastmod: "2010-04-30T17:54:06Z"
id: 657220
url: "https://prodream.iteye.com/blog/657220"
---

前言:掌握apache的commons的常用的工具后，你会发现开发起来顺手多了...

 

 

一.框架介绍

 

beanutils主要用来处理java bean 开发包，提供java反射和API自省的包装。

 

 

二.使用场景

 

对表单的处理，jdbc后的数据

 

 

三.运用

 

1.属性值读取,写入

 

- PropertyUtils.getSimpleProperty(Object bean, String name)
- PropertyUtils.setSimpleProperty(Object bean, String name, Object value)

  但我更喜欢Ognl...方便.简单,支持复杂的结构（当然beanutils也支持，但没有Ognl方便...）

 

         Ognl.setValue( String expression, Object  root, Object  value)

         Ognl.getValue( String expression, Object root)

 

2\. 包装ResultSet 对象--包装jdbc的返回类型对象

  

         ResultSetDynaClass(ResultSet rs).iterator()

         RowSetDynaClass(ResultSet rs).getRows()

 

      大多的时候我们使用RowSetDynaClass,因为使用ResultSetDynaClass的时候，必须保持ResultSet没有close()

    

 

```
ResultSet rs = stmt.executeQuery ("select account_id, name from customers");
```

```
 Iterator rows = (new ResultSetDynaClass(rs)).iterator();
```

```
 List rows=(new (RowSetDynaClass(rs)).getRows();
```

 

3\. 将一个bean包装成一个dynabean,便于使用

 

```
MyBean bean = ...;
    DynaBean wrapper = new WrapDynaBean(bean);
    String firstName = wrapper.get("firstName");
```

 

dynabean是一种动态的Bean,他的使用非常像Map的操作

 

```
DynaBean car = new LazyDynaBean();
         car.set("carNo",1);
         car.set("owner","张三");         out.print(car.get("carNo"));
```

 

     Lazy  DynaBeans很强大，可以减除非常VO,formBean的带来的麻烦

- 1\. LazyDynaBean - A *Lazy* DynaBean
- 2\. LazyDynaMap - A *light weight* DynaBean facade to a Map with *lazy* map/list processing
- 3\. LazyDynaList - A *lazy list* for DynaBean's, `java.util.Map`'s or POJO beans.
- 4\. LazyDynaClass - A MutableDynaClass implementation.

4.数据类型转化

 

```
BeanUtils.populate(bean, map); //将Map中的数据，填入到bean中..强大吧..
```

```
ConvertUtils.convert("2010-4-30",Date.class);  //转化为时间类
```

```
ConvertUtils.convert("20.02",Double.class);  //转化为double	 
```

 当然你也可以自己定义转化的方法

 

 ConvertUtils.register(new Converter(){

   @Override  
   public Object convert(Class type, Object value) {  
    // TODO Auto-generated method stub  
      return null;  
     }

  }, Date.class);

 

5.对collection集合进行操作

 

  批量更改：

 

```
// create the closure
    BeanPropertyValueChangeClosure closure =
        new BeanPropertyValueChangeClosure( "activeEmployee", Boolean.TRUE );
 
    // update the Collection
    CollectionUtils.forAllDo( peopleCollection, closure );
```

  

 

查找/过滤：

 

  

```
BeanPropertyValueEqualsPredicate predicate =
        new BeanPropertyValueEqualsPredicate( "activeEmployee", Boolean.FALSE );
 
    // filter the Collection
    CollectionUtils.filter( peopleCollection, predicate );
```

 转化：

 

 

```
// create the transformer
    BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer( "person.address.city" );
 
    // transform the Collection
    Collection peoplesCities = CollectionUtils.collect( peopleCollection, transformer );
```