Files
wahyd4.github.com/source/_posts/spring-data-mongodb-start.md
T
Hermes Bot 232ecd7c22 Rebuild blog source from GitHub Pages artifact
- 308 posts reverse-extracted from wahyd4.github.com (Hexo 6.3.0 + NexT 8.25)
- Hexo project with NexT theme, reading-experience custom styles
- publish-post.sh: write post -> hexo build -> PR (master untouched)
2026-08-04 09:44:07 +10:00

5.6 KiB
Raw Blame History

title, date, tags
title date tags
Spring Data MongoDB 简单使用 2012-08-06 00:00:00
java
入门
Spring
mongo

这篇文章,主要是作为一个记录,记录我一个小时之前搞定这个东西的兴奋心情。spring mongodb主要是mongodb-java-driver 进行了一些封装,然后可以配合强大的spring使用。我觉得可以简单把它看成一个mongo的”ORM”框架。通过我从星期五到星期天的捣腾,我发现熟悉spring 框架本身其实是最重要的。好下面直接上吧:

首先我在eclipse里面建了一个maven项目,你需要添加如下依赖信息(这里用到的spring框架是3.0.6.RELEASE版本)

 UTF-8
 3.0.6.RELEASE
 1.6.1
 

 
 junit
 junit
 4.8.1
 test
 
 
 org.springframework.data
 spring-data-mongodb
 1.0.2.RELEASE
 
 
 log4j
 log4j
 1.2.16
 
 
 org.slf4j
 slf4j-api
 ${slf4j.version}
 
 
 org.slf4j
 jcl-over-slf4j
 ${slf4j.version}
 
 
 org.slf4j
 slf4j-log4j12
 ${slf4j.version}
 
 
 
 org.springframework
 spring-core
 ${spring.version}
 
 
 commons-logging
 commons-logging
 
 
 
 
 org.springframework
 spring-test
 ${spring.version}
 test
 
 
 commons-logging
 commons-logging
 
 
 
 
 org.springframework
 spring-context
 ${spring.version}
 
 
 org.springframework
 spring-aop
 ${spring.version}
 
 
 cglib
 cglib
 2.2
 
 
 org.mongodb
 mongo-java-driver
 2.8.0
 
 
 org.springframework.data
 spring-data-commons-core
 1.2.1.RELEASE
 
 

由于这里面的某些依赖可能maven中央仓库没有,所以请在pom里添加下面两个spring的仓库:

 spring-maven-release
 Spring Maven Release Repository
 http://maven.springframework.org/release
 
 
 spring-maven-milestone
 Spring Maven Milestone Repository
 http://maven.springframework.org/milestone
 

下面进行我们的代码编写,首先我们需要一个简单的java POJO类:person.java用于存储人的信息,其有三个属性:id(和mongodb中的ObjectId对应)name-姓名,age-年龄。

package com.toozhao.mongo.bean;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

/**
 *
 * @author Junv
 *
 */
@Document
public class Person {

@Id
 private String id;
 private String name;
 private int age;

public Person() {

}

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

public String getId() {
 return id;
 }

public String getName() {
 return name;
 }

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

public void setName(String name) {
 this.name = name;
 }

public void setAge(int age) {
 this.age = age;
 }

public int getAge() {
 return age;
 }

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

}

然后我们定义一个执行操作的接口,和其实现类,这里可以看做是一个dao模式。在接口中我们只定义了保存对象和查询一条记录两个方法。

package com.toozhao.mongo.repository;

import com.toozhao.mongo.bean.Person;
/**
 *
 * @author Junv
 *
 */
public interface MongoAction {

 public void saveObject(Person objToSave);

 public Person findObject(String id);

}

实现类:

package com.toozhao.mongo.repository;

import org.springframework.beans.factory.annotation.Autowired;
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.stereotype.Repository;

import com.toozhao.mongo.bean.Person;

@Repository
public class PersonActionImp implements MongoAction {

@Autowired
 MongoOperations operation;

public void saveObject(Person objToSave) {
 operation.insert(objToSave);

}

public Person findObject(String id) {
 return operation.findOne(new Query(Criteria.where("_id").is(id)),
 Person.class);

}

}

@repository,和@autowired是spring框架中的,主要功能是起到类似反射的作用吧,具体我还没弄得很清楚。

写一个有main方法的java类,对功能进行简单测试:

package com.toozhao.mongo;

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

import com.toozhao.mongo.bean.Person;
import com.toozhao.mongo.repository.MongoAction;

public class MainTest {

private static final Log log = LogFactory.getLog(MainTest.class);

public static void main(String[] args) {

log.info("initial -----------------");
 ConfigurableApplicationContext context = null;
 //实例化appliaction Context对象
 context = new ClassPathXmlApplicationContext(
 "META-INF/spring/applicationContext.xml");
 //获取一个MongoAction Bean,以便后面执行相关操作
 MongoAction action = context.getBean(MongoAction.class);
 Person person = new Person("Tom", 11);

 action.saveObject(person);
 log.info("结果为:" + action.findObject(person.getId()));

}

}

最后,贴出applicationContext文档,里面主要对mongodb进行了简单的配置,和设置了repository的扫描目录(和@repository对应)。

 

 
 

 
 
 
 
 
 
 
 
 

 
 

当然你还需要做的就是运行这个项目,我们需要在pom.xml里面添加如下代码,为你需要运行的类:

 org.codehaus.mojo
 exec-maven-plugin
 1.1.1
 
 
 com.toozhao.mongo.MainTest
 
 

最后,使用 mvn exec:java 就可以运行代码了,当然你在有main方法那个类,右键以java程序的方式也可以运行。结果就是你应该可以看到程序像数据库junv.person中插入了一条数据。