现在的位置: 首页 > 综合 > 正文

Commons-beanutils使用指南

2018年05月09日 ⁄ 综合 ⁄ 共 4966字 ⁄ 字号 评论关闭

一、commons-beanutils的简要介绍

commons-beanutils是一组使用程序的集合,它能容易的处理bean及属性.该工具允许你以属性名检索bean属性、根据属性排列bean、将bean转换为Map等等。



二、实例说明

1. Author

package org.abu.commons.beanutils;

import java.util.Date;

public class Author {
    private String name;
    private String email;
    private Date birth;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

}

 

2.Book

package org.abu.commons.beanutils;

import java.util.Date;
import java.util.List;

public class Book {

    private String isbn;
    private String bookName;
    private List<Author> authors;
    private String publisher;
    private Date publishDate;

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public List<Author> getAuthors() {
        return authors;
    }

    public void setAuthors(List<Author> authors) {
        this.authors = authors;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public Date getPublishDate() {
        return publishDate;
    }

    public void setPublishDate(Date publishDate) {
        this.publishDate = publishDate;
    }

}

 

3.测试

 

package org.abu.commons.beanutils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;

public class BeanUtilsTest {

    private Book book = new Book();
    private Book copiedBook = new Book();   
   
    private List<Author> authors = new ArrayList<Author>();

    public BeanUtilsTest() {

        // 定义三个作者


        Author author1 = new Author();
        Author author2 = new Author();
        Author author3 = new Author();

        author1.setName("Liky");
        author1.setEmail("liky@yahoo.com");
        author1.setBirth(new Date());

        author2.setName("John");
        author2.setEmail("john@yahoo.com");
        author2.setBirth(new Date());

        author3.setName("Susan");
        author3.setEmail("susan@yahoo.com");
        author3.setBirth(new Date());

        // book属性


        authors.add(author1);
        authors.add(author2);
        authors.add(author3);

        book.setAuthors(authors);
        book.setIsbn("ISBN 7-096-13964-4");
        book.setBookName("Struts2 in Action");
        book.setPublisher("O'Reilly Media Inc.");
        book.setPublishDate(new Date());
    }

    public static void main(String[] args) throws Exception {
        BeanUtilsTest but = new BeanUtilsTest();

        //but.getProperty();
        but.copyProperties();
    }

    /**
     * 演示使用PropertyUtils来获得属性,
     * 注意获得属性有三种方式,分别是:
     * 1.getSimpleProperty 用来获取基本数据类型的属性
     * 2.getMappedProperty 用来获取Map类型的属性,
     * 例如:Book有一个Map类型的author,而author中有三个key,其中一个是name.
     * 那么就可以这样来使用getMappedProperty(book, "author", "name");
     * 3.getNestedProperty 用来获取嵌套的属性.
     * 也就是说如果Book有一个Author的属性,而Author又是另一个Bean,Author有个属性是name,
     * 那么就可以这样来使用getNestedProperty(book,"author.name");
     *
     * @throws Exception
     */



    @SuppressWarnings("unchecked")
    public void getProperty() throws Exception {
        // 获得Bean的简单属性


        String bookName = (String) PropertyUtils.getSimpleProperty(book,
                "bookName");
        String publisher = (String) PropertyUtils.getSimpleProperty(book,
                "publisher");
        String isbn = (String) PropertyUtils.getSimpleProperty(book, "isbn");
        Date publishDate = (Date) PropertyUtils.getSimpleProperty(book,
                "publishDate");

        StringBuffer bookString = new StringBuffer();
        bookString.append("book:/n").append(
                "bookName: " + bookName + "/npublisher: " + publisher
                        + "/nisbn: " + isbn + "/npublish date: " + publishDate + "/n/n");

        // 获得嵌套的属性,这里的Book中有一个Authors的属性,它是Author这个Bean的列表
        // 如果要获得嵌套的属性,那么必须使用getNestedProperty这个方法



        System.out.println(bookString);
        List<Author> authorList = (List<Author>) PropertyUtils
                .getNestedProperty(book, "authors");
       
        StringBuffer authorsString = new StringBuffer();

        for (int i = 0; i < authorList.size(); i++) {
            Author author = authorList.get(i);
            authorsString.append("author" + i + ":/n");
            authorsString.append("name:"
                    + PropertyUtils.getSimpleProperty(author, "name") + "/n");
            authorsString.append("email:"
                    + PropertyUtils.getSimpleProperty(author, "email") + "/n");
            authorsString.append("birth:"
                    + PropertyUtils.getSimpleProperty(author, "birth") + "/n/n");
        }
       
        System.out.println(authorsString);
    }
   
    /**   
     * 我们来测试是否book会复制属性给copiedBook.
     * @throws Exception
     */



    public void copyProperties() throws Exception {
        PropertyUtils.copyProperties(copiedBook, book);
       
        StringBuffer sb = new StringBuffer();
       
        sb.append("book:/n").append("bookName: " + copiedBook.getBookName()).append("/npublisher: " + copiedBook.getPublisher()).append("/npublish date: " + copiedBook.getPublishDate());
       
        System.out.println(sb);
    }
}

【上篇】
【下篇】

抱歉!评论已关闭.