关键字:Java|Map|Copy|VO|Bean

摘要:这段代码主要功能是把Map中的值复制到VO(或一个普通Bean)对象,这个VO对象必须要有setter方法,在程序中传递转换存储值时有一些用处。

代码如下:

定义一个TestVO类。

package xc.utils;

import java.util.Date;

public class TestVO {
    private String name;
    private int age;
    private double num;
    private Date today;

    public TestVO() {
    }

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

    public void setAge(int age) {
        this.age = age;
    }

    public void setNum(double num) {
        this.num = num;
    }

    public void setToday(Date today) {
        this.today = today;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public double getNum() {
        return num;
    }

    public Date getToday() {
        return today;
    }
}

值拷贝函数copyVO,定义在ObjectUtil中。

/**
     * 复制Map中的值到VO(map的Key全部大写)
     * @param map
     * @param vo
     */
    public static void copyVO(Map<String,Object> map, Object vo) {
        try {
            if (map==null || vo==null) {
                System.out.println("VOUtil.copyVO 参数[map] or [vo] is null");
                return;
            }

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Method[] methodList = vo.getClass().getDeclaredMethods();
            String key = "", vType="", mName="", mType="", mValue;
            Object value = null;

            for (Method m : methodList) {
                mName = m.getName();
                if (mName.startsWith("set")) {
                    mName = m.getName().substring(3).toLowerCase();
                    key = mName;

                    //这是我额外的处理,可删除下面这段,如果名称末两位是“dm”且前面一位不是下划线,尝试“dm”前加下划线去取值。
                    int len = mName.length();
                    if (mName.substring(len-2, len).equalsIgnoreCase("dm") && !mName.substring(len-3, len-2).equalsIgnoreCase("_")) {
                        key = (mName.substring(0,len-2) + "_" + mName.substring(len-2, len)).toUpperCase();
                        if (!map.containsKey(key))
                            key = mName;
                    }
                    if (!map.containsKey(key))
                        continue;

                    value = map.get(key);
                    if (value == null)
                        continue;

                    vType = value.getClass().getCanonicalName();
                    mType = m.getParameterTypes()[0].getCanonicalName();
                    m.invoke(vo, value);
                    
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

定义在main函数中的测试代码。

public static void main(String[] args)
    {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("num", 12.01);
            map.put("age", 30);
            map.put("today", new Date());
            map.put("name", "Cat");

            TestVO vo = new TestVO();
            ObjectUtil.copyVO(map, vo);

            System.out.println("num: "+vo.getNum());
            System.out.println("age: "+vo.getAge());
            System.out.println("today: "+vo.getToday());
            System.out.println("name: "+vo.getName());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }