1、設定檔mongo-config.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation="http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/data/mongohttp://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 定義mongo對象,相關ip地址和連接埠 --> <mongo:mongo host="localhost" port="27017" /> <!-- mongo工廠,通過它來取得mongo執行個體 --> <mongo:db-factory dbname="mongodemo" /> <bean id="mappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext"></bean> <bean id="defaultMongoTypeMapper" class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper"> <constructor-arg name="typeKey" value="null" /> </bean> <!-- MongoMappingConverter類起到實體映射的作用,通過注釋把java類轉換為mongodb的文檔。 --> <bean id="mappingMongoConverter" class="org.springframework.data.mongodb.core.convert.MappingMongoConverter"> <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> <constructor-arg name="mappingContext" ref="mappingContext" /> <property name="typeMapper" ref="defaultMongoTypeMapper" /> </bean> <!-- mongodb的主要操作對象,所有對mongodb的增刪改查的操作都是通過它完成 --> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> <constructor-arg name="mongoConverter" ref="mappingMongoConverter" /> </bean></beans>
2、實體物件User.java
View Code
public class User { private String uname; private String upwd; public User(String uname,String upwd){ this.uname=uname; this.upwd=upwd; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpwd() { return upwd; } public void setUpwd(String upwd) { this.upwd = upwd; } public String toString() { return "[使用者資訊:UName:"+uname+"\tUpwd:"+upwd+"]"; } }
3、工具類MongoUtil.java
public class MongoUtil { static ApplicationContext ctx = new GenericXmlApplicationContext("mongo-config.xml"); private static MongoOperations mongoOperations=null; /** * 獲得MongoOperations對象(負責CRUD工作) * @return */ public static MongoOperations getMongoOperation(){ if(mongoOperations==null){ mongoOperations = (MongoOperations) ctx.getBean("mongoTemplate"); } return mongoOperations; } /** * 不定長度參數,構造查詢條件 * @param criterias * @return */ public static Query setQuery(Criteria...criterias){ Query query=new Query(); for (int i = 0; i < criterias.length; i++) { query.addCriteria(criterias[i]); } return null; }}
4、測試增刪除查改CRUDTest.java
package com.archie.test;import java.util.List;import org.springframework.data.mongodb.core.MongoOperations;import org.springframework.data.mongodb.core.query.Criteria;import org.springframework.data.mongodb.core.query.Query;import org.springframework.data.mongodb.core.query.Update;import com.archie.util.MongoUtil;import com.archie.vo.User;/** * 增刪除查改測試 * @author archie2010 * * since 2013-3-22 下午04:43:39 */public class CRUDTest { public static void main(String[] args) { MongoOperations mongoOperation=MongoUtil.getMongoOperation();
User user = new User("archie", "123"); // 增加 mongoOperation.save(user, "users"); Criteria criteriaName=Criteria.where("uname").is("tom"); Criteria criteriaPwd=Criteria.where("upwd").is("123"); Query query=new Query(); query.addCriteria(criteriaName); query.addCriteria(criteriaPwd); User userLogin = mongoOperation.findOne(query, User.class, "users"); System.out.println(userLogin); // 查詢 User savedUser = mongoOperation.findOne( new Query(Criteria.where("uname").is("archie")), User.class, "users"); System.out.println("savedUser : " + savedUser); // 更新 mongoOperation.updateMulti( new Query(Criteria.where("uname").is("archie")), Update.update("upwd", "1111111111"), "users"); // 查詢 User updatedUser = mongoOperation.findOne( new Query(Criteria.where("uname").is("archie")), User.class, "users"); System.out.println("updatedUser : " + updatedUser); // 刪除 mongoOperation.remove( new Query(Criteria.where("uname").is("archie")), "users"); // 查詢集合 List<User> listUser = mongoOperation.findAll(User.class, "users"); System.out.println("Number of user = " + listUser.size()); }}
Run: