您的位置:首页 > 编程语言 > Java开发

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

2015-09-05 04:14 633 查看
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相比多出来界面需要的属性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息