• MongoDB增删改查实例


    MongoDB之Java测试代码(DAO层),mongodbdao


    MongoInit.java是数据库初始化及连接类

    MongoUtils.java是对mongodb的各种操作方法

    MongoInit.java

    package com.wlwcloud.datatest;  import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.UnknownHostException; import java.util.Properties;  import org.apache.log4j.Logger;  import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.Mongo; import com.mongodb.MongoOptions; import com.mongodb.ServerAddress;  /**  * Mongo工具类:设计为单例模式,每当月份发生变化,数据库连接名称就会发生变化,这是业务规则 因MongoDB的Java驱动是线程安全的,对于一般的应用,只要一个Mongo实例即可,Mongo有个内置的连接池(池大小默认为10个)。  * 对于有大量写和读的环境中,为了确保在一个Session中使用同一个DB时,我们可以用以下方式保证一致性: DB mdb =mongo.getDB('dbname'); mdb.requestStart(); // 业务代码 mdb.requestDone();  * DB和DBCollection是绝对线程安全的  * @author undoner  */ public class MongoInit { 	/** DB监听端口号 */ 	private static int DBPort = 27017; 	/** DB连接URL */ 	private static String DBUrl = "localhost"; 	/** 连接DB库名称 */ 	private static String DBName = "wlwdb"; 	static Logger log = Logger.getRootLogger(); 	private static Mongo mongo; 	private static DBCollection coll; 	private static DB db;  	static { 		try { 			DBproperties(); 			MongoOptions options = new MongoOptions(); 			options.autoConnectRetry = true; 			options.connectionsPerHost = 1000; 			options.maxWaitTime = 5000; 			options.socketTimeout = 0; 			options.connectTimeout = 15000; 			options.threadsAllowedToBlockForConnectionMultiplier = 5000; 			// 事实上,Mongo实例代表了一个数据库连接池,即使在多线程的环境中,一个Mongo实例对我们来说已经足够了 			ServerAddress serverAddress = new ServerAddress(DBUrl, DBPort); 			mongo = new Mongo(serverAddress, options); 		} catch (UnknownHostException e) { 			log.info("get mongo instance failed"); 		} 	}  	static void DBproperties() { 		// 读取配置文件config.properties中的属性值 		String myFilePath = MongoInit.class.getResource("/").getPath() + "MongoDBConfig.properties"; 		Properties properties = new Properties(); 		FileInputStream fileInputStream = null; 		try { 			   fileInputStream = new FileInputStream(myFilePath); 			   properties.load(fileInputStream); 			   DBPort = Integer.parseInt((String) properties.getProperty("DBPort")); 			   DBUrl = (String) properties.getProperty("DBUrl"); 			   DBName = (String) properties.getProperty("DBName"); 			  } catch (Exception e) { 			   e.printStackTrace(); 			  } finally { 			   try { 			    fileInputStream.close(); 			   } catch (IOException e) { 			    // TODO Auto-generated catch block 			    e.printStackTrace(); 			   } 		}  	}  	public static DB getDB() { 		if (db == null) { 			db = mongo.getDB(DBName); 		} 		return db; 	}  	public static Mongo getMong() { 		return mongo; 	}  	public static DBCollection getColl(String collname) { 		return getDB().getCollection(collname); 	}  }

    MongoUtils.java

    package com.wlwcloud.datatest;  import java.net.UnknownHostException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Set; import java.util.regex.Pattern;  import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.ketayao.ketacustom.entity.main.User; import com.ketayao.utils.SecurityUtils; import com.mongodb.AggregationOutput; import com.mongodb.BasicDBList; import com.mongodb.BasicDBObject; import com.mongodb.BasicDBObjectBuilder; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.MapReduceCommand; import com.mongodb.MapReduceOutput; import com.mongodb.Mongo; import com.mongodb.MongoException; import com.mongodb.QueryBuilder; import com.mongodb.WriteConcern;  public class MongoUtils { 	//DBCursor cursor = coll.find(condition).addOption(Bytes.QUERYOPTION_NOTIMEOUT);//设置游标不要超时             /**       * 获取所有数据库实例       */       public void testGetDBS() {           List<String> dbnames = MongoInit.getMong().getDatabaseNames();           for (String dbname : dbnames) {               System.out.println("dbname:" + dbname);           }       }                /**       * 删除数据库       */       public void dropDatabase(String dbname) {           MongoInit.getMong().dropDatabase(dbname);       }                /**       * 查询所有表名       */       public void getAllCollections() {           Set<String> colls = MongoInit.getDB().getCollectionNames();           for (String s : colls) {               System.out.println("Collections:"+s);           }       }          /**       * 删除一个表       */       public void dropCollection(String collection) {           MongoInit.getColl(collection).drop();       }          /**       * 添加一条记录       */             public void addData(String dbCollection,String dataID,String dataTime,String dataContent,String dataStatus) {           DBCollection coll = MongoInit.getColl(dbCollection);           BasicDBObject doc = new BasicDBObject();           doc.put("id", dataID);           doc.put("time", dataTime);           doc.put("data", dataContent);           doc.put("status", dataStatus);           coll.insert(doc);           // 设定write concern,以便操作失败时得到提示           coll.setWriteConcern(WriteConcern.SAFE);           findOne(dbCollection);     }                /**       * 创建索引       */       public void createIndex(String collection) {           MongoInit.getColl(collection).createIndex(new BasicDBObject("index_id", 1));       }                /**       * 获取索引信息       */       public void getIndexInfo(String dbCollection) {           List<DBObject> list = MongoInit.getColl(dbCollection).getIndexInfo();           for (DBObject o : list) {               System.out.println(o);           }       }                /**       * 添加多条记录       */       public void addMultiData() {           for (int i = 0; i < 100; i++) {               MongoInit.getColl("wujintao").insert(                       new BasicDBObject().append("i", i));           }              List<DBObject> docs = new ArrayList<DBObject>();           for (int i = 0; i < 50; i++) {               docs.add(new BasicDBObject().append("i", i));           }           MongoInit.getColl("wujintao").insert(docs);           // 设定write concern,以便操作失败时得到提示           MongoInit.getColl("wujintao").setWriteConcern(WriteConcern.SAFE);       }                /**       * 查找第一条记录       */       public void findOne(String dbCollection) {           DBObject myDoc = MongoInit.getColl(dbCollection).findOne();           System.out.println(myDoc);       }                /**       * 获取表中所有记录条数       */       public void count(String dbCollection) {           System.out.println(MongoInit.getColl(dbCollection).getCount());           System.out.println(MongoInit.getColl(dbCollection).count());       }                /**       * 获取查询结果集的记录数       */       public void getCount(String dbCollection,String dataID) {           DBObject query = new BasicDBObject("id", dataID);           long count = MongoInit.getColl(dbCollection).count(query);           System.out.println(count);       }                /**       * 查询所有结果       */       public void getAllDocuments(String dbCollection) {           DBCursor cursor = MongoInit.getColl(dbCollection).find();           try {               while (cursor.hasNext()) {                   System.out.println(cursor.next());               }           } finally {               cursor.close();           }       }                /**       * 按照一个条件查询       */       public void queryByConditionOne() {           BasicDBObject query = new BasicDBObject();           query.put("name", "MongoDB");           DBCursor cursor = MongoInit.getColl("wujintao").find(query);              try {               while (cursor.hasNext()) {                   System.out.println(cursor.next());               }           } finally {               cursor.close();           }       }       public List<SensorData> queryById(String id) {      	User user = SecurityUtils.getLoginUser();         BasicDBObject query = new BasicDBObject();           System.out.print(id);         query.put("id", id);           String table="table_";         DBCursor cursor = MongoInit.getColl("table_"+user.getUsername()).find(query);           List<DBObject> list=cursor.toArray();         System.out.println(list.size());         System.out.println("dao.."+list.toString());         List<SensorData> sensordata=new ArrayList<SensorData>();         for(int i=1;i<list.size();i++)         {         	String sid=(String)list.get(i).get("id");         	String stime=(String)list.get(i).get("time");         	String sdata=(String)list.get(i).get("data");         	String status=(String)list.get(i).get("status");         	String sstatus;         	if(status.equals("0"))sstatus="正常";         	else if(status.equals("1")) sstatus="删除";         	else sstatus="未知状态";             SensorData a=new SensorData(sid,stime,sdata,sstatus);         //System.out.print(a.getData()+a.getId()+a.getStatus()+a.getTime());         sensordata.add(a);         }         return sensordata;        /* try {               while (cursor.hasNext()) {                   System.out.println(cursor.next());               }           } finally {               cursor.close();           } */      }             /**       * AND多条件查询,区间查询       */       public void queryMulti() {           BasicDBObject query = new BasicDBObject();           // 查询j不等于3,k大于10的结果集           query.put("j", new BasicDBObject("$ne", 3));           query.put("k", new BasicDBObject("$gt", 10));           DBCursor cursor = MongoInit.getColl("wujintao").find(query);           try {               while (cursor.hasNext()) {                   System.out.println(cursor.next());               }           } finally {               cursor.close();           }       }                /**       * 区间查询       * select * from table where i >50       */       public void queryMulti2() {           BasicDBObject query = new BasicDBObject();           query = new BasicDBObject();           query.put("i", new BasicDBObject("$gt", 50)); // e.g. find all where i >           DBCursor cursor = MongoInit.getColl("wujintao").find(query);           try {               while (cursor.hasNext()) {                   System.out.println(cursor.next());               }           } finally {               cursor.close();           }       }                /**       * 区间查询       * select * from table where 20 < i <= 30          //比较符             //"$gt": 大于             //"$gte":大于等于             //"$lt": 小于             //"$lte":小于等于             //"$in": 包含          */       public void queryMulti3() {           BasicDBObject query = new BasicDBObject();           query = new BasicDBObject();              query.put("i", new BasicDBObject("$gt", 20).append("$lte", 30));           DBCursor cursor = MongoInit.getColl("wujintao").find(query);           try {               while (cursor.hasNext()) {                   System.out.println(cursor.next());               }           } finally {               cursor.close();           }       }          /**       * 组合in和and select * from test_Table where (a=5 or b=6) and (c=5 or d = 6)       */       public void queryMulti4() {           BasicDBObject query11 = new BasicDBObject();           query11.put("a", 1);           BasicDBObject query12 = new BasicDBObject();           query12.put("b", 2);           List<BasicDBObject> orQueryList1 = new ArrayList<BasicDBObject>();           orQueryList1.add(query11);           orQueryList1.add(query12);           BasicDBObject orQuery1 = new BasicDBObject("$or", orQueryList1);              BasicDBObject query21 = new BasicDBObject();           query21.put("c", 5);           BasicDBObject query22 = new BasicDBObject();           query22.put("d", 6);           List<BasicDBObject> orQueryList2 = new ArrayList<BasicDBObject>();           orQueryList2.add(query21);           orQueryList2.add(query22);           BasicDBObject orQuery2 = new BasicDBObject("$or", orQueryList2);              List<BasicDBObject> orQueryCombinationList = new ArrayList<BasicDBObject>();           orQueryCombinationList.add(orQuery1);           orQueryCombinationList.add(orQuery2);              BasicDBObject finalQuery = new BasicDBObject("$and",                   orQueryCombinationList);           DBCursor cursor = MongoInit.getColl("wujintao").find(finalQuery);       }                /**       * IN查询       * if i need to query name in (a,b); just use { name : { $in : ['a', 'b'] } }       * select * from things where name='a' or name='b'       * @param coll       */       public void queryIn() {           BasicDBList values = new BasicDBList();           values.add("a");           values.add("b");           BasicDBObject in = new BasicDBObject("$in", values);           DBCursor cursor = MongoInit.getColl("wujintao").find(                   new BasicDBObject("name", in));           try {               while (cursor.hasNext()) {                   System.out.println(cursor.next());               }           } finally {               cursor.close();           }       }                /**       * 或查询       * select * from table where name  = '12' or title = 'p'       * @param coll       */       public void queryOr() {           QueryBuilder query = new QueryBuilder();           query.or(new BasicDBObject("name", 12), new BasicDBObject("title", "p"));           DBCursor cursor = MongoInit.getColl("wujintao").find(query.get()).addSpecial("$returnKey", "");           try {               while (cursor.hasNext()) {                   System.out.println(cursor.next());               }           } finally {               cursor.close();           }       }                    public void customQueryField() throws UnknownHostException{           Mongo mongo = new Mongo("localhost", 27017);           DB db = mongo.getDB("zhongsou_ad");           BasicDBObjectBuilder bulder = new BasicDBObjectBuilder();           bulder.add("times",1);           bulder.add("aid",1);           DBCursor cusor =  db.getCollection("ad_union_ad_c_1").find(new BasicDBObject(),bulder.get());           for (DBObject dbObject : cusor) {               System.out.println(dbObject);           }       }                    public void mapReduce() throws UnknownHostException{           Mongo mongo = new Mongo("localhost", 27017);           DB db = mongo.getDB("zhongsou_ad");           /***           *  book1 = {name : "Understanding JAVA", pages : 100}           *  book2 = {name : "Understanding JSON", pages : 200}           *  db.books.save(book1)           *  db.books.save(book2)           *  book = {name : "Understanding XML", pages : 300}           *  db.books.save(book)           *  book = {name : "Understanding Web Services", pages : 400}           *  db.books.save(book)           *  book = {name : "Understanding Axis2", pages : 150}           *  db.books.save(book)             *            var map = function() {              var category;              if ( this.pages >= 250 )                  category = 'Big Books';              else                  category = "Small Books";              emit(category, {name: this.name});          };          var reduce = function(key, values) {              var sum = 0;              values.forEach(function(doc) {                  sum += 1;              });              return {books: sum};          };                 var count  = db.books.mapReduce(map, reduce, {out: "book_results"});           */           try {                  DBCollection books = db.getCollection("books");                  BasicDBObject book = new BasicDBObject();               book.put("name", "Understanding JAVA");               book.put("pages", 100);               books.insert(book);                              book = new BasicDBObject();                 book.put("name", "Understanding JSON");               book.put("pages", 200);               books.insert(book);                              book = new BasicDBObject();               book.put("name", "Understanding XML");               book.put("pages", 300);               books.insert(book);                              book = new BasicDBObject();               book.put("name", "Understanding Web Services");               book.put("pages", 400);               books.insert(book);                            book = new BasicDBObject();               book.put("name", "Understanding Axis2");               book.put("pages", 150);               books.insert(book);                              String map = "function() { "+                          "var category; " +                           "if ( this.pages >= 250 ) "+                           "category = 'Big Books'; " +                         "else " +                         "category = 'Small Books'; "+                           "emit(category, {name: this.name});}";                              String reduce = "function(key, values) { " +                                        "var sum = 0; " +                                        "values.forEach(function(doc) { " +                                        "sum += 1; "+                                        "}); " +                                        "return {books: sum};} ";                              MapReduceCommand cmd = new MapReduceCommand(books, map, reduce,                 null, MapReduceCommand.OutputType.INLINE, null);                  MapReduceOutput out = books.mapReduce(cmd);                  for (DBObject o : out.results()) {                System.out.println(o.toString());               }              } catch (Exception e) {                e.printStackTrace();              }       }                    public void GroupByManyField() throws UnknownHostException{           //此方法没有运行成功           Mongo mongo = new Mongo("localhost", 27017);           DB db = mongo.getDB("libary");           DBCollection books = db.getCollection("books");           BasicDBObject groupKeys = new BasicDBObject();           groupKeys.put("total", new BasicDBObject("$sum","pages"));                      BasicDBObject condition = new BasicDBObject();           condition.append("pages", new BasicDBObject().put("$gt", 0));                            String reduce = "function(key, values) { " +                   "var sum = 0; " +                   "values.forEach(function(doc) { " +                   "sum += 1; "+                   "}); " +                   "return {books: sum};} ";           /**           BasicDBList basicDBList = (BasicDBList)db.getCollection("mongodb中集合编码或者编码")                     .group(DBObject key,   --分组字段,即group by的字段                  DBObject cond,        --查询中where条件                  DBObject initial,     --初始化各字段的值                  String reduce,        --每个分组都需要执行的Function                  String finial         --终结Funciton对结果进行最终的处理           */           DBObject obj = books.group(groupKeys, condition,  new BasicDBObject(), reduce);           System.out.println(obj);                      AggregationOutput ouput = books.aggregate(new BasicDBObject("$group",groupKeys));           System.out.println(ouput.getCommandResult());           System.out.println(books.find(new BasicDBObject("$group",groupKeys)));       }                       /**       * 分页查询          */       public void pageQuery() {           DBCursor cursor = MongoInit.getColl("wujintao").find().skip(0)                   .limit(10);           while (cursor.hasNext()) {               System.out.println(cursor.next());           }       }          /**       * 模糊查询       */       public void likeQuery() {           Pattern john = Pattern.compile("joh?n");           BasicDBObject query = new BasicDBObject("name", john);              // finds all people with "name" matching /joh?n/i           DBCursor cursor = MongoInit.getColl("wujintao").find(query);       }                /**       * 条件删除          */       public void delete(String dbCollection,String dataID) {           BasicDBObject query = new BasicDBObject();           query.put("id", dataID);           // 找到并且删除,并返回删除的对象           DBObject removeObj = MongoInit.getColl(dbCollection).findAndRemove(query);           System.out.println(removeObj);       }                /**       * 更新       */       public void update() {           BasicDBObject query = new BasicDBObject();           query.put("name", "liu");           DBObject stuFound = MongoInit.getColl("wujintao").findOne(query);           stuFound.put("name", stuFound.get("name") + "update_1");           MongoInit.getColl("wujintao").update(query, stuFound);       }   	     public void testGpsData() {       	try { 			//13862082455:117.13172:029.77659:131.2580     		Date d = new Date();  			SimpleDateFormat  sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS"); 			String dateNowStr = sdf.format(d);   			System.out.println(dateNowStr); 			     		DBCollection coll = MongoInit.getColl("table_admin");               BasicDBObject doc = new BasicDBObject();               doc.put("id", "12345678");               doc.put("time", dateNowStr);               doc.put("data", "117.13172:029.77659:131.2580");               doc.put("status", "0");             coll.insert(doc);               // 设定write concern,以便操作失败时得到提示               coll.setWriteConcern(WriteConcern.SAFE);    			BasicDBObject dsort = new BasicDBObject(); 			dsort.put("time", -1); 			 			BasicDBObject searchDevice = new BasicDBObject(); 			searchDevice.put("id", "12345678"); 			DBCursor cursor = coll.find(searchDevice).sort(dsort);		 			 			List<DBObject> list = cursor.toArray(); 			//ArrayList<SensorData> listSensorData=new ArrayList<SensorData>(list.size()); 			 			ObjectMapper mapper = new ObjectMapper();   			String jsonfromList = null; 			try { 				//list转json 				jsonfromList = mapper.writeValueAsString(list); 			} catch (JsonProcessingException e) { 				// TODO Auto-generated catch block 				e.printStackTrace(); 			}   	        //System.out.println(jsonfromList);   //			for(int i=0;i<list.size();i++) //			{ //				System.out.println((String) list.get(i).get("time")); 			//} 			 			//System.out.println(list.size()+","+listSensorData);//list的长度 //		    System.out.println(cursor.count());//计算结果的数量,类似于(mysql count()函数),不受limit的影响 			 //			while (cursor.hasNext()) { //				System.out.println(cursor.next()); //			} 			System.out.println("The Search Query has Executed!"); 		} catch (MongoException e) { 			e.printStackTrace(); 		}     }            public static void main(String[] args) {     	String dbname="wlwdb";     	String dbCollection="table_admin";     	String dbCollection1="15257102317";     	String dataID="12345678"; 		MongoUtils tb=new MongoUtils(); 		tb.testGetDBS();//查询所有数据库 		tb.getAllCollections();//查询所有表 //		 //		tb.findOne(dbCollection);//查找第一条记录 		tb.testGpsData(); 		tb.getAllDocuments(dbCollection);//获取制定表明的所有数据 		//tb.dropDatabase(dbname);//删除指定数据库 		//tb.delete(dbCollection,dataID);//删除制定表明和设备ID的数据 		//tb.dropCollection(dbCollection);// 删除一个表 		 		//tb.createIndex(dbCollection1);//创建索引 		//tb.getIndexInfo(dbCollection1);//获得表中所有索引 	} } 

    MongoDBConfig.properties

    #端口设置 DBPort=27017 #URL DBUrl=localhost #URL DBName=wlwdb 



    [DAO层]java代码解释逐行解释

    你用的应该是ibatis,这里是批量删除,相当于把一堆操作用一个事务来提交,提高数据库性能。
    //函数功能,删除list中的所有元素在数据库中的记录。
    protected void batchDelete(List<?> lst, String statement)
    throws SQLException
    {
    getSqlMapClient().startBatch();//开始批量操作
    //遍历list
    for (Iterator localIterator = lst.iterator(); localIterator.hasNext(); ) 

    Object obj = localIterator.next();
    getSqlMapClient().delete(statement, obj);//statement表示你调用的删除方法,在xml文件中定义的。
    }
    getSqlMapClient().executeBatch();//结束批量操作
    }

  • 相关阅读:
    WPF:DataGrid 自动生成行号
    C#:WinForm之Command
    i.MX RT1010之FlexIO模拟SSI外设
    i.MX RT1010之FlexIO模拟I2S外设
    i.MX RT600之DSP开发环境调试篇
    i.MX RT600之DSP调试环境搭建篇
    i.MX RT600之DMIC外设介绍及应用
    i.MX RT600之I2S外设介绍及应用
    ssm框架思维-登录
    idea里面搭建SSM框架-crud
  • 原文地址:https://www.cnblogs.com/jym-sunshine/p/6344003.html
Copyright © 2020-2023  润新知