java利用反射动态获取实体类的属性值

直接贴代码吧,有需要的话,可以根据自己的需要修改部分代码:

public BigDecimal getByAttributeName(ThmdGwqriR thmdGwqriR, String attributeName){
        for (Field field : thmdGwqriR.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            try{
                if (attributeName.toLowerCase().equals(field.getName().toLowerCase())){
                    return new BigDecimal(field.get(thmdGwqriR).toString());
                }
            }
            catch (Exception e){
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

猜你喜欢

转载自www.cnblogs.com/codecat/p/11497485.html
今日推荐