map填充bean赋值,包括父类全部填充。
有不少工具类给bean填充值。但是填充,很多都是只能填充到当前类的对象。经过需求修改,做了个工具类:
import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.iafclub.baseTools.vo.PageResultVo; import com.iafclub.baseTools.vo.PageVo; /**对象填充类 * * @author 陈惟鲜 * @date 2016年11月10日 下午12:51:04 * */ public class MyCollectionUtil { /**map转换为对象 * * @param map map信息 * @param beanClass 对象类 * @return * @throws Exception * @author 陈惟鲜 * @date 2016年4月11日 上午11:05:40 */ public static Object mapToObject(Map<String, Object> 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; } field.setAccessible(true); field.set(obj, map.get(field.getName())); } return obj; } /**map转换为对象,包括对象父类全部赋值 * * @param map map信息 * @param beanClass 对象类 * @return * @throws Exception * @author 陈惟鲜 * @date 2016年4月11日 上午11:05:40 */ public static Object mapToObjectAll(Map<String, Object> map, Class<?> beanClass) throws Exception { if (map == null){ return null; } Object obj = beanClass.newInstance(); // 获取所有父类的信息定义 for(Class<?> clazz = obj.getClass(); clazz != Object.class ; clazz = clazz.getSuperclass()) { Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { int mod = field.getModifiers(); if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){ continue; } field.setAccessible(true); field.set(obj, map.get(field.getName())); } } return obj; } /**对象转换为map * * @param obj * @return * @throws Exception * @author 陈惟鲜 * @date 2016年4月11日 上午11:05:59 */ public static Map<String, Object> objectToMap(Object obj) throws Exception { if(obj == null){ return null; } Map<String, Object> map = new HashMap<String, Object>(); Field[] declaredFields = obj.getClass().getDeclaredFields(); for (Field field : declaredFields) { field.setAccessible(true); map.put(field.getName(), field.get(obj)); } return map; } /**对象转换为map,b * * @param obj * @return * @throws Exception * @author 陈惟鲜 * @date 2016年4月11日 上午11:05:59 */ public static Map<String, Object> objectToMapAll(Object obj) throws Exception { if(obj == null){ return null; } Map<String, Object> map = new HashMap<String, Object>(); for(Class<?> clazz = obj.getClass(); clazz != Object.class ; clazz = clazz.getSuperclass()) { Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); map.put(field.getName(), field.get(obj)); } } return map; } public static void main(String[] args) { PageResultVo pageResultVo = new PageResultVo(); PageVo pageVo = new PageVo(); pageVo.setCount(50); pageVo.setPageSize(500); List<PageVo> resultList = new ArrayList<PageVo>(); resultList.add(pageVo); pageResultVo.setPage(pageVo); pageResultVo.setResultList(resultList); try { Map map = objectToMap(pageResultVo); System.out.println(map); PageVo pageVo2 = (PageVo) mapToObject(map, PageVo.class); System.out.println(pageVo2.getPageSize()); } catch (Exception e) { e.printStackTrace(); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?