zoukankan      html  css  js  c++  java
  • 根据类名与字段名称取值(可用于循环取实体所有值非常好用)

    /**

    * 根据字段名称取值

    * 

    * @param obj 类名

    * @param fieldName 属性名

    * @return

    */

    public static Object getClassValue(Object obj, String fieldName) {

    if (obj == null) {

    return null;

    }

    try {

    Class beanClass = obj.getClass();

    Method[] ms = beanClass.getMethods();

    for (int i = 0; i < ms.length; i++) {

    // 非get方法不取

    if (!ms[i].getName().startsWith("get")) {

    continue;

    }

    Object objValue = null;

    try {

    objValue = ms[i].invoke(obj, new Object[] {});

    } catch (Exception e) {

    // logger.info("反射取值出错:" + e.toString());

    continue;

    }

    if (objValue == null) {

    continue;

    }

    if (ms[i].getName().toUpperCase().equals(fieldName.toUpperCase())

    || ms[i].getName().substring(3).toUpperCase().equals(fieldName.toUpperCase())) {

    return objValue;

    } else if (fieldName.toUpperCase().equals("SID") && (ms[i].getName().toUpperCase().equals("ID")

    || ms[i].getName().substring(3).toUpperCase().equals("ID"))) {

    return objValue;

    }

    }

    } catch (Exception e) {

    // logger.info("取方法出错!" + e.toString());

    }

    return null;

    }

  • 相关阅读:
    hdu1686 最大匹配次数 KMP
    洛谷 P5057 [CQOI2006]简单题(树状数组)
    洛谷 P5020 货币系统
    洛谷 P5019 铺设道路(差分)
    洛谷 P1119 灾后重建(Floyd)
    洛谷 P1082 同余方程(同余&&exgcd)
    洛谷 P2384 最短路
    洛谷 P3371 【模板】单源最短路径(弱化版) && dijkstra模板
    洛谷 P1387 最大正方形
    洛谷 P2866 [USACO06NOV]糟糕的一天Bad Hair Day
  • 原文地址:https://www.cnblogs.com/mayanze/p/8595891.html
Copyright © 2011-2022 走看看