将Entity转为Map类型

来源:互联网 发布:sst nc数据下载 编辑:程序博客网 时间:2024/05/22 03:33
1、实体类
import java.util.Date;


public class Game {
    private int id;
    private String gameName;
    private Date time;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getGameName() {
        return gameName;
    }
    public void setGameName(String gameName) {
        this.gameName = gameName;
    }
    public Date getTime() {
        return time;
    }
    public void setTime(Date time) {
        this.time = time;
    }
    public String toString() {
        return "Game [id=" + id + ", gameName=" + gameName + ", time=" + time
                + "]";
    }
}
2、处理方法
public class Swap {
    public static void main(String[] args) {
        Game game = new Game();
        game.setId(1);
        game.setGameName("梦幻西游");
        game.setTime(new Date());
        // 将对象转为实体
        System.out.println(go(game));
        // {id=1, time=Wed Apr 27 11:20:46 CST 2016, class=class Game, gameName=梦幻西游}
    }

    public static Map<String, Object> go(Object obj){
        Class c = null;
        Map<String, Object> dataMap = new HashMap<String, Object>();
        try {
            c = Class.forName(obj.getClass().getName());
            //System.out.println(c);//class Game
            Method[] methodArr = c.getMethods();
            for (Method method : methodArr) {
                /*
                 *     所有方法:
                 *  public java.lang.String Game.toString()
                    public int Game.getId()
                    public void Game.setId(int)
                    public void Game.setGameName(java.lang.String)
                    public void Game.setTime(java.util.Date)
                    public java.lang.String Game.getGameName()
                    public java.util.Date Game.getTime()
                 */
                //System.out.println(method);
                /*  method.getName:所有方法名
                 *  toString
                    getId
                    setId
                    setGameName
                    setTime
                    getGameName
                    getTime
                    wait
                    wait
                    wait
                    equals
                    hashCode
                    getClass
                    notify
                    notifyAll
                 */
                //System.out.println(method.getName());
                String methodName = method.getName();
                if (methodName.startsWith("get")){
                    /*  method.invoke(obj):
                     *  1
                        梦幻西游
                        Wed Apr 27 11:09:15 CST 2016
                        class Game

                     */
                    Object value = method.invoke(obj);
                    // 从第三位开始截取
                    String key = methodName.substring(3);
                    // 经典
                    key = key.substring(0,1).toLowerCase() + key.substring(1);
                    //System.out.println(key);// id gameName, time class
                    if (null != value){
                        /*
                         *  java.lang.Integer
                            java.lang.String
                            java.util.Date
                            java.lang.Class
                         */
                        //System.out.println(value.getClass().getName());
                        if(value.getClass().getClassLoader() != null){  //处理自定义的对象类型
                            go(value);
                        }
                        if("java.util.Date".equals(value.getClass().getName())){
                           // value = DateUtil.format((Date)value);
                          //System.out.println(value.getClass().getName());

                        }
                        dataMap.put(key, value == null ? "" : value);
                    } else {
                        dataMap.put(key, "");
                    }
                }
            }
        } catch (Exception e) {
        }
        return dataMap;

    }
}

原创粉丝点击