MongoDB学习(四)SpringData与MongoDB集成

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/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
          http://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:

 

posted on 2012-12-22 19:43  archie2010  阅读(1399)  评论(0编辑  收藏  举报