一个项目思路(1):用反射机制写的函数,使2个javabean相同属性一次性导入

public static void eval(Object bo,Object po)//2个对应的javabean对象内部相等的属性互相转化
    {
            
            //取得Method方法 //反射
            Method[] methods = bo.getClass().getMethods();
            int bolength=bo.getClass().getMethods().length;//bo 总的方法(包含get和set)个数
            int polength=po.getClass().getMethods().length;//po 总的方法(包含get和set)个数
            Method bogetMethod = null;//bo的get方法
            Method posetMethod = null;//po的set方法
            String posetMethodName =null;//po的set方法名称
            String fieldName=null;
            //反射, bo中取到get方法
            for(int i = 0; i <bolength; i++)
            {
                
                String methodName=methods[i].getName();//取得每个方法的名称
                if(methodName.substring(0, 3).equals("get"))//判断是否是get方法
                {
                    try
                    {
                        if(methodName.equals("getClass"))
                        {
                            continue;// getClass是对象自带的方法,不用管,直接跳过
                        }else
                        {
                            bogetMethod=methods[i];//获得bo的get方法
                            Object value=bogetMethod.invoke(bo,null);//反射执行bo的get方法,获得值
                            if(value!=null)
                            {
                                fieldName=methodName.substring(3);//属性名(第一个字母大写)
                                posetMethodName="set"+fieldName;//获得po的set方法名称
                                try
                                {    
    
                                    posetMethod=po.getClass().getMethod(posetMethodName,value.getClass());//获得po的set方法
                                    posetMethod.invoke(po,value);
                                }catch(Exception e)
                                {
        //                            e.printStackTrace();
                                    System.out.println(bo.getClass().getSimpleName()+"的get方法:"+bogetMethod.getName()+"在"+po.getClass().getSimpleName()+"中没有对应的set方法");
                                }
                                bogetMethod=null;
                                posetMethod=null;
                                posetMethodName=null;
                                fieldName=null;
                            }
                        }
                    }catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
                
            }

    }

相应测试函数:

 public void testEvalout()//po.setName(bo。getName) 其实也可以是 bo.setName(po.getName),从数据库取出来验证
     {
          CustomDAOImpl customDAO=new CustomDAOImpl();   
          CustomPO bo=customDAO.getCustomBySid("0006");
          CustomPO  po=new CustomPO();  
          eval.eval(po, bo);
          System.out.println("bo对象自带getclass方法:"+bo.getClass());
          System.out.println(bo.getC_no());
          System.out.println(bo.getC_comp());
          System.out.println(bo.getArea_no());
          System.out.println(bo.getEmpno());
          System.out.println(bo.getC_level());
          
          System.out.println(bo.getC_state());
          System.out.println(bo.getC_sat());
          System.out.println(bo.getC_cred());
          System.out.println(bo.getC_addr());
          System.out.println(bo.getC_post());
          
          System.out.println(bo.getC_phone());
          System.out.println(bo.getC_fax());
          System.out.println(bo.getC_url());
          System.out.println(bo.getC_regno());
          System.out.println(bo.getC_lawman());
          
          System.out.println(bo.getC_regfund());
          System.out.println(bo.getC_turnover());
          System.out.println(bo.getC_optank());
          System.out.println(bo.getC_bankno());
          System.out.println(bo.getC_loctax());
          System.out.println(bo.getC_nattax());
          System.out.println(bo.getC_losedate()+"类型:"+bo.getC_losedate().getClass());
            
     }

po是数据库取出来的对象,最好数据类型转一下,如sqlDate转为UtilDate,po和bo在转换时,相同属性数据类型也要相同。

bo是界面层的对象,可以有和po相比多出来界面需要的属性。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值