将entity内String类型返回值为null更改为""

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);
    }



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值