这段时间尝试了一下MongoDB,感觉十分易用,方便,相比关系型的数据库来说优势也很大,于是尝试了下使用java连接MongoDB,并进行了
基本的增删改查操作。
首先先在控制台中连接数据库,查看数据库有几个表。
现在,新建一个maven工程,pom.xml中的依赖如下:
<!-- WICKET DEPENDENCIES --> <dependency> <groupId>org.apache.wicket</groupId> <artifactId>wicket</artifactId> <version>${wicket.version}</version> </dependency> <!-- OPTIONAL <dependency> <groupId>org.apache.wicket</groupId> <artifactId>wicket-extensions</artifactId> <version>${wicket.version}</version> </dependency> --> <!-- LOGGING DEPENDENCIES - LOG4J --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.4.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> <!-- JUNIT DEPENDENCY FOR TESTING --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--mongoDB for test--> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.0.2</version> </dependency> <!-- JETTY DEPENDENCIES FOR TESTING --> <dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty</artifactId> <version>${jetty.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-util</artifactId> <version>${jetty.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-management</artifactId> <version>${jetty.version}</version> <scope>provided</scope> </dependency> </dependencies>
现在,在源文件夹中新建MongoDb类
编写以下九个方法,以下几个方法,就是主要的连接,增删改查操作:
package com.haizhi.mongoDB.connect; import com.mongodb.*; import java.util.List; import java.util.Map; import java.util.Set; /** * Created by xiaxuan on 15/7/17. */ public class MongoDb { //1、连接到数据库 public MongoClient getMongo(){ //1、连接到MongoDB数据库 MongoClient mongo=new MongoClient("127.0.0.1",27017); return mongo; } //2、获取指定数据库 public DB getDb(String DbName){ MongoClient mongo=getMongo(); return mongo.getDB(DbName); } //3、显示数据库中所有数据库 public void showDb(){ MongoClient mongo=getMongo(); //显示所有数据库 List<String> dbs=mongo.getDatabaseNames(); for(String database:dbs){ System.out.println(database); } } //4、获取一个表 public DBCollection getTable(DB db,String tableName){ //如果表不存在,MongoDB将会创建一个 DBCollection table=db.getCollection("users"); return table; } //5、显示数据库中所有表 public void showTables(DB db){ //显示选择的数据库中所有表 Set<String> tables=db.getCollectionNames(); for(String coll:tables){ System.out.println(coll); } } //6、新增一条记录 public void insert(Map<String,Object> map,DBCollection table){ BasicDBObject document=new BasicDBObject(); for(Object key:map.keySet()){ document.put((String) key, map.get(key)); } table.insert(document); } //7、查找并显示一条记录 public void search(BasicDBObject basicDBObject,DBCollection table){ DBCursor dbCursor=table.find(basicDBObject); while(dbCursor.hasNext()){ System.out.println(dbCursor.next()); } System.out.println("End"); } //8、更新一条记录 public void update(BasicDBObject query,BasicDBObject newDocument,DBCollection table){ BasicDBObject updateObj=new BasicDBObject(); updateObj.put("$set",newDocument); table.update(query, updateObj); } //9、删除一条记录 public void delete(BasicDBObject basicDBObject,DBCollection table){ table.remove(basicDBObject); } }
现在,在test文件夹中,新建测试类:
testInsert:package com.haizhi.testMongoDB; import com.haizhi.mongoDB.connect.MongoDb; import com.mongodb.DBCollection; import com.mongodb.DB; import com.mongodb.MongoClient; import org.junit.Test; import java.util.HashMap; import java.util.Map; /** * Created by xiaxuan on 15/7/17. */ public class testInsert { @Test public void insert(){ MongoDb mongoDb=new MongoDb(); MongoClient mongo=mongoDb.getMongo(); DB dB=mongoDb.getDb("MongoTest"); DBCollection table=mongoDb.getTable(dB, "users"); Map<String,Object> map=new HashMap<String, Object>(); map.put("id",3); map.put("name","xiaxuan"); map.put("age",22); map.put("sex",true); map.put("nickname","bingwen"); mongoDb.insert(map,table); } }
执行完程序后,在数据库中搜索:
在数据库中查询到,这一条记录成功插入。
现在测试查询:
testSearch:
package com.haizhi.testMongoDB; import com.haizhi.mongoDB.connect.MongoDb; import com.mongodb.BasicDBObject; import com.mongodb.DBCollection; import com.mongodb.MongoClient; import com.mongodb.DB; import org.junit.Test; /** * Created by xiaxuan on 15/7/17. */ public class testSearch { @Test public void testSearch(){ MongoDb mongoDb=new MongoDb(); MongoClient mongo=mongoDb.getMongo(); DB dB=mongoDb.getDb("MongoTest"); DBCollection talbe=mongoDb.getTable(dB, "users"); BasicDBObject basicDBObject=new BasicDBObject(); basicDBObject.append("name","xiaxuan"); mongoDb.search(basicDBObject,talbe); } }
成功查询到两条数据。
修改的测试源程序和删除的基本相同,这里就只贴出源码,不再演示具体效果。
testUpdate:
package com.haizhi.testMongoDB; import com.haizhi.mongoDB.connect.MongoDb; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.sun.xml.internal.rngom.parse.host.Base; import org.junit.Test; /** * Created by xiaxuan on 15/7/17. */ public class testUpdate { @Test public void update(){ MongoDb mongoDb=new MongoDb(); DB dB=mongoDb.getDb("MongoTest"); DBCollection table=mongoDb.getTable(dB, "users"); BasicDBObject query=new BasicDBObject(); query.put("name","xiaxuan"); BasicDBObject newDocument=new BasicDBObject(); newDocument.put("name","bingwen"); mongoDb.update(query,newDocument,table); } }
testDelete:
package com.haizhi.testMongoDB; import com.haizhi.mongoDB.connect.MongoDb; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import org.junit.Test; /** * Created by xiaxuan on 15/7/17. */ public class testDelete { @Test public void delete(){ MongoDb mongoDb=new MongoDb(); DB dB=mongoDb.getDb("MongoTest"); DBCollection table=mongoDb.getTable(dB, "users"); BasicDBObject basicDBObject=new BasicDBObject(); basicDBObject.put("name","xiaxuan"); mongoDb.delete(basicDBObject,table); } }
连接Mongodb,以及基本的增删改查操作,就是这些,连接MongoDb,操作,基本操作都还是比较简单。
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-01 04:50:59