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配置文件如下:


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">














添加实体类:

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 findAll();
public List 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 findAll() {
return getMongoTemplate().find(new Query(), Person.class);
}

@Override
public void findAndModify(String id) {
getMongoTemplate().updateFirst(new Query(Criteria.where("id").is(id)), new Update().inc("age", 3));
}

@Override
public List findByRegex(String regex) {
Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
Criteria criteria = new Criteria("name").regex(pattern.toString());
return getMongoTemplate().find(new Query(criteria), Person.class);
}

@Override
public Person findOne(String id) {
return getMongoTemplate().findOne(new Query(Criteria.where("id").is(id)), Person.class);
}

@Override
public void insert(Person person) {
getMongoTemplate().insert(person);
}

@Override
public void removeAll() {
List list = this.findAll();
if(list != null){
for(Person person : list){
getMongoTemplate().remove(person);
}
}
}

@Override
public void removeOne(String id){
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));
}
}

public MongoTemplate getMongoTemplate() {
return mongoTemplate;
}

public void setMongoTemplate(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}

}


添加一个测试类就可以测试以上的方法了

你可能感兴趣的:(mongodb)