Java Spring-data for mongodb

    spring项目提供了对大数据的支持,Spring-data 项目,其中有关于Spring Data ForMongodb的,研究了一下,弄了个小示例参考一下。

    application.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.0.xsd     
          http://www.springframework.org/schema/beans     
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<mongo:mongo host="localhost" port="27017" />

	<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
		<constructor-arg ref="mongo" />
		<constructor-arg name="databaseName" value="his" />
		<constructor-arg name="defaultCollectionName" value="person" />
	</bean>

	<bean id="personRepository" class="com.mongo.dao.impl.PersonRepository">
		<property name="mongoTemplate" ref="mongoTemplate"></property>
	</bean>

	<context:annotation-config />

</beans> 

   Person.java

package com.nbey.data.forward.mongo.persistence;

import java.io.Serializable;

public class Person implements Serializable {
	 private static final long serialVersionUID = 3617931430808763429L;  
     
	    private String id;     
	    private String name;     
	    private int age;  
	    public Person() {  
	        super();  
	    }  
	    public Person(String id, String name, int age) {  
	        super();  
	        this.id = id;  
	        this.name = name;  
	        this.age = age;  
	    }  
	    /** 
	     * @return the id 
	     */  
	    public String getId() {  
	        return id;  
	    }  
	    /** 
	     * @param id the id to set 
	     */  
	    public void setId(String id) {  
	        this.id = id;  
	    }  
	    /** 
	     * @return the name 
	     */  
	    public String getName() {  
	        return name;  
	    }  
	    /** 
	     * @param name the name to set 
	     */  
	    public void setName(String name) {  
	        this.name = name;  
	    }  
	    /** 
	     * @return the age 
	     */  
	    public int getAge() {  
	        return age;  
	    }  
	    /** 
	     * @param age the age to set 
	     */  
	    public void setAge(int age) {  
	        this.age = age;  
	    }  
	    /** 
	     *  
	     * @param name 
	     * @param age 
	     */  
	    public Person(String name, int age) {  
	        super();  
	        this.name = name;  
	        this.age = age;  
	    }     
	  
	     public String toString() {     
	            return "Person[id="+id+",name="+name+",age="+age+"]";     
	        }    
}

   interface  AbstractRepository.java

package com.nbey.data.forward.mongo.manager;

import java.util.List;

import com.nbey.data.forward.mongo.persistence.Person;

public interface AbstractRepository {
	/** 
     *  
     *<b>function:</b>��Ӷ��� 
     * @author cuiran 
     * @createDate 2012-12-12 11:41:30 
     */  
    public void insert(Person person);   
      
    /** 
     *  
     *<b>function:</b>���ID���Ҷ��� 
     * @author cuiran 
     * @createDate 2012-12-12 11:41:41 
     */  
    public Person findOne(String id);     
    /** 
     *  
     *<b>function:</b>��ѯ���� 
     * @author cuiran 
     * @createDate 2012-12-12 16:26:06 
     */  
    public List<Person> findAll();     
      
    public List<Person> findByRegex(String regex);  
    /** 
     *  
     *<b>function:</b>ɾ��ָ����ID���� 
     * @author cuiran 
     * @createDate 2012-12-12 16:26:16 
     */  
    public void removeOne(String id);     
    /** 
     *  
     *<b>function:</b>ɾ������ 
     * @author cuiran 
     * @createDate 2012-12-12 16:25:40 
     */  
    public void removeAll();     
    /** 
     * ͨ��ID�ҵ����޸� 
     *<b>function:</b> 
     * @author cuiran 
     * @createDate 2012-12-12 16:25:51 
     */  
    public void findAndModify(String id);     
  
}

  PersonRepository.java

 

package com.nbey.data.forward.mongo.manager.impl;

import java.util.List;
import java.util.regex.Pattern;

import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.query.Criteria;
import org.springframework.data.document.mongodb.query.Query;
import org.springframework.data.document.mongodb.query.Update;
import org.springframework.transaction.annotation.Transactional;

import com.nbey.data.forward.mongo.manager.AbstractRepository;
import com.nbey.data.forward.mongo.persistence.Person;

@Transactional
public class PersonRepository implements AbstractRepository {
	private MongoTemplate mongoTemplate;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.mongo.dao.AbstractRepository#findAll()
	 */
	@Override
	public List<Person> findAll() {
		// TODO Auto-generated method stub
		return getMongoTemplate().find(new Query(), Person.class);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.mongo.dao.AbstractRepository#findAndModify(java.lang.String)
	 */
	@Override
	public void findAndModify(String id) {
		// TODO Auto-generated method stub
		// new Query(Criteria.where("id").is(id)), new Update().inc("age", 3)

		getMongoTemplate().updateFirst(new Query(Criteria.where("id").is(id)),
				new Update().inc("age", 3));

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.mongo.dao.AbstractRepository#findByRegex(java.lang.String)
	 */
	@Override
	public List<Person> findByRegex(String regex) {
		// TODO Auto-generated method stub
		Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
		Criteria criteria = new Criteria("name").regex(pattern.toString());
		return getMongoTemplate().find(new Query(criteria), Person.class);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.mongo.dao.AbstractRepository#findOne(java.lang.String)
	 */
	@Override
	public Person findOne(String id) {
		// TODO Auto-generated method stub
		return getMongoTemplate().findOne(
				new Query(Criteria.where("id").is(id)), Person.class);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.mongo.dao.AbstractRepository#insert(com.mongo.bean.Person)
	 */
	@Override
	public void insert(Person person) {
		// TODO Auto-generated method stub
		getMongoTemplate().insert(person);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.mongo.dao.AbstractRepository#removeAll()
	 */
	@Override
	public void removeAll() {
		// TODO Auto-generated method stub
		List<Person> list = this.findAll();
		if (list != null) {
			for (Person person : list) {
				getMongoTemplate().remove(person);
			}
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.mongo.dao.AbstractRepository#removeOne(java.lang.String)
	 */
	@Override
	public void removeOne(String id) {
		// TODO Auto-generated method stub
		Criteria criteria = Criteria.where("id").in(id);
		if (criteria == null) {
			Query query = new Query(criteria);
			if (query != null
					&& getMongoTemplate().findOne(query, Person.class) != null)
				getMongoTemplate().remove(
						getMongoTemplate().findOne(query, Person.class));
		}

	}

	/**
	 * @return the mongoTemplate
	 */
	public MongoTemplate getMongoTemplate() {
		return mongoTemplate;
	}

	/**
	 * @param mongoTemplate
	 *            the mongoTemplate to set
	 */
	public void setMongoTemplate(MongoTemplate mongoTemplate) {
		this.mongoTemplate = mongoTemplate;
	}

}

  Main.java

 

package com.nbey.data.forward.mongo.startup;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.nbey.data.forward.mongo.manager.AbstractRepository;
import com.nbey.data.forward.mongo.manager.impl.PersonRepository;
import com.nbey.data.forward.mongo.persistence.Person;

public class Main {
	private static Log log = LogFactory.getLog(Main.class.getName());

	private AbstractRepository pr = null;

	public void init() {
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"application.xml");
		pr = (PersonRepository) ctx.getBean("personRepository");
	}

	public void insert() {

		Person p = new Person("cuiran", 27);
		pr.insert(p);
		
	}

	public void findOne() {
		String id = "50c83cb552c2ceb0463177d6";
		Person p = pr.findOne(id);
		log.debug(p);
	}

	public void listAll() {

		List<Person> list = pr.findAll();
		
		for (Person p : list) {
			log.debug(p.toString());
		}

	}

	public void start() {
		init();

		insert();
		listAll();

		findOne();
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Main t = new Main();
		t.start();
	}
}

 

你可能感兴趣的:(java)