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
- BeanUtils<T1, T2> beanToBean
- srm 526 T1 && T2
- srm527 T1 && T2
- sm 528 T1 && T2
- SRM 608 T1 && T2
- 【队内胡策 11.1】 T1 T2
- 【队内胡策 11.3】 T1 T2
- SRM 605 T1 T2 T3
- 磁共振中的T1, T2 和 T2*的原理和区别
- Func<T1, T2, TResult> 泛型委托
- 磁共振T1 T2 T1WI T2WI含义
- Dictionary<T1,T2>和Hashtable的异同
- 【JLOI2013】T1、T2、T3 Bzoj3190~3192
- SIP协议中的T1和T2
- NOIP 2013 day 2 t1 t2
- 51nod 1097 拼成最小的数 思维题 t1+t2<t2+t1
- FS-V1、FS-M1、FS-M2、FS-T1、FS-T2
- DateTime.Compare(t1,t2)比较两个日期大小
- Java的学习——培训或自学
- phpQuery—基于jQuery的PHP实现
- 动态二维数组的传参与删除
- 【ny-oj】-116-士兵杀敌(二)(树状数组,线段树,基础)
- zzuli 1921 (二进制序列)
- BeanUtils<T1, T2> beanToBean
- Minify对css,js的操作
- 方向导数与梯度
- 0-7 文本编辑器的使用
- SAP.G/L Account
- C语言指针(一)
- linux下安装chrome后没看到快捷方式,也不知安哪儿了?
- ACdreamer 1020 The Game about Kill
- VS2010下C# WebService使用方法