BeanUtils包的使用

2017-05-30  本文已影响0人  LinkedIn

The Java language provides Reflection and Introspection APIs (see the java.lang.reflect and java.beans packages in the JDK Javadocs). However, these APIs can be quite complex to understand and utilize. The BeanUtils component provides easy-to-use wrappers around these capabilities.
简单的说,BeanUtils 简化了对于bean的操作。提高了开发效率。
(1) BeanUtils相关包

commons-beanutils-1.8.3.jar

commons-beanutils-1.8.3-javadoc.jar

commons-beanutils-1.8.3-javadoc.jar

commons-beanutils-bean-collections-1.8.3.jar

commons-beanutils-core-1.8.3.jar

(2) 下面列举开发常用的例子:

package comm.dbutils;
//封bean包
import java.util.Date;

public class user {
    private String username;
    private String password;
    private int qqnumber;
    private Date date;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public int getQqnumber() {
        return qqnumber;
    }
    public void setQqnumber(int qqnumber) {
        this.qqnumber = qqnumber;
    }
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    @Override
    public String toString() {
        return "user [username=" + username + ", password=" + password
                + ", qqnumber=" + qqnumber + ", date=" + date + "]";
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((date == null) ? 0 : date.hashCode());
        result = prime * result
                + ((password == null) ? 0 : password.hashCode());
        result = prime * result + qqnumber;
        result = prime * result
                + ((username == null) ? 0 : username.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        user other = (user) obj;
        if (date == null) {
            if (other.date != null)
                return false;
        } else if (!date.equals(other.date))
            return false;
        if (password == null) {
            if (other.password != null)
                return false;
        } else if (!password.equals(other.password))
            return false;
        if (qqnumber != other.qqnumber)
            return false;
        if (username == null) {
            if (other.username != null)
                return false;
        } else if (!username.equals(other.username))
            return false;
        return true;
    }
    
}

(3) bean的读取

package comm.dbutils;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;

/**
 * @author DGW
 * @date 2017 2017年5月30日 下午2:44:45
 * @filename basicContrll.java  @TODO
 */
public class basicContrll {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        Class cls = Class.forName("comm.dbutils.user");
        user instance = (user) cls.newInstance();
        BeanUtils.setProperty(instance, "username", "dgw");
        String string = BeanUtils.getProperty(instance, "username");
        System.out.println(string);
    }

}

(4) 带有时间控制器的转换

package comm.dbutils;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

/**
 * @author DGW
 * @date 2017 2017年5月30日 下午2:44:45
 * @filename basicContrll.java @TODO
 */
public class basicContrll {
    public static void main(String[] args) throws ClassNotFoundException,
            InstantiationException, IllegalAccessException,
            InvocationTargetException, NoSuchMethodException {
        Class cls = Class.forName("comm.dbutils.user");
        user instance = (user) cls.newInstance();
        // 自带时间转换器
        // ConvertUtils.register(new DateLocaleConverter(), Date.class);
        // 自定义时间控制器
        ConvertUtils.register(new Converter() {
            @Override
            public Object convert(Class type, Object value) {
                // 当value参数等于空时返回空
                if (value == null) {
                    return null;
                }
                // 自定义时间的格式为yyyy-MM-dd
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
                // 创建日期类对象
                Date dt = null;
                try {
                    // 使用自定义日期的格式转化value参数为yyyy-MM-dd格式
                    dt = sdf.parse((String) value);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                // 返回dt日期对象
                return dt;
            }
        }, Date.class);
        BeanUtils.setProperty(instance, "date", "1997-22-22");
        String string = BeanUtils.getProperty(instance, "date");
        System.out.println(string);
    }

}

(5) 嵌套方法的结果

    @Test
    public void demo() {
        user user = new user();
        try {
            BeanUtils.setProperty(user, "username", "大神");
            //转换为map
            Map<String, String> map = BeanUtils.describe(user);
            System.out.println(map.get("username"));
            //执行方法
            MethodUtils.invokeMethod(user, "speak", null);
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
上一篇下一篇

猜你喜欢

热点阅读