- 有些业务需求就需要非关系型数据库来实现,特别是用到了内嵌文档,暑假的时候就已经写了但是不知道被自己误删还是别人删掉了一大段代码,心疼自己,特此来记录一下。
- 连接数据库
-
1 //连接到mangodb服务 2 static MongoClient mongoClient = new MongoClient("127.0.0.1",27017); //ip和端口号 3 public static DB getConn() { 4 try{ 5 //连接数据库 6 DB mongoDatabase = mongoClient.getDB("xxxxx");//数据库名 7 System.out.println("Connet to database successfully"); 8 return mongoDatabase; 9 }catch(Exception e){ 10 System.err.println( e.getClass().getName() + ": " + e.getMessage()); 11 return null; 12 } 13 } 14 //关闭连接 15 public static void close() { 16 if (mongoClient != null) { 17 mongoClient.close(); 18 mongoClient = null; 19 } 20 }
- 增加一条记录(包括内嵌文档)
1 public static void CreateGridByUuid(int uuid) { 2 DB conn=MongoDB.getConn(); 3 DBCollection collection = conn.getCollection("test"); 4 try { 5 BasicDBObject[] addrAttrs = new BasicDBObject[10]; 6 for(int i=0;i<10;i++){ 7 BasicDBObject addrAttr = new BasicDBObject(); 8 addrAttr.put("index",i+1); 9 addrAttr.put("goods_id", 0); 10 addrAttr.put("count", 0); 11 addrAttrs[i] = addrAttr; 12 } 13 14 BasicDBObject doc1 = new BasicDBObject (); 15 doc1.put("uuid", uuid); 16 doc1.put("grid",addrAttrs); 17 collection.insert(doc1);//插入记录 18 19 System.out.println("Document inserted successfully"); 20 } catch (Exception e) { 21 e.printStackTrace(); 22 }
23 }
- 在内嵌集合中添加一条记录
1 //单个格子创建 2 public static void AddGridByUuid(int uuid,int count) { 3 DB conn=MongoDB.getConn(); 4 DBCollection collection = conn.getCollection("test"); 5 try { 6 BasicDBObject query = new BasicDBObject(); 7 BasicDBObject field = new BasicDBObject(); 8 query.put("uuid",uuid); 9 field.put("grid", 1); 10 DBCursor cursor = collection.find(query,field); 11 while(cursor.hasNext()){ 12 BasicDBObject result = (BasicDBObject) cursor.next(); 13 ArrayList<BasicDBObject> versi=(ArrayList<BasicDBObject>)result.get("grid"); 14 BasicDBObject addgrid=new BasicDBObject(); 15 addgrid.put("index", count+1); 16 addgrid.put("goods_id", 0); 17 addgrid.put("count", 0); 18 versi.add(addgrid); 19 20 BasicDBObject doc = new BasicDBObject(); 21 BasicDBObject res = new BasicDBObject(); 22 res.put("grid", versi); 23 doc.put("$set", res); 24 collection.update(query,doc,false,true); 25 } 26 }catch (Exception e) { 27 e.printStackTrace(); 28 } 29 }
- 查
public static ArrayList<Grid> SelectGridByUuid(int uuid) { DB conn=MongoDB.getConn(); DBCollection collection = conn.getCollection("test"); ArrayList<Grid> gridlist = new ArrayList<Grid>(); try { BasicDBObject query = new BasicDBObject(); BasicDBObject field = new BasicDBObject(); query.put("uuid",uuid);//查询条件 field.put("grid", 1); DBCursor cursor = collection.find(query,field); while(cursor.hasNext()){ BasicDBObject result = (BasicDBObject) cursor.next(); ArrayList<BasicDBObject> versi=(ArrayList<BasicDBObject>)result.get("grid"); result.entrySet(); for(BasicDBObject embedded:versi){ Grid grid = new Grid(); grid.setIndex(embedded.getInt("index")); grid.setGoods_id(embedded.getInt("goods_id")); grid.setCount(embedded.getInt("count")); gridlist.add(grid); } } }catch (Exception e) { e.printStackTrace(); } return gridlist; }
- 修改内嵌文档中的某字段
public static void UpdateGridAndCountByIndexAndUuid(int index, int goodsId, int count, int uuid) { DB conn=MongoDB.getConn(); DBCollection collection = conn.getCollection("test"); try { BasicDBObject query = new BasicDBObject(); query.put("uuid", uuid); query.put("grid.index", index); BasicDBObject doc = new BasicDBObject(); BasicDBObject res = new BasicDBObject(); int pos=index-1; res.put("grid."+pos+".goods_id", goodsId); res.put("grid."+pos+".count", count); doc.put("$set", res); collection.update(query,doc,false,true); System.out.println("更新数据完成!"); logger.debug("已将物品"+goodsId+"放入到第"+index+"格中"); } catch (Exception e) { e.printStackTrace(); } }
- Grid类
1 public class Grid { 2 private int uuid; 3 private int index; 4 private int goods_id; 5 private int count; 6 public int getUuid() { 7 return uuid; 8 } 9 public void setUuid(int uuid) { 10 this.uuid = uuid; 11 } 12 public int getIndex() { 13 return index; 14 } 15 public void setIndex(int index) { 16 this.index = index; 17 } 18 public int getGoods_id() { 19 return goods_id; 20 } 21 public void setGoods_id(int goods_id) { 22 this.goods_id = goods_id; 23 } 24 public int getCount() { 25 return count; 26 } 27 public void setCount(int count) { 28 this.count = count; 29 } 30 31 }