日期:2014-05-16  浏览次数:20469 次

MongoDB整合Spring
MongoDB和Spring整合,根据开发文档找到对应的版本
Spring Data Document 1.x binaries requires JDK level 6.0 and above, and Spring Framework 3.0.x and above.
In terms of document stores, MongoDB preferably version 1.6.5 or later or CouchDB 1.0.1 or later are required.
根据官方文档知道Spring必须是3.0.x或以上的版本,MongoDB必须是1.6.5以上的版本才行。
要想整合Spring和Mongodb必须下载相应的jar,这里主要是用到两种jar一种是spring-data-document和spring-data-commons两种类型的jar但是两种类型的jar版本必须匹配以免引起jar的冲突因为commons后面的版本jar目录有所改变所有版本必须要对应好下面是jar下载地址
http://www.springsource.org/spring-data/mongodb
http://www.springsource.org/spring-data/commons


下面是我下载的jar还有就是添加mongodb java驱动包,spring jar添加我就不多说了
Spring配置文件如下:
<?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"> 
  </mongo:mongo>
  
  <bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
    <constructor-arg ref="mongo"/>
    <constructor-arg name="databaseName" value="db"/>
    <constructor-arg name="defaultCollectionName" value="person" />
  </bean>
  	<bean id="personRepository" class="com.mongo.repository.PersonRepository">
		<property name="mongoTemplate" ref="mongoTemplate"></property>
	</bean>
 </beans>

添加实体类:
package com.mongo.entity;


public class Person {
	private String id;
	private String name;
	private int age;

	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
	
	public Person(){
		
	}
	
	public Person(String id, String name, int age) {
		this.id = id;
		this.name = name;
		this.age = age;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	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;
	}

	@Override
	public String toString() {
		return "Person[id="+id+",name="+name+",age="+age+"]";
	}

}


定义增删查改的接口
package com.mongo.intf;

import java.util.List;

import com.mongo.entity.Person;

public interface AbstractRepository {
	public void insert(Person person);
	public Person findOne(String id);
	public List<Person> findAll();
	public List<Person> findByRegex(String regex);
	public void removeOne(String id);
	public void removeAll();
	public void findAndModify(String id);
}


实现接口类:
package com.mongo.repository;

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 com.mongo.entity.Person;
import com.mongo.intf.AbstractRepository;

public class PersonRepository implements AbstractRepository{

	private MongoTemplate mongoTemplate;

	@Override
	public List<Person> findAll() {
		return getMongoTemplate().find(new Query(), Person.class);
	}

	@Override
	public void findAnd