美文网首页
通过反射把JavaBean映射到map

通过反射把JavaBean映射到map

作者: ssttIsme | 来源:发表于2019-03-26 23:55 被阅读0次

    通过反射吧一个JavaBean中的属性和属性值拷贝到Map集合中

    遍历所有get方法,得到方法名,有此方法名进行截取,得到name作为map的key。执行此方法,得到返回值作为map的value。

    package reflectmap;
    
    public class Product {
        private int id;
        private String name;
        private String kind;
        private double price;
        public Product() {
        }
        public Product(int id, String name, String kind, double price) {
            this.id = id;
            this.name = name;
            this.kind = kind;
            this.price = price;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getKind() {
            return kind;
        }
        public void setKind(String kind) {
            this.kind = kind;
        }
        public double getPrice() {
            return price;
        }
        public void setPrice(double price) {
            this.price = price;
        }
        
    
    }
    
    
    package reflectmap;
    
    import java.lang.reflect.Method;
    import java.util.HashMap;
    import java.util.Map;
    
    public class CopyToMap {
        public static void copy(Object obj,Map<String,Object>map) throws Exception{
            //实现从obj复制数据到map中
            Class<?> cls = obj.getClass();
            //查找该类中的get方法
            Method[] methods = cls.getDeclaredMethods();
            //判断是否为get方法
            for (Method m : methods) {
                // getName get Name
                String name = m.getName();
                if(name.startsWith("get")){
                    //截取name得到key
                    System.out.println("origin :"+name);
                    name=name.substring(3);
                    System.out.println("withoutget :"+name);
                    String first=name.substring(0, 1).toLowerCase();
                    System.out.println("first :"+first);
                    String last=name.substring(1);
                    System.out.println("last :"+last);
                    String key=first+last;
                    //执行方法得到value
                    Object value = m.invoke(obj);
                    map.put(key, value);
                    System.out.println();
                }
            }
            
        }
        public static void main(String[] args)throws Exception {
            Product p=new Product(1,"九口南红","南红",1.1);
            Map<String,Object> map=new HashMap<>();
            copy(p, map);
            System.out.println(map);
        }
    }
    
    

    运行结果

    origin :getName
    withoutget :Name
    first :n
    last :ame
    
    origin :getId
    withoutget :Id
    first :i
    last :d
    
    origin :getPrice
    withoutget :Price
    first :p
    last :rice
    
    origin :getKind
    withoutget :Kind
    first :k
    last :ind
    
    {id=1, price=1.1, name=九口南红, kind=南红}
    

    去掉日志后

    package reflectmap;
    
    import java.lang.reflect.Method;
    import java.util.HashMap;
    import java.util.Map;
    
    public class CopyToMap {
        public static void copy(Object obj,Map<String,Object>map) throws Exception{
            //实现从obj复制数据到map中
            Class<?> cls = obj.getClass();
            //查找该类中的get方法
            Method[] methods = cls.getDeclaredMethods();
            //判断是否为get方法
            for (Method m : methods) {
                // getName get Name
                String name = m.getName();
                if(name.startsWith("get")){
                    //截取name得到key
                    name=name.substring(3);
                    String first=name.substring(0, 1).toLowerCase();
                    String last=name.substring(1);
                    String key=first+last;
                    //执行方法得到value
                    Object value = m.invoke(obj);
                    map.put(key, value);
                }
            }
            
        }
        public static void main(String[] args)throws Exception {
            Product p=new Product(1,"九口南红","南红",1.1);
            Map<String,Object> map=new HashMap<>();
            copy(p, map);
            System.out.println(map);
        }
    }
    
    

    相关文章

      网友评论

          本文标题:通过反射把JavaBean映射到map

          本文链接:https://www.haomeiwen.com/subject/dmwovqtx.html