MONGODB (iv)--dbobject and JavaBean conversion

Source: Internet
Author: User
Tags object object

First, DBObject to JavaBean
   /*** Convert entity Bean object to DBObject **/     Public Static<T>dbobject beantodbobject (T Bean)throwsIllegalArgumentException, illegalaccessexception {if(Bean = =NULL)            return NULL; DBObject DBObject=NewBasicdbobject (); //get the domain of the object classfield[] Fields =Bean.getclass (). Getdeclaredfields ();  for(Field field:fields) {//gets the property name of the variableString VarName =Field.getname (); //Modify access control permissions            BooleanAccessflag =field.isaccessible (); if(!Accessflag) {field.setaccessible (true); } Object param=Field.get (Bean); if(param = =NULL) {                Continue; } Else if(paraminstanceofInteger) {                //determine the type of the variable                intValue =((Integer) param). Intvalue ();            Dbobject.put (varName, value); } Else if(paraminstanceofString) {String value=(String) param;            Dbobject.put (varName, value); } Else if(paraminstanceofDouble) {                DoubleValue =((Double) param). Doublevalue ();            Dbobject.put (varName, value); } Else if(paraminstanceofFloat) {                floatValue =((Float) param). Floatvalue ();            Dbobject.put (varName, value); } Else if(paraminstanceofLong) {                LongValue =((Long) param). Longvalue ();            Dbobject.put (varName, value); } Else if(paraminstanceofBoolean) {                BooleanValue =((Boolean) param). Booleanvalue ();            Dbobject.put (varName, value); } Else if(paraminstanceofDate) {Date value=(Date) param;            Dbobject.put (varName, value); }            //Restore access control permissionsfield.setaccessible (Accessflag); }        returnDBObject; }

Second, JavaBean converted to DBObject
      /*** Convert DBObject to Bean Object **/     Public Static<T>T Dbobjecttobean (dbobject dbobject, T Bean)throwsillegalaccessexception, InvocationTargetException, nosuchmethodexception {if(Bean = =NULL) {            return NULL; } field[] fields=Bean.getclass (). Getdeclaredfields ();  for(Field field:fields) {String varName=Field.getname (); Object Object=Dbobject.get (varName); if(Object! =NULL) {Beanutils.setproperty (bean, VarName, object); }        }        returnBean; }    //Remove the attribute value from the MONGO and assign a value to the bean     Public Static<T>voidsetProperty (t Bean, String varName, T object) {VarName= varname.substring (0, 1). toUpperCase () + varname.substring (1); Try{String type=Object.getclass (). GetName (); //type is string            if(Type.equals ("java.lang.String") ) {Method m= Bean.getclass (). GetMethod ("get" +varName); String value=(String) M.invoke (bean); if(Value = =NULL) {m=Bean.getclass (). GetMethod ("Set" + VarName, String.class);                M.invoke (Bean, object); }            }            //type is integer            if(Type.equals ("Java.lang.Integer") ) {Method m= Bean.getclass (). GetMethod ("get" +varName); String value=(String) M.invoke (bean); if(Value = =NULL) {m= Bean.getclass (). GetMethod ("set" +varName, Integer.class);                M.invoke (Bean, object); }            }            //type is Boolean            if(Type.equals ("Java.lang.Boolean") ) {Method m= Bean.getclass (). GetMethod ("get" +varName); String value=(String) M.invoke (bean); if(Value = =NULL) {m= Bean.getclass (). GetMethod ("set" +varName, Boolean.class);                M.invoke (Bean, object); }            }        } Catch(nosuchmethodexception e) {e.printstacktrace (); } Catch(SecurityException e) {e.printstacktrace (); } Catch(illegalaccessexception e) {e.printstacktrace (); } Catch(IllegalArgumentException e) {e.printstacktrace (); } Catch(InvocationTargetException e) {e.printstacktrace (); }    }

MONGODB (iv)--dbobject and JavaBean conversion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.