NoSql之MongoDB实现数据库数据操作

前端之家收集整理的这篇文章主要介绍了NoSql之MongoDB实现数据库数据操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
1、创建唯一数据库实例DB
  1. package com.boonya.mongo;
  2. import java.net.UnknownHostException;
  3. import java.util.Arrays;
  4. import com.mongodb.DB;
  5. import com.mongodb.MongoClient;
  6. import com.mongodb.ServerAddress;
  7.  
  8.  
  9. public class DBConnection {
  10. private DBConnection(){}
  11. private static String userName="boonya";
  12. private static char [] password="boonya".tocharArray();
  13. private static DB db=null;
  14. /**
  15. * mongoClient = new MongoClient();
  16. * // or
  17. * mongoClient = new MongoClient( "localhost" );
  18. * // or
  19. * mongoClient = new MongoClient( "localhost",27017 );
  20. * // or,to connect to a replica set,supply a seed list of members
  21. * @return
  22. */
  23. public synchronized static DB getInstance(){
  24. if(db==null){
  25. MongoClient mongoClient=null;
  26. try {
  27. mongoClient = new MongoClient(Arrays.asList(new ServerAddress("localhost",27017),new ServerAddress("localhost",27018),27019)));
  28. } catch (UnknownHostException e) {
  29. e.printStackTrace();
  30. }
  31. db = mongoClient.getDB("mydb");
  32. boolean auth = db.authenticate(userName,password);
  33. if(!auth){ //the authentication is invalid
  34. return null;
  35. }
  36. }
  37. return db;
  38. }
  39.  
  40. }
2、创建CRUD操作工具类
  1. package com.boonya.mongo.optimize;
  2. import java.net.UnknownHostException;
  3. import com.boonya.mongo.utils.ConstantUtils;
  4. import com.mongodb.DB;
  5. import com.mongodb.DBCollection;
  6. import com.mongodb.DBCursor;
  7. import com.mongodb.DBObject;
  8. import com.mongodb.Mongo;
  9. import com.mongodb.MongoException;
  10. import com.mongodb.MongoOptions;
  11.  
  12.  
  13. public class MongDbCRUD {
  14. private static Mongo mongo = null;
  15. private static DB db;
  16. private static DBCollection table;
  17. private static MongDbCRUD instace;
  18. public static synchronized MongDbCRUD getInstance(){
  19. if(instace==null||mongo==null||db==null||table==null){
  20. instace = new MongDbCRUD();
  21. }
  22. return instace;
  23. }
  24. public MongDbCRUD(){
  25. String host = ConstantUtils.getValue("mongo.host").toString();
  26. int port = Integer.parseInt(ConstantUtils.getValue("mongo.port").toString());
  27. String datablease = ConstantUtils.getValue("mongo.datablease").toString();
  28. String tablelename = ConstantUtils.getValue("mongo.table.bound").toString();
  29. try {
  30. mongo = new Mongo(host,port);
  31. // 连接池
  32. MongoOptions opt = mongo.getMongoOptions();
  33. opt.connectionsPerHost = 10 ; //poolsize
  34. opt.threadsAllowedToBlockForConnectionMultiplier = 10 ;
  35. //获取temp DB;如果默认没有创建,mongodb会自动创建
  36. db = mongo.getDB(datablease);
  37. //获取users DBCollection;如果默认没有创建,mongodb会自动创建
  38. table = db.getCollection(tablelename);
  39. //DO SOMETHING
  40. } catch (UnknownHostException e) {
  41. e.printStackTrace();
  42. } catch (MongoException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. public MongDbCRUD(String host,int port,String datablease,String tablelename){
  47. try {
  48. mongo = new Mongo(host,port);
  49. // 连接池
  50. MongoOptions opt = mongo.getMongoOptions();
  51. opt.connectionsPerHost = 10 ; //poolsize
  52. opt.threadsAllowedToBlockForConnectionMultiplier = 10 ;
  53. //获取temp DB;如果默认没有创建,mongodb会自动创建
  54. db = mongo.getDB(datablease);
  55. //获取users DBCollection;如果默认没有创建,mongodb会自动创建
  56. table = db.getCollection(tablelename);
  57. //DO SOMETHING
  58. } catch (UnknownHostException e) {
  59. e.printStackTrace();
  60. } catch (MongoException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. public static void free(){
  65. if(mongo!=null){
  66. mongo.close();
  67. }
  68. mongo = null;
  69. db = null;
  70. table = null;
  71. System.gc();
  72. }
  73. public static DBCollection getDBCollection(){
  74. return table;
  75. }
  76. public void add(DBObject row){
  77. table.save(row);
  78. }
  79. public void query(){
  80. DBCursor cur = table.find();
  81. while (cur.hasNext()) {
  82. DBObject dbobject = cur.next();
  83. System.out.println(dbobject.get("key"));//get value
  84. }
  85. }
  86. }

3、mongo.properties内容

  1. #mongodb setting
  2. mongo.host=192.168.20.38
  3. mongo.port=27017
  4. mongo.database=map
  5. mongo.table.bound=bound
  6. mongo.username=username
  7. mongo.pwd=pwd

猜你在找的NoSQL相关文章