自定义对象数据替换其字段值
来源:互联网 发布:淘宝哪个店铺能套现 编辑:程序博客网 时间:2024/04/28 07:07
import java.beans.BeanInfo;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;/** * 数据操作工具类 * @author xuyang * @time 2016/11/10 15:58 */public class DataHelper { /** * 替换对象的字段数据(对象类型必须相同) * @param sourceObject 被替换的源数据对象 replaceObject 替换的数据对象 * @return Object 替换后的数据对象 * @author xuyang * @time 2016/11/10 15:58 */ public static <T> T updateObject(T sourceObject,T replaceObject){ Class<?> sourceClazz = sourceObject.getClass(); Field[] sourceFields = sourceClazz.getDeclaredFields(); Class<?> replaceClazz = sourceObject.getClass(); Field[] replaceFields = replaceClazz.getDeclaredFields(); try { for (int i = 0;i < replaceFields.length;i++){ Field replaceField = replaceFields[i]; replaceField.setAccessible(true); //字段子更新赋值判断 if (null != replaceField.get(replaceObject)) { Field sourceField = sourceFields[i]; sourceField.setAccessible(true); sourceField.set(sourceObject,replaceField.get(replaceObject)); } } }catch (Exception e){ e.printStackTrace(); } return sourceObject; } /** * 替换对象的字段数据(对象类型可以不同,只替换同类型同字段名的字段值) * @param sourceObject 被替换的源数据对象 replaceObject 替换的数据对象 * @return Object 替换后的数据对象 * @author xuyang * @time 2016/11/10 15:58 */ public static <T> T updateDifObject(T sourceObject,Object replaceObject){ Class<?> sourceClazz = sourceObject.getClass(); Field[] sourceFields = sourceClazz.getDeclaredFields(); Class<?> replaceClazz = replaceObject.getClass(); Field[] replaceFields = replaceClazz.getDeclaredFields(); try { for (int i = 0;i < replaceFields.length;i++){ Field replaceField = replaceFields[i]; replaceField.setAccessible(true); //字段子更新赋值判断 if (null != replaceField.get(replaceObject)) { for (int j = 0; j < sourceFields.length; j++) { Field sourceField = sourceFields[j]; if(sourceField.getName().equals(replaceField.getName()) && sourceField.getType().getName().equals(replaceField.getType().getName())){ sourceField.setAccessible(true); sourceField.set(sourceObject,replaceField.get(replaceObject)); break; } } } } }catch (Exception e){ e.printStackTrace(); } return sourceObject; } /** * 判断对象某些值是否为空 * @param object * @param <T> * @return * @author gaoliubei */ public static <T> String verify(T object,String[] needVerify) { Class<?> sourceClazz = object.getClass(); Field [] fields = sourceClazz.getDeclaredFields(); StringBuilder result = new StringBuilder(); boolean isLegal = true; for (int i = 0;i < fields.length;i++) { Field field = fields[i]; field.setAccessible(true); try { Object value = field.get(object); if (value == null) { String filedName = field.getName(); boolean isd = false; for (int j = 0;j < needVerify.length; j++) { String isResult = needVerify[j]; if (isResult == filedName) { isLegal = false; isd = true; break; } } if (isd) { result.append(filedName+" : 不能为空 "); } } } catch (IllegalAccessException e) { e.printStackTrace(); } } if (!isLegal) { return result.toString(); } return null; } /** * map转对象 * @param map * @param beanClass * @return * @throws Exception */ public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) { try { 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; }catch (Exception e){ e.printStackTrace(); return null; } } /** * 对象转map * @param obj * @return */ public static Map<?, ?> objectToMap(Object obj) { try { if(obj == null) return null; Map<String, Object> map = new HashMap<String, Object>(); 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; }catch (Exception e){ e.printStackTrace(); return null; } }}
0 0
- 自定义对象数据替换其字段值
- 替换NText字段数据
- 替换字段值
- 替换字段值
- mysql 替换字段值
- sql语句批量替换字段数据
- mysql替换语句之字段值替换
- 【sql】字段空值替换
- Oracle中字段值替换
- 关于Oracle Sales Cloud 中自定义对象和字段数据的导入
- FormData表单字段数据对象
- 提取某个字段数据并统计其分布规律
- [Liferay] 使用 Expando 自定义字段搜索对象
- liferay中自定义字段得到Group对象
- 根据字段值查询其所在的表、字段
- 如何替换数据库中某个字段中的数据?
- mysql替换字段里数据内容部分字符串
- [Oracle]varchar2替换为clob(该字段中已有大量数据)
- (1)开发环境搭建-持久层
- Android自定义View实现标签流效果
- Spring原理之通过反射+配置文件生成对象池
- 自定义SwipeRefreshLayout 实现上拉加载功能
- 使用GetExtendedTcpTable获取TCP相关信息
- 自定义对象数据替换其字段值
- python绘图 初识Python绘图
- VC中BSTR、Char和CString类型的转换(太牛了)
- http2 协议
- 常用的文档URL
- 正则-判断数字类型(小数和整数)
- Android Studio获取sha1
- Codeforces Round #397 Tree Folding
- 大话数据结构读书笔记(四)-栈和队列