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;

    }

  • 相关阅读:
    学习Mybatis中的一对多表关联
    学习Mybatis中的一对一表关联
    学习Mybatis中的动态sql
    学习Mybatis中的约定大于配置、数据库配置优化、定义别名、类型处理器、resultMap和parameterType
    第八周进度
    构建之法阅读笔记07
    正则表达式
    梦断代码之阅读笔记02
    顶会热词统计
    本周进度
  • 原文地址:https://www.cnblogs.com/mayanze/p/8595891.html
Copyright © 2011-2022 走看看