MongoDB Series 4: MongoDB and Java
Because of the different versions, the API may also be different. This tutorial uses version 3.0.
1. The jdbc driver version of mongodb used is: mongo-java-driver-3.0.0.jar
2. This section briefly introduces the JDBC operations, and does not consider efficiency issues for the moment.
3. The encapsulated tool code is as follows:
Public class MongoDBProxy {private static MongoDBProxy proxy = null; // Single Instance private static MongoDatabase db = null; // database connection object private static String [] paramArray = new String [5]; // database connection parameter private MongoDBProxy () {} static {paramArray [0] = "username"; paramArray [1] = "password"; paramArray [2] = "host "; paramArray [3] = "port"; paramArray [4] = "databaseName";}/*** get MongoDBProxy * using the default system configuration */public static MongoDBProxy getMongoDBP Roxy () {if (proxy = null) {proxy = new MongoDBProxy (); String sURI = String. format ("mongodb: // % s: % s @ % s: % d/% s", paramArray [0], paramArray [1], paramArray [2], Integer. parseInt (paramArray [3]), paramArray [4]); incluclienturi uri = new incluclienturi (sURI); incluclient client = new incluclient (uri); db = incluclient. getDatabase (paramArray [4]);} return proxy;}/*** batch query data * @ param table set name * @ param page paging parameter * @ Param filter condition * @ param sort sorting condition */public Page findDocList (String table, Page page, Bson filter, Bson sort) {your collection <Document> coll = db. getCollection (table); long count = coll. count (filter); // obtain the total amount of data based on the filter conditions Page p = PageUtil. createPage (page, Integer. parseInt (String. valueOf (count); p. setFromUrl (page. getFromUrl () = null )? "": Page. getFromUrl (); p. setParamString (page. getParamString () = null )? "": Page. getParamString (); FindIterable <Document> resultIterable = coll. find (); // execution condition filter resultIterable = resultIterable. sort (sort ). filter (filter ). skip (p. getBeginIndex ()). batchSize (p. getEveryPage (); extends cursor <Document> cousor = resultIterable. iterator (); List <Document> dataList = new ArrayList <Document> (); while (cousor. hasNext () {dataList. add (cousor. next ();} p. setDataList (dataList); return PageUtil. buildPageSt Ring (p);}/*** obtain a single Document * @ param table set name * @ param filter filtering condition * @ param sort sorting condition */public Document findOneDoc (String table, bson filter, Bson sort) {collect collection <Document> coll = db. getCollection (table); FindIterable <Document> resultIterable = coll. find (); if (sort! = Null) {resultIterable. sort (sort) ;}if (filter! = Null) {resultIterable. filter (filter);} return resultIterable. first ();}/*** Add Document * @ param table set name * @ prama doc Document content */public void addDocument (String table, Document doc) {collection <Document> coll = getCollection (table); coll. insertOne (doc);}/*** add documents in batches * @ param table set name * @ prama docList Document Set */public void addDocumentList (String table, List <Document> docList) {collection <Document> coll = getCollection (table); coll. insertist (docList);}/*** update document * @ param table set name * @ param query condition * @ param up update data */public UpdateResult updateDocument (String table, bson query, Bson up) {collect collection <Document> coll = getCollection (table); return coll. updateOne (query, up );} /*** replace document ** @ param table set name * @ param query condition * @ param up replace object */public UpdateResult replaceDocument (String table, Bson query, document up) {collect collection <Document> coll = getCollection (table); return coll. replaceOne (query, up);}/*** delete document * @ param table set name * @ param delete condition */public DeleteResult deleteDocument (String table, Bson delete) {collect collection <Document> coll = getCollection (table); return coll. deleteOne (delete);}/*** get collection object * @ param table collection name */private collections collection <Document> getCollection (String table) {return db. getCollection (table );}}
4. Call demo
MongoDBProxy proxy = MongoDBProxy. getMongoDBProxy (); System. out. println (proxy. findOneDoc ("users", null, null ). get ("_ id"); Document doc = new Document (); doc. put ("user", "Li Si"); proxy. addDocument ("users", doc); Bson bson = new BasicDBObject ("user", "Zhang San"); proxy. deleteDocument ("users", bson );
For further study, use the demo first. Haha.
Copyright statement: Reprinted with the blog address to respect the author's Labor achievements. Welcome to http://blog.csdn.net/zgs_shmilyto grow together.