将entity内String类型返回值为null更改为""
来源:互联网 发布:mac照片导入iphone 编辑:程序博客网 时间:2024/05/23 15:48
package com.pbh.util;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.List;import org.springframework.transaction.annotation.Transactional;/** * * @ClassName: EntityFieldUtil * @Description: TODO(泛型bean操作类) * @author PanBaihui * @date 2017年8月19日 下午11:29:18 * */public class EntityFieldUtil { /** * * @Title: replaceNullToString * @Description: 将entity内String类型返回值为null更改为"" * @author PanBaihui * @time 2017年8月31日 下午2:14:58 * @param entity * @return * @throws */ @Transactional(readOnly=true) public static <T> T replaceNullToString(T entity) { Class entityClass = (Class) entity.getClass(); Method[] methods = entityClass.getMethods();//获取包括父类所有public方法 for (int i = 0; i < methods.length; i++) { Method method = methods[i]; int index = method.getReturnType().toString().indexOf("String"); if (index > -1) {//判断返回类型是String String methodName = method.getName();//方法名 if (methodName.startsWith("get")) {//判断get方法 try { Object value = method.invoke(entity);//得到get方法的值 if (value == null) {//判断值为空 String setMethodName = methodName.replace("get", "set");//set方法名 Method setMethod = entityClass.getMethod(setMethodName,String.class);//set方法 setMethod.invoke(entity,"");//设置String类型值为"" } } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } } } } return entity; } /** * * @Title: replaceNullToString * @Description: 将entitys内String类型返回值为null更改为"" * @author PanBaihui * @time 2017年8月31日 下午3:10:13 * @param entitys * @return List<T> * @throws */ public static <T> List<T> replaceNullToString(List<T> entitys) { List<T> newList = new ArrayList<>(); for (T t : entitys) { newList.add(replaceNullToString(t)); } return newList; } /** * * @Title: getFieldName * @Description: TODO(根据get或set方法返回属性名) * @param methodName * @param replace * @return String 返回类型 */ public static String getFieldName(String methodName, String replace) { methodName = methodName.replace(replace, ""); return methodName.substring(0, 1).toLowerCase() + methodName.substring(1); }}
阅读全文
0 0
- 将entity内String类型返回值为null更改为""
- @RequestMapping方法返回值为String类型
- 将String类型转化为Date类型
- 将内置类型转换为string类型
- 将String类型转换为Int类型
- 函数有返回值且返回值为引用类型的问题返回null
- mybatis查询,返回类型为resultType,某些属性值为null
- findViewById返回为NULL
- “无法将类型"string"转换为"char"”?
- 将int转换为string类型
- 如何将string转换为Date类型
- 将整型数据转换为string类型
- ”属性不能设置为“Int64”值。必须将该属性设置为类型为“Int32”的非 null 值。
- SQLiteDatabase的openOrCreateDatabase方法的返回类型为null
- FormsAuthenication.Encrypt(FormsAuthenticationTicke)返回值为null
- SurfaceView中lockCanvas返回值为null
- mybatis查询,返回类型为HashMap,字段为null时没返回
- 将null值转化为其他值
- Qt 之 QSqlRecord Class
- SynchronizedMap和ConcurrentHashMap的深入分析
- LoadRunner视频教程
- opencv: 阈值处理(cv2.threshold) 探究(图示+源码)
- Nt32 build run之后总是找不到网卡
- 将entity内String类型返回值为null更改为""
- junit 方法:assertEquals 和 assertTrue
- 矩阵的幂的和
- 索引、游标学习过程中遇到的一些摘抄
- WriteHuge不是CFile的成员
- c语言控制台字体、背景、大小、拷贝复制。
- ajax基础入门
- Java Service Wrapper 将Java程序部署成系统服务
- 独木舟(贪心)