操作系统:Windows 7
IDE开发工具:MyEclipse 8
数据库:MongoDB 2.4.4
JDK:1.7.0_25
mongo-2.10.1.jar
新建一个MongoDB工程,导入需要的jar包:
在CMD里面执行如下命令来启动MongoDB:D:\Download\software\mongodb-win32-i386-2.4.4\bin\mongod --dbpath=E:\Mongodb --logpath=E:\Mongodb\logs\mongodb.log �Crest
本文将以客户基本信息为例来演示如何在Java程序里面操作MongoDB。
假设客户信息(Customer)包含如下基本字段:名字(name)、年龄(age)、性别(sex)等。
Customer类代码如下:
package com; public class Customer { private String name; private int age; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "Customer [age=" + age + ", name=" + name + ", sex=" + sex + "]"; } } |
客户信息操作类CustomerService.java的代码如下所示:
package com; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.Mongo; public class CustomerService { static final String MONGODB_HOST = "127.0.0.1"; static final int MONGODB_PORT = 27017; static final String DB_NAME = "test"; static final String COLLECTION_NAME = "users"; /** * 获取MongoDB对象,以便进行增删改查操作 * * @return MongoDB对象 * @throws UnknownHostException */ public static Mongo getMongoDB() throws UnknownHostException { Mongo mongo = new Mongo(CustomerService.MONGODB_HOST, CustomerService.MONGODB_PORT); return mongo; } /** * 添加一条客户信息记录 * * @param customer * 客户信息 */ public static void addCustomer(Customer customer) { try { Mongo mongo = CustomerService.getMongoDB(); DB db = mongo.getDB(CustomerService.DB_NAME); DBCollection dbCollection = db .getCollection(CustomerService.COLLECTION_NAME); DBObject dbObject = new BasicDBObject(); dbObject.put("name", customer.getName()); dbObject.put("age", customer.getAge()); dbObject.put("sex", customer.getSex()); dbCollection.insert(dbObject); } catch (UnknownHostException e) { e.printStackTrace(); } } /** * 删除一条客户信息 * * @param customer * 客户信息 */ public static void removeCustomer(Customer customer) { try { Mongo mongo = CustomerService.getMongoDB(); DB db = mongo.getDB(CustomerService.DB_NAME); DBCollection dbCollection = db .getCollection(CustomerService.COLLECTION_NAME); DBObject dbObject = new BasicDBObject(); dbObject.put("name", customer.getName()); dbObject.put("age", customer.getAge()); dbObject.put("sex", customer.getSex()); dbCollection.remove(dbObject); } catch (UnknownHostException e) { e.printStackTrace(); } } /** * 修改客户信息 * * @param oldCustomer * 旧的客户信息 * @param newCustomer * 新的客户信息 */ public static void updateCustomer(Customer oldCustomer, Customer newCustomer) { try { Mongo mongo = CustomerService.getMongoDB(); DB db = mongo.getDB(CustomerService.DB_NAME); DBCollection dbCollection = db .getCollection(CustomerService.COLLECTION_NAME); DBObject oldDBObject = new BasicDBObject(); DBObject newDBObject = new BasicDBObject(); oldDBObject.put("name", oldCustomer.getName()); oldDBObject.put("age", oldCustomer.getAge()); oldDBObject.put("sex", oldCustomer.getSex()); newDBObject.put("name", newCustomer.getName()); newDBObject.put("age", newCustomer.getAge()); newDBObject.put("sex", newCustomer.getSex()); dbCollection.update(oldDBObject, newDBObject); } catch (UnknownHostException e) { e.printStackTrace(); } } /** * 查询全部客户信息列表 * * @return 客户信息列表 */ public static List<Customer> getCustomerList() { List<Customer> customerList = new ArrayList<Customer>(); Customer customer = null; try { Mongo mongo = CustomerService.getMongoDB(); String dbName = "test"; String collectionName = "users"; DB db = mongo.getDB(dbName); DBCollection dbCollection = db.getCollection(collectionName); DBCursor cur = dbCollection.find(); DBObject dbObject = null; while (cur.hasNext()) { dbObject = cur.next(); customer = new Customer(); customer.setName((String) dbObject.get("name")); customer.setAge((Integer) dbObject.get("age")); customer.setSex((String) dbObject.get("sex")); customerList.add(customer); } } catch (UnknownHostException e) { e.printStackTrace(); } return customerList; } /** * 为方便测试,该方法将客户信息列表打印出来 * * @param customerList * 客户信息列表 */ public static void printCustomerList(List<Customer> customerList) { System.out.println(">>>>>>>>>>>>>>>> CustomerList >>>>>>>>>>>>>>"); if (null == customerList || customerList.isEmpty()) { return; } for (int i = 0; i < customerList.size(); i++) { System.out.println(customerList.get(i).toString()); } System.out.println("<<<<<<<<<<<<<<< CustomerList <<<<<<<<<<<<<<<"); } } |
测试类Test.java代码如下:
package com; import java.net.UnknownHostException; public class Test { /** * @param args * @throws UnknownHostException * @throws InterruptedException */ public static void main(String[] args) throws UnknownHostException, InterruptedException { System.out.println("Enter main"); CustomerService.printCustomerList(CustomerService.getCustomerList()); Customer customer = new Customer(); customer.setName("mazi"); customer.setAge(30); customer.setSex("man"); CustomerService.removeCustomer(customer); CustomerService.printCustomerList(CustomerService.getCustomerList()); CustomerService.addCustomer(customer); CustomerService.printCustomerList(CustomerService.getCustomerList()); Customer newCustomer = new Customer(); newCustomer.setName("mazi"); newCustomer.setAge(33); newCustomer.setSex("man"); CustomerService.updateCustomer(customer, newCustomer); CustomerService.printCustomerList(CustomerService.getCustomerList()); CustomerService.removeCustomer(newCustomer); CustomerService.printCustomerList(CustomerService.getCustomerList()); Thread.sleep(1000); System.out.println("End main"); } } |
确认工程无报错之后,就可以点击进行测试了,如果一切OK的话,将会在console输出如下信息:
本文出自 “烟花易冷” 博客,转载请与作者联系!