博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
根据类名与字段名称取值(可用于循环取实体所有值非常好用)
阅读量:7285 次
发布时间:2019-06-30

本文共 1019 字,大约阅读时间需要 3 分钟。

/**

* 根据字段名称取值

* 

* @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;

}

转载于:https://www.cnblogs.com/mayanze/p/8595891.html

你可能感兴趣的文章
网站运营的4点经验
查看>>
电信运营商的流量增值——互联网广告
查看>>
查看硬盘物理序列号的程序源代码
查看>>
Debian查看启动日志
查看>>
haproxy配置详解以及动静分离的实现
查看>>
1.2 Zookeeper伪集群安装
查看>>
查看客户端域策略应用结果
查看>>
美团外卖Android平台化的复用实践
查看>>
美团即时物流的分布式系统架构设计
查看>>
流行语折射科技新活力
查看>>
Zabbix高级应用二、监控磁盘阵列、Exchange队列、DAG
查看>>
Ubuntu16.04LTS上搭建Sentry
查看>>
oracle查看表空间大小及表数量
查看>>
js 常用提示 console.log & console.info
查看>>
php stdClass 转数组
查看>>
优化NGINX的25种手段
查看>>
svn安装
查看>>
动态容器 数组 api
查看>>
抽取IPA包里的所有图片,包括.car压缩包中的文件
查看>>
DFS lock handle等待事件
查看>>