在Struts--hibernate开发框架中,实现formbean与hibernate pojo的自动转换

在我们实际项目开发过程中,经常有大量的对单表的基本维护。在做这一部分工作时,最耗费我们宝贵时间的就是formbean与hibernate pojo之间的转换问题了。经过一天的努力,初见成效。贴出来,让大家拍拍砖。

源代码:


  • import java.lang.reflect.Field;
    import java.lang.reflect.Method;
  • /**
     * <p>Title: </p>
     *
     * <p>Description: </p>
     *
     * <p>Copyright: Copyright (c) 2006</p>
     *
     * <p>Company: </p>
     *
     * @author 赵志宽
     * @version 1.0
     */
    public class DataForm {
  •     /**
         * VOToForm
         *
         * @param vo Object
         * @param form Object
         * @throws ClassNotFoundException
         */
        public static void VOToForm(Object vo, Object form) throws
                ClassNotFoundException {
  •         Field[] voFileds;
            Field[] formFields;
            Class beanVo = vo.getClass();
            voFileds = beanVo.getFields();
            Class formData = form.getClass();
            formFields = formData.getFields();
            try {
                DataTrants(voFileds, formFields, vo, form, "0");
            } catch (Exception ex) {
                System.out.println("转换失败" + ex.getMessage());
            }
        }
  •     /**
         * FormToVO
         *
         * @param vo Object
         * @param form Object
         */
        public static void FormToVO(Object vo, Object form) {
            Field[] voFileds;
            Field[] formFields;
            Class beanVo = vo.getClass();
            voFileds = beanVo.getFields();
            Class formData = form.getClass();
            formFields = formData.getFields();
  •         try {
                DataTrants(voFileds, formFields, vo, form, "1");
  •         } catch (Exception ex) {
                System.out.println("转换失败" + ex.getMessage());
            }
  •     }
  •     /**
         * DataTrants
         * flag = "0" 从VO到FORM flag = "1" 从form到VO
         * @param voFileds Field[]
         * @param formFields Field[]
         */
        public static void DataTrants(Field[] voFileds, Field[] formFields,
                                      Object vo,
                                      Object form, String flag) throws
                Exception {
            String voFieldName = "";
            String formFieldName = "";
            Method m = null;
            Method v = null;
            String value = "";
            Class param[] = new Class[1];
            Class param1[] = new Class[0];
            Object[] paramValue = new Object[1];
            if (flag.equals("0")) {
                for (int i = 0; i < voFileds.length; i++) {
                    voFieldName = voFileds[i].getName();
                    for (int j = 0; j < formFields.length; j++) {
                        formFieldName = formFields[j].getName();
                        if (voFieldName.equals(formFieldName.toLowerCase())) {
                            v = vo.getClass().getMethod(getMethodName("get",
                                    voFieldName),
                                    param1);
                            value = String.valueOf(v.invoke(vo, param1));
                            param[0] = formFields[j].getType();
                            m = form.getClass().getMethod("set" +
                                    voFieldName.toUpperCase(), param);
                            paramValue[0] = value;
                            if (String.valueOf(param[0].getName()).equals(
                                    "java.lang.Integer")) {
                                paramValue[0] = new Integer(value);
                            }
                            m.invoke(form, paramValue);
                        }
                    }
                }
            } else {
                if (flag.equals("1")) {
                    for (int i = 0; i < formFields.length; i++) {
                        formFieldName = formFields[i].getName();
                        v = form.getClass().getMethod("get" + formFieldName, param1);
                        value = String.valueOf(v.invoke(form, param1));
                        param[0] = vo.getClass().getField(formFieldName.toLowerCase()).
                                   getType();
                        m = vo.getClass().getMethod(getMethodName("set",
                                formFieldName), param);
                        paramValue[0] = value;
                        if (String.valueOf(param[0].getName()).equals(
                                "java.lang.Integer")) {
                            paramValue[0] = new Integer(value);
                        }
                        if (String.valueOf(param[0].getName()).equals(
                                "java.util.Date")) {
                            int year = new Integer(value.substring(0, 2)).intValue();
                            int month = new Integer(value.substring(4, 6)).intValue();
                            int days = new Integer(value.substring(8, 10)).intValue();
                            paramValue[0] = new java.util.Date(year, month, days);
                        }
                        m.invoke(vo, paramValue);
                    }
                } else {
                    throw new Exception("数据转换标志不对");
                }
  •         }
        }
  •     /**
         * getMethodName
         *
         * @param flag String
         * @param columName String
         * @return String
         */
        private static String getMethodName(String flag, String columName) {
            String pre = columName.substring(0, 1).toUpperCase();
            String next = columName.substring(1, columName.length()).toLowerCase();
            StringBuffer methodName = new StringBuffer();
            methodName.append(flag);
            methodName.append(pre);
            methodName.append(next);
            return new String(methodName);
  •     }
  •  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值