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