热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

对象转map工具类BeanUtil

1、2、当isAccessible()的结果是false时不允许通过反射访问private变量。packagecom.yung.ppapi.util;importjava.be

1、

2、当isAccessible()的结果是false时不允许通过反射访问private变量。

package com.yung.ppapi.util;import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import org.apache.commons.beanutils.BeanMap;
import org.springframework.beans.BeanUtils;import com.yung.ppapi.integration.dto.YCPFExpressDTO;/*** * @author lisheng4**/
public class BeanUtil {public static T clone(Object source, Class type) {try {if (source == null) {return null;}T target = type.newInstance();BeanUtils.copyProperties(source, target);return target;} catch (Exception e) {e.printStackTrace();return null;} } public static T clone(Object source, Class type, String... ignoreProperties) {try {if (source == null) {return null;}T target = type.newInstance();BeanUtils.copyProperties(source, target, ignoreProperties);return target;} catch (Exception e) {e.printStackTrace();return null;} } /*** 利用反射实现* @param map* @param beanClass* @return* @throws Exception*/public static Object mapToObject(Map map, Class beanClass) throws Exception {if (map == null) {return null;}Object obj = beanClass.newInstance();Field[] fields = obj.getClass().getDeclaredFields();for (Field field : fields) {int mod = field.getModifiers();if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {continue;}boolean accessFlag = field.isAccessible();field.setAccessible(true);// 允许通过反射访问该字段field.set(obj, map.get(field.getName()));field.setAccessible(accessFlag);}return obj;}/*** 利用反射实现*

  • 空属性不转换*
  • 超过10万条数据不建议使用* @param obj* @return* @throws Exception*/public static Map objectToMap(Object obj) throws Exception {if (obj == null) {return null;}Map map = new HashMap();Field[] fields = obj.getClass().getDeclaredFields();for (int i = 0, len = fields.length; i map, Class beanClass) throws Exception { if (map == null)return null; Object obj = beanClass.newInstance(); BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { Method setter = property.getWriteMethod(); if (setter != null) { setter.invoke(obj, map.get(property.getName())); } } return obj; } /*** 利用java.beans.Introspector实现* @param obj* @return* @throws Exception*/public static Map objectToMap2(Object obj) throws Exception { if(obj == null) return null; Map map = new HashMap(); BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { String key = property.getName(); if (key.compareToIgnoreCase("class") == 0) { continue; } Method getter = property.getReadMethod(); Object value = getter!=null ? getter.invoke(obj) : null; map.put(key, value); } return map; } /*** 利用org.apache.commons.beanutils.BeanUtils实现* @param map* @param beanClass* @return* @throws Exception*/public static Object mapToObject3(Map map, Class beanClass) throws Exception { if (map == null) return null; Object obj = beanClass.newInstance(); org.apache.commons.beanutils.BeanUtils.populate(obj, map); return obj; } /*** 利用org.apache.commons.beanutils.BeanMap实现* @param obj* @return*/public static Map objectToMap3(Object obj) { if(obj == null) return null; return new BeanMap(obj); } // public static void main(String[] args) throws Exception {
    // List> mapList = new ArrayList>();
    // List> mapList2 = new ArrayList>();
    // List> mapList3 = new ArrayList>();
    // long t11 = System.currentTimeMillis();
    // for(int i=1;i<=100000;i++) {
    // mapList.add(objectToMap(new YCPFExpressDTO()));
    // }
    // long t12 = System.currentTimeMillis();
    //
    // long t21 = System.currentTimeMillis();
    // for(int i=1;i<10000;i++) {
    // mapList2.add((Map) objectToMap2(new YCPFExpressDTO()));
    // }
    // long t22 = System.currentTimeMillis();
    //
    // long t31 = System.currentTimeMillis();
    // for(int i=1;i<10000;i++) {
    // mapList3.add((Map) objectToMap3(new YCPFExpressDTO()));
    // }
    // long t32 = System.currentTimeMillis();
    // System.out.println(t12-t11);
    // System.out.println(t22-t21);
    // System.out.println(t32-t31);
    // //System.out.println(t32-t31);
    // }}

     

     

    参考文章:https://www.cnblogs.com/XuYiHe/p/6871799.html

    参考文章:https://blog.csdn.net/o_nianchenzi_o/article/details/78022416

     

     

    楼主这么辛苦,请使用楼主的推荐码领取支付宝红包吧,记得一定要消费掉哦。双赢^_^。

    1、打开支付宝首页搜索“8282987” 立即领红包。

     


  • 推荐阅读
    • javascript  – 概述在Firefox上无法正常工作
      我试图提出一些自定义大纲,以达到一些Web可访问性建议.但我不能用Firefox制作.这就是它在Chrome上的外观:而那个图标实际上是一个锚点.在Firefox上,它只概述了整个 ... [详细]
    • Java容器中的compareto方法排序原理解析
      本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
    • 标题: ... [详细]
    • Spring源码解密之默认标签的解析方式分析
      本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
    • Nginx使用(server参数配置)
      本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
    • 开发笔记:加密&json&StringIO模块&BytesIO模块
      篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
    • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
    • 原文地址:https:www.cnblogs.combaoyipSpringBoot_YML.html1.在springboot中,有两种配置文件,一种 ... [详细]
    • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
    • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
    • importjava.util.ArrayList;publicclassPageIndex{privateintpageSize;每页要显示的行privateintpageNum ... [详细]
    • 本文介绍了Python爬虫技术基础篇面向对象高级编程(中)中的多重继承概念。通过继承,子类可以扩展父类的功能。文章以动物类层次的设计为例,讨论了按照不同分类方式设计类层次的复杂性和多重继承的优势。最后给出了哺乳动物和鸟类的设计示例,以及能跑、能飞、宠物类和非宠物类的增加对类数量的影响。 ... [详细]
    • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
    • 2018深入java目标计划及学习内容
      本文介绍了作者在2018年的深入java目标计划,包括学习计划和工作中要用到的内容。作者计划学习的内容包括kafka、zookeeper、hbase、hdoop、spark、elasticsearch、solr、spring cloud、mysql、mybatis等。其中,作者对jvm的学习有一定了解,并计划通读《jvm》一书。此外,作者还提到了《HotSpot实战》和《高性能MySQL》等书籍。 ... [详细]
    • SpringMVC接收请求参数的方式总结
      本文总结了在SpringMVC开发中处理控制器参数的各种方式,包括处理使用@RequestParam注解的参数、MultipartFile类型参数和Simple类型参数的RequestParamMethodArgumentResolver,处理@RequestBody注解的参数的RequestResponseBodyMethodProcessor,以及PathVariableMapMethodArgumentResol等子类。 ... [详细]
    author-avatar
    faithKOBE
    这个家伙很懒,什么也没留下!
    PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
    Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有