The following example inserts a user object into the MongoDB and reads it
The code is as follows |
Copy Code |
user.java: Package Com.mongo; public class User { string name; int age; oid _id; public String G Etname () { return name; } public void SetName (String name) { THIS.name = name; } public int getage () { return age; .} public void setage (int age) { ; this.age = age; } /** * MongoDB automatically generates Objectid * @author FHP * */ public Clas s oid{ string $oid; public String get$oid () { return $oid; & NBSP;&NBSP} public void set$oid (String $oid) { this. $oid = $oid; & NBSP;&NBSP} } } |
Main.java:
The code is as follows |
Copy Code |
Package Com.mongo;
Import java.net.UnknownHostException;
Import Com.google.gson.Gson; Import Com.mongodb.DB; Import com.mongodb.DBCollection; Import Com.mongodb.DBCursor; Import Com.mongodb.DBObject; Import com.mongodb.MongoClient; Import Com.mongodb.util.JSON;
public class Main {
/** * @param args * @throws unknownhostexception */ public static void Main (string[] args) throws Unknownhostexception { TODO auto-generated Method Stub Mongoclient mongoclient = new Mongoclient ();
DB Psdoc = Mongoclient.getdb ("Psdoc"); Dbcollection user=psdoc.getcollection ("user");
User U1=new user (); U1.setage (20); U1.setname ("SSSs"); Gson gson=new Gson (); Convert to JSON string, then convert to DBObject object DBObject dbobject = (dbobject) json.parse (Gson.tojson (U1)); Insert Database User.insert (DBObject);
Dbcursor Cursor=user.find (); while (Cursor.hasnext ()) { DBObject Obj=cursor.next (); Reverse User U=gson.fromjson (obj.tostring (), user.class); System.out.println (u.name); } }
} |
In order to facilitate, you can directly integrate the Gson into the drive, directly modify the driving source code
Com.mongodb.DBCollection.java:
New methods:
The code is as follows |
Copy Code |
/** * Write common objects to the database * * @param obj * @return */ Public writeresult Insert (Object obj) { Gson Gson = new Gson (); DBObject dbobject = (dbobject) json.parse (Gson.tojson (obj)); Gson=null; return insert (DBObject); } Com.mongodb.DBCursor.java:
|
New methods
The code is as follows |
Copy Code |
/** * Read the object from the database * @param obj * @param clazz * @return */ Public <T> T nextobj (class<t> clazz) { _checktype (Cursortype.iterator); Gson gson=new Gson (); T U=gson.fromjson (_next (). toString (), clazz); Gson=null; return u; }
|
Modified method of invocation:
The code is as follows |
Copy Code |
Main.java: Package Com.mongo;
Import java.net.UnknownHostException;
Import Com.mongodb.DB; Import com.mongodb.DBCollection; Import Com.mongodb.DBCursor; Import com.mongodb.MongoClient;
public class Main {
/** * @param args * @throws unknownhostexception */ public static void Main (string[] args) throws Unknownhostexception { TODO auto-generated Method Stub Mongoclient mongoclient = new Mongoclient ();
DB Psdoc = Mongoclient.getdb ("Psdoc"); Dbcollection user=psdoc.getcollection ("user");
User U1=new user (); U1.setage (20); U1.setname ("SSSs"); Insert Object directly User.insert (U1); Dbcursor Cursor=user.find (); while (Cursor.hasnext ()) { Pass in the class parameter, back to the class object User u=cursor.nextobj (User.class); System.out.println (u.name); } }
} |