获取任意泛型类的某个属性

这里我以id属性为例,从表中取到(任意)对象集合后,我想将其中的某一属性全部取出。

下面是利用反射的代码实现

    @org.junit.Test
    public void classTest(){
        TbUser tbUser1 = new TbUser();
        tbUser1.setId(new Long(12));
//        EntityWrapper ew = new EntityWrapper();
//        ew.ge("id",7);
//        List tbUserList = tbUserMapper.selectList(ew);
//        tbUserList.forEach(x ->{
//            System.out.println(x.getUsername());
//        } );
        List tbUserList = tbUserMapper.selectPage(new Page(1, 10),new EntityWrapper());
        List IdList = getIdList(tbUserList);
        System.out.println(IdList.size());
        for (String s : IdList){
            System.out.println(s);
        }
    }

    private  List getIdList(List tList){
        List idList = new ArrayList<>();
        for (T t : tList){
            try {
                idList.add(t.getClass().getMethod("getId").invoke(t).toString());
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return idList;
    }

这里我是将取id写死了,当然也可以做一些修改以扩展其适用范围

另附lamda表达式写法(这里需要注意,子类的私有属性在这应该不合适)

    public void classTest(){
        TbUser tbUser1 = new TbUser();
        tbUser1.setId(new Long(12));
//        EntityWrapper ew = new EntityWrapper();
//        ew.ge("id",7);
//        List tbUserList = tbUserMapper.selectList(ew);
//        tbUserList.forEach(x ->{
//            System.out.println(x.getUsername());
//        } );
        List tbUserList = tbUserMapper.selectPage(new Page(1, 10),new EntityWrapper());
//        List IdList = getIdList(tbUserList);
        List IdList = getIdList1(tbUserList);
        System.out.println(IdList.size());
        for (Long s : IdList){
            System.out.println(s);
        }
    }


    /**
     *   Lamda 表达式test
     */
    public static List getIdList1(List tList){
        return tList.stream().map(BaseEntity :: getId).collect(Collectors.toList());
    }


public class BaseEntity> implements Serializable {
    private static final Long serialVersionUID = 1L;
    private Long id;

    public static Long getSerialVersionUID() {
        return serialVersionUID;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

这里我又做了些修改,支持同时访问父类和子类属性,如果有多继承的话,还需要再修改 

    private  List getIdList(List tList,String field,Boolean isSuperfield){

        if(org.apache.commons.lang.StringUtils.isBlank(field))
            return null;

        //拼接方法
        field = new StringBuffer("get").append(field.substring(0,1).toUpperCase()).append(field.substring(1)).toString();
        List idList = new ArrayList<>();
        Method method = null;
        try {
            if (isSuperfield)
                method = tList.get(0).getClass().getSuperclass().getMethod(field);
            else {
                method = tList.get(0).getClass().getMethod(field);
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        try {
            for (T t : tList){
                Object s = method.invoke(t);
                idList.add(s);
            }
        }catch (NullPointerException e){
            e.printStackTrace();
        }catch (InvocationTargetException e){
            e.printStackTrace();
        }catch (IllegalAccessException e){
            e.printStackTrace();
        }
        return idList;
    } 
  

0.0(一曲肝肠断,天涯何处觅知音)

你可能感兴趣的:(获取任意泛型类的某个属性)