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

学习使用spring data连接mongodb replset
1、分别在两个命令窗口中以replset方式启动两个mongodb进程
D:\mongodb\bin>mongod.exe --dbpath=d:\mongodata\data0 --port=10001 --replSet rcw
/127.0.0.1:20001

D:\mongodb\bin>mongod.exe --dbpath=d:\mongodata\data1 --port=20001 --replSet rcw
/127.0.0.1:10001

2、连接到任何一个mongodb实例,初始话replset
mongo 127.0.0.1:10001/admin

> db.runCommand({"replSetInitiate":{
... "_id":"rcw",
... "members":[{
... "_id":1,
... "host":"127.0.0.1:10001"
... },
... {
... "_id":2,
... "host":"127.0.0.1:20001"
... }
... ]
... }})

3、再加入一个仲裁服务器
D:\mongodb\bin>mongod.exe --dbpath=d:\mongodata\data1 --port 30001 replSet 127.0.0.1:10001
mongo 127.0.0.1:10001/admin
>rs.addArb({"127.0.0.1:30001"})


4、下载spring,spring-data-common即(spring-data-core)以及spring-mongodb,注意这里的data-common要下载1.2.1的,才能和mongodb1.0.1配合,最新版会出错,建立一个spring mvc项目,配置文件
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>人才信息管理</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/config/spring/app-config.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:/config/spring/mvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>
  <!-- <filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping> -->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <error-page>
    <error-code>500</error-code>
    <location>/WEB-INF/views/commons/timeout.jsp</location>
  </error-page>
  <error-page>
    <error