Configuration please see my other article, followed by, MongoDB performance test results, a "fast" word
To the source package please leave the mailbox
Code Construction diagram
Directly on the code
Basedao.java
Package Com.yiyuwanglu.basecore.dao;import Java.util.list;public Interface Basedao {<t> T FindByID (Class<T > Entityclass, String ID);<t> list<t> findAll (class<t> entityclass); void remove (Object obj); void Add (object obj); void Saveorupdate (Object obj);}
Mongodbbasedao.java
Package Com.yiyuwanglu.basecore.dao.mongodb;import Java.util.collection;import Java.util.list;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.beans.factory.annotation.qualifier;import Org.springframework.data.mongodb.core.mongotemplate;import Org.springframework.data.mongodb.core.query.Query; Import Org.springframework.stereotype.repository;import Com.yiyuwanglu.basecore.dao.basedao;import com.yiyuwanglu.basecore.page.page;/** * mongodb data Generic DAO class * * @author HJN * @version 1.0 2014-09-16 */@Repository (value = " Mongodbbasedao ") public class Mongodbbasedao implements Basedao {@Autowired @qualifier (" mongotemplate ") protected Mongotemplate mongotemplate;/** * Returns an object based on the primary key ID * * @param ID * uniquely identifies * @return T object */public <T> t FindByID (class<t> Entityclass, String ID) {return this.mongoTemplate.findById (ID, entityclass);} /** * Get full list of objects based on class * * @param entityclass * return type * @return list<t> return to Object list */public <T> list< T> findAll (class<t> entityclass) {return this.mongoTemplate.findAll (entityclass);} /** * Delete an object * * @param obj * Mongo object to be deleted */public void Remove (object obj) {this.mongoTemplate.remove (obj);} /** * Add Object * * @param obj * MONGO object to be added */public void Add (Object obj) {this.mongoTemplate.insert (obj);} /** * Modify Object * * @param obj * MONGO object to be modified */public void Saveorupdate (Object obj) {this.mongoTemplate.save (obj);} /** * Query and pagination * * @param entityclass * Object Type * @param query * Query criteria * @param page * pagination * @re Turn */public <T> list<t> findbyquery (class<t> entityclass, query Query, page page) {Long count = this.c Ount (entityclass, query);p age.setcount (count); int pagenumber = Page.getcurrent (); int pageSize = Page.getpagecount (); Query.skip ((pageNumber-1) * pageSize). Limit (pageSize); return this.mongoTemplate.find (query, entityclass);} /** * * @param entityclass * Query Object * @param query * search conditions * @rEturn */public <T> Long count (class<t> entityclass, query query) {return This.mongoTemplate.count (query, Entityclass);} /** * BULK INSERT * @param Entityclass object class * @param collection collection of objects to be inserted */public <T> void addcollection (class<t> ent Ityclass, collection<t> Collection) {This.mongoTemplate.insert (Collection, entityclass);} Public Mongotemplate Getmongotemplate () {return mongotemplate;}}
Page.java
Package Com.yiyuwanglu.basecore.page;import Java.io.ioexception;import Java.io.serializable;import Javax.servlet.jsp.jspexception;import Javax.servlet.jsp.jspwriter;import Javax.servlet.jsp.tagext.simpletagsupport;public class Page extends Simpletagsupport implements Serializable {private Static final Long serialversionuid = 1l;//serial number private Integer current;//current page number private long count;//record total private I Nteger pagecount;//per page record number private string path;//page link private string param;//passed in Parameter private Boolean notquerycount = false;//false to query the total number of records when paging, default false public page () {this.current = 1;//default first page This.count = 0L;//Total number of records This.pagecount = 10; Default per page 10 records} @Override public void Dotag () throws Jspexception, IOException {int pageSize = (int.) (This . Count/this.pagecount + (this.count% This.pagecount > 0? 1:0));//Total number of pages//display the current page and total pages jspwriter out = t His.getjspcontext (). Getout ();//Specify Input stream for page output paging information StringBuffer SB= new StringBuffer ();//Build StringBuffer object, user splicing page label sb.append ("<div class=\" page\ ">"); Sb.append ("<ul>"); If the current page is on the first page, then the home and previous page does not have a hyperlink if (this.current = = 1) {sb.append ("<li class=\" disabled\ "> Home </li>& Lt;li class=\ "disabled\" > Prev </li> "); } else {sb.append ("<li><a href=\"); Sb.append (This.path); Sb.append ("current="); Sb.append (1); if (This.param! = NULL &&! "". Equals (This.param)) {sb.append ("&"); Sb.append (This.param); } sb.append ("\" > Home </a></li> "); Sb.append ("<li><a href=\"); Sb.append (This.path); Sb.append ("current="); Sb.append (this.current-1); if (This.param! = NULL &&! "". Equals (This.param)) {sb.append ("&"); Sb.append (This.param); } sB.append ("\" > Previous page </a></li> "); }//The following code shows the page number, the current page in the middle position if (PageSize <=) {for (int i = 1; I <= pageSize; i++) { If the page is less than or equal to 10 pages, all displays if (i = = this.current) {//If the page number equals the current page, there is no hyperlink Sb.append ("< Li class=\ "current\" > "); Sb.append (i); Sb.append ("</li>"); } else {//otherwise give hyperlink Sb.append ("<li><a href=\"); Sb.append (This.path); Sb.append ("current="); Sb.append (i); if (This.param! = NULL &&! "". Equals (This.param)) {sb.append ("&"); Sb.append (This.param); } sb.append ("\" > "); Sb.append (i); Sb.append ("</a></li>"); }}} or else {//if greater than 10 pages, only 10 pages are displayed from the current page center int index = 1; if (This.current > 4) {//And if the current page is greater than 4 pages, show 10 pages starting from the current page 4 pages if (this.current + 4 >= pageSize) {//If the current page +4 &G t;= Total pages, the last 10 pages all show for (int j = pageSize-9; J <= PageSize; j + +) {if (j = = This.current) {//If the page number equals the current page, there is no hyperlink sb.append ("<li class=\" current\ ">"); Sb.append (j); Sb.append ("</li>"); } else {//otherwise a given hyperlink Sb.append ("<li><a href=\"); Sb.append (This.path); Sb.append ("current="); Sb.append (j); if (This.param! = NULL &&! "". Equals (This.param)) {sb.append ("&"); Sb.append (This.param); } sb.append ("\" > "); Sb.append (j); Sb.append ("</a></li>"); }}} else {for (int j = this.current-4; J <= PageSize; j + +) { if (j = = This.current) {//If the page number equals the current page, there is no hyperlink sb.append ("<li class=\" Current\ ">"); Sb.append (j); Sb.append ("</li>"); } else {//otherwise a given hyperlink Sb.append ("<li><a href=\"); Sb.append (This.path); Sb.append ("current="); Sb.append (j); if (This.param! = NULL &&! "". Equals (This.param)) {sb.append ("&"); Sb.append (This.param); } sb.append ("\" > "); Sb.append (j); Sb.append ("</a></li>"); } index++; if (Index > 10) {//If the loop is 10 times, exit the loop break; }}}} else {for (int i = 1; I <= pageSize; i++) { If the number of pages is less than or equal to 10 pages, all displays if (i = = this.current) {//If page numbers are equal to the current page, the page has no hyperlinks s B.append ("<li class=\" current\ ">"); Sb.append (i); Sb.append ("</li>"); } else {//otherwise give hyperlink Sb.append ("<li><a href=\"); Sb.append (This.path); Sb.append ("current="); Sb.append (i); if (This.param! = NULL &&! "". Equals (This.param)) {sb.append ("&"); Sb.append (This.param); } sb.append ("\" > "); Sb.append (i); Sb.append ("</a></li>"); } index++; if (Index >) {break; }}}}//If the current page is the last page, then the last and next pages have no hyperlinks if (this.current.equals (pageSize) | | | this . Count = = 0) {sb.append ("<li class=\" disabled\ "> Next </li><li class=\" disabled\ "> last page </li> "); } else {sb.append ("<li><a href=\"); Sb.append (This.path); Sb.append ("current="); Sb.append (this.current + 1); if (This.param! = NULL &&! "". Equals (This.param)) {sb.append ("&"); Sb.append (This.param); } sb.append ("\" > Next page </a></li> "); Sb. Append ("<li><a href=\"); Sb.append (This.path); Sb.append ("current="); Sb.append (pageSize); if (This.param! = NULL &&! "". Equals (This.param)) {sb.append ("&"); Sb.append (This.param); } sb.append ("\" > End </a></li> "); } sb.append ("</ul>"); Sb.append ("</div>"); Out.print (SB); }/** * Gets the total number of record bars * * @return */public Long GetCount () {return this.count; }/** * Set total number of records * * @param count */public void SetCount (Long count) {this.count = count; }/** * Gets the current page * * @return */public Integer GetCurrent () {return this.current; }/** * Sets the current page * * @param present */public void setcurrent (Integer) {try { if (current <= 0) {this.current = 1; } else { This.current = current; }} catch (Exception e) {this.current = 1; }}/** * Gets the number of records per page * * @return */public Integer Getpagecount () {return this.pagecount; }/** * Set how many records per page * * @param pagecount */public void Setpagecount (Integer pagecount) {th Is.pagecount = PageCount; }/** * Gets the URI address * * @return */public String GetPath () {return path; }/** * Set URI address * * @param path */public void SetPath (String path) {this.path = path; }/** * Gets the parameter value * * @return */public String GetParam () {return param; }/** * Set parameter value * * @param param */public void SetParam (String param) {string[] x = PARAM.SPL It ("&"); StringBuilder sb = new StringBuilder (); for (int i = 0; i < x.length; i++) {string[] y = x[i].split ("="); if (Y.LEngth > 1 &&! "". Equals (Y[1].trim ())) {sb.append (x[i]); Sb.append ("&"); }} This.param = Sb.tostring (). substring (0, sb.tostring (). LastIndexOf ("&")); }/** * Gets the total number of pages * * @return */public int getpages () {if (this.count% This.pagecount = = 0) { return (int) (this.count/this.pagecount); } else {return (int) (This.count/this.pagecount + 1); }}/** * is the first page * * @return */public boolean firstenable () {return previoenable (); }/** * is the last page * * @return */public boolean lastenable () {return nextenable (); }/** * has the next page * * @return */public boolean nextenable () {return this.current * This.pageco Unt < This.count; }/** * Whether there is a previous page * * @return */public boolean previoenable () {return this.current > 1; } public Boolean Isnotquerycount () {return this.notquerycount; The public void Setnotquerycount (Boolean notquerycount) {this.notquerycount = Notquerycount; }/** * Gets the position of the first data in the dataset at any one page. * * @param pageno page number from 1 * @param pageSize number of records per page * @return the page First Data */public int getstartofpage (long PageNo, long PageSize) {return (int) ((pageNo-1) * pageSize); }}
User.java
Package Com.yiyuwanglu.test.entity;import Java.util.date;import org.springframework.data.mongodb.core.mapping.Document; @Documentpublic class User {private String Id;private string username;private int age;private Date Createtime;public user () {}public User (String username, int age, date createtime) {s Uper (); this.username = Username;this.age = Age;this.createtime = Createtime;} Public String GetId () {return ID;} public void SetId (String id) {this.id = ID;} Public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public Date Getcreatetime () {return createtime;} public void Setcreatetime (Date createtime) {this.createtime = Createtime;}}
Basetest.java
Package Basetest;import Org.junit.runner.runwith;import org.springframework.test.context.ContextConfiguration; Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith (Springjunit4classrunner.class) @ Contextconfiguration (locations={"Classpath:config/spring/applicationcontext.xml"}) public class BaseTest { }
Testbasedao.java
Package Basetest.dao;import Java.util.arraylist;import Java.util.date;import java.util.list;import Javax.annotation.resource;import Org.junit.test;import Org.springframework.data.mongodb.core.query.Criteria; Import Org.springframework.data.mongodb.core.query.query;import basetest. Basetest;import Com.yiyuwanglu.basecore.dao.mongodb.mongodbbasedao;import Com.yiyuwanglu.test.entity.user;public Class Testbasedao extends Basetest {@Resource (name = "Mongodbbasedao") Mongodbbasedao mongodbbasedao;/** * Insert single data, ID custom * /public void Testadd () {User user = new User ();D ate creattime = new Date () user.setcreatetime (creattime); User.setage (10); User.setusername ("a"); This.mongoDBBaseDao.add (user); /** * Insert 1 million data, ID custom */@Testpublic void testaddcollection () {list<user> userlist = new arraylist<user> (); for (int j = 0; J <; J + +) {for (int i = 0; i < 100000; i++) {User user = new User ();D ate creattime = new Date (); u Ser.setcreatetime (Creattime); User.setage () user.setusername ("register South"); usErlist.add (user);}} This.mongoDBBaseDao.add (userlist);} @Testpublic void Testfindall () {list<user> userlist = This.mongoDBBaseDao.findAll (User.class); for (User user: userlist) {System.out.println ("ID:" + user.getid () + "username:" + user.getusername () + "Age:" + user.getage ());} System.out.println ("Get all the Data----------------------");} @Testpublic void Testfindbyid () {User user = This.mongoDBBaseDao.findById (user.class, "1234567"); System.out.println (User.getusername ()); System.out.println ("Get a single object----------------------");} @Testpublic void Testupdate () {User user = This.mongoDBBaseDao.findById (user.class, "1234567"); User.setusername ("It is the Huai "); this.mongoDBBaseDao.saveOrUpdate (user); User NewUser = this.mongoDBBaseDao.findById (User.class, "1234567"); System.out.println (Newuser.getusername ()); SYSTEM.OUT.PRINTLN ("Modify data Success"); This.mongoDBBaseDao.saveOrUpdate (user);} @Testpublic void Testremove () {User user = This.mongoDBBaseDao.findById (user.class, "1234567"); This.mongoDBBaseDao.remove (user); User oLduser = This.mongoDBBaseDao.findById (User.class, "1234567"), if (Olduser = = null) {System.out.println (Olduser = = null); System.out.println ("Delete object succeeded");} This.mongoDBBaseDao.add (user);} @Testpublic void Testcount () {Query query = new query (); criteria = new criteria (), Criteria.and ("username"). Is ("Fu Tung"); Query.addcriteria (criteria); Long total = This.mongoDBBaseDao.count (user.class, query); SYSTEM.OUT.PRINTLN ("Total number of users:" + totals);}}
Springmvc+mongodb+maven Project Test Code