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

MongoDB学习笔记之 第4章 MongoDB整合Spring
MongoDB学习笔记之 第1章 MongoDB的安装
MongoDB学习笔记之 第2章 MongoDB的增删改查
MongoDB学习笔记之 第3章 MongoDB的Java驱动
MongoDB学习笔记之 第4章 MongoDB整合Spring

?第4章 MongoDB整合Spring

(黎明你好原创作品,转载请注明)

4.1 创建maven项目

4.1.1 repositories

创建maven项目,其中repositories使用spring的maven库:

?

<repositories>
	<repository>
		<id>central</id>
		<name>Maven Central</name>
		<url>http://repo1.maven.org/maven2/</url>
	</repository>
	<repository>
		<id>spring-release</id>
		<name>Spring Maven Release Repository</name>
		<url>http://repo.springsource.org/libs-release</url>
	</repository>
	<repository>
		<id>atlassian-m2-repository</id>
		<url>https://m2proxy.atlassian.com/repository/public</url>
	</repository>
</repositories>

?

4.1.2 Dependencies

使用到的jar包:

?

<dependencies>
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>servlet-api</artifactId>
		<version>2.5</version>
		<type>jar</type>
		<scope>provided</scope>
	</dependency>
	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-api</artifactId>
		<version>1.6.1</version>
		<type>jar</type>
		<scope>compile</scope>
	</dependency>
	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-log4j12</artifactId>
		<version>1.7.5</version>
		<type>jar</type>
		<scope>runtime</scope>
	</dependency>
	<dependency>
		<groupId>org.mongodb</groupId>
		<artifactId>mongo-java-driver</artifactId>
		<version>2.10.1</version>
		<type>jar</type>
		<scope>compile</scope>
	</dependency>
	<dependency>
		<groupId>org.springframework.data</groupId>
		<artifactId>spring-data-mongodb</artifactId>
		<version>1.2.1.RELEASE</version>
		<type>jar</type>
		<scope>compile</scope>
	</dependency>
	<dependency>
		<groupId>org.springframework.data</groupId>
		<artifactId>spring-data-mongodb-cross-store</artifactId>
		<version>1.2.1.RELEASE</version>
		<type>jar</type>
		<scope>compile</scope>
	</dependency>
	<dependency>
		<groupId>org.springframework.data</groupId>
		<artifactId>spring-data-mongodb-log4j</artifactId>
		<version>1.2.1.RELEASE</version>
		<type>jar</type>
		<scope>compile</scope>
	</dependency>
</dependencies>

?

?

4.2 添加spring配置文件

spring的配置文件applicationContext.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/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/data/mongo     
        http://www.springframewor