BeanUtils<T1, T2> beanToBean

来源:互联网 发布:想自己制作软件 编辑:程序博客网 时间:2024/04/30 08:26
/** * entity 转化成表现成数据* @ClassName: BeanUtils * @Description: TODO(这里用一句话描述这个类的作用) * @author EX-HUYAOYUAN700 * @date 2016年4月13日 上午10:23:50 */public class BeanUtils<T1, T2> {    private static final Logger logger = LoggerFactory            .getLogger(BeanUtils.class);    @SuppressWarnings({ "unchecked", "rawtypes" })    public T1 entityToForm(T1 beanForm, Object entity) throws ServiceException {        logger.info("BeanUtils,entityToForm");        Method[] method = beanForm.getClass().getDeclaredMethods();        T1 beanFormCls = null;        try {            beanFormCls = (T1) beanForm.getClass().newInstance();            Class entityCls = entity.getClass();            beanFormCls = this.setVaLue(beanFormCls, method, entityCls, entity);        } catch (Exception e) {            logger.error("BeanUtils,entityToForm", e);            throw new ServiceException("单个bean转换错误!!!");        }        return beanFormCls;    }    /**     * @throws ServiceException      * @throws Exception      * listTntityToListForm    * @Title: listTntityToListForm     * @Description: TODO(这里用一句话描述这个方法的作用)     * @param beanForm    * @param entity    * @return    * @throws     */    @SuppressWarnings({ "unchecked", "rawtypes" })    public List<T1> listEntityToListForm(T1 beanForm, List listEntity)            throws ServiceException {        logger.info("BeanUtils,listEntityToListForm");        List<T1> list1 = new ArrayList<>();        Method[] method = beanForm.getClass().getDeclaredMethods();        Class entityCls = null;        if (CollectionUtils.isNotEmpty(listEntity)) {            entityCls = listEntity.get(0).getClass();        }        // 遍歷method        if (null != method && method.length > 0) {            for (Object entity : listEntity) {                // 获取from实例                T1 beanFormCls = null;                try {                    beanFormCls = (T1) beanForm.getClass().newInstance();                    beanFormCls = this.setVaLue(beanFormCls, method, entityCls,                            entity);                } catch (Exception e) {                    logger.error("BeanUtils,listEntityToListForm", e);                    throw new ServiceException("listEntityToListForm转换错误!!!");                }                list1.add(beanFormCls);            }        }        return list1;    }    /**     *  entity to form     * @Title: setVaLue     * @Description: TODO(这里用一句话描述这个方法的作用)     * @param beanFormCls    * @param method    * @param entityCls    * @param entity    * @return    * @throws Exception    * @throws     */    @SuppressWarnings({ "unchecked", "rawtypes" })    private T1 setVaLue(T1 beanFormCls, Method[] method, Class entityCls,            Object entity) throws Exception {        logger.info("BeanUtils,setVaLue");        // 遍歷method        if (null != method && method.length > 0) {            for (int i = 0; i < method.length; i++) {                // 判斷該方法上是否有註解                if (method[i].isAnnotationPresent(BeanCopyAnnotation.class)) {                    BeanCopyAnnotation myAnnotation = method[i]                            .getAnnotation(BeanCopyAnnotation.class);                    // 拿到注解中的属性                    String methodName = myAnnotation.methodName();                    String returnType = myAnnotation.returnType();                    String dateType = myAnnotation.dateType();                    // 根據註解獲取方法名稱                    Method methodEntity = null;                    if (StringUtils.isEmpty(methodName)) {                        String[] aa = method[i].toString().split("\\(");                        String str = "";                        if (null != aa) {                            str = "g" + aa[0]                                    .split("\\.")[aa[0].split("\\.").length - 1]                                            .substring(1);                        }                        // 注解没有加入方法说明                        methodEntity = entityCls.getDeclaredMethod(str);                    } else {                        methodEntity = entityCls.getDeclaredMethod(methodName);                    }                    // 根據放回值類型設置值                    if ("BigDecimal".equals(returnType)) {                        // 默認金额是數據庫存儲的時間是Integer                        BigDecimal decimal = new BigDecimal("0.00");                        if (!StringUtils.isEmpty(                                methodEntity.invoke(entity, new Object[] {}))) {                            method[i].invoke(beanFormCls,                                    new Object[] { decimal                                            .add(new BigDecimal(methodEntity                                                    .invoke(entity,                                                            new Object[] {})                                                    .toString())                                                            .divide(new BigDecimal(                                                                    "100")))                                            .toString() });                        } else {                            method[i].invoke(beanFormCls, new Object[] { "" });                        }                    } else {                        if (StringUtils.isEmpty(dateType)) {                            // 就表示是string,Integer                            if (!StringUtils.isEmpty(methodEntity.invoke(entity,                                    new Object[] {}))) {                                method[i]                                        .invoke(beanFormCls,                                                new Object[] { methodEntity                                                        .invoke(entity,                                                                new Object[] {})                                                        .toString() });                            } else {                                method[i].invoke(beanFormCls,                                        new Object[] { "" });                            }                        } else {                            // 默認時間是數據庫存儲的時間是Integer                            if (!StringUtils.isEmpty(methodEntity.invoke(entity,                                    new Object[] {}))) {                                method[i].invoke(beanFormCls, new Object[] {                                        DateUtil.getDateFrIntByDateType(                                                (Integer) methodEntity.invoke(                                                        entity, new Object[] {}),                                                dateType) });                            } else {                                method[i].invoke(beanFormCls,                                        new Object[] { "" });                            }                        }                    }                }            }        }        return beanFormCls;    }}

0 0