1. 程式人生 > >springmvc整合mongodb 增查改刪操作

springmvc整合mongodb 增查改刪操作

  1. 1. 新建dynamic web project,專案結構如下:

編輯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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>springmvcmongodb</display-name>

<servlet>
<servlet-name>springmvcmongodb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>


<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springmvcmongodb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


</web-app>

編寫springmvc.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

<mvc:annotation-driven />

<context:component-scan base-package="com.yf.controller"></context:component-scan>

<!-- 試圖解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<!-- 靜態資源對映 -->
<mvc:resources location="/js/" mapping="/js/**" />
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/img/" mapping="/img/**" />
<mvc:resources location="/fonts/" mapping="/fonts/**" />

<import resource="classpath:mongo/mongodb-context.xml"/>
</beans>

編寫mongo.properties

mongo.host=192.168.31.33
mongo.port=27017
mongo.connectionsPerHost=8
mongo.threadsAllowedToBlockForConnectionMultiplier=4
#連線超時時間
mongo.connectTimeout=1000
#等待時間
mongo.maxWaitTime=1500
mongo.autoConnectRetry=true
mongo.socketKeepAlive=true
#socket超時時間
mongo.socketTimeout=1500
mongo.slaveOK=true
mongo.writeconcern=safe

編寫mongodb-context.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:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd">


<!-- 載入mongodb的配置屬性檔案 -->
<context:property-placeholder location="classpath:mongo/mongodb.properties" />

<mongo:mongo-client host="${mongo.host}" port="${mongo.port}"
id="mongo">
<mongo:client-options write-concern="${mongo.writeconcern}"
connect-timeout="${mongo.connectTimeout}" socket-keep-alive="${mongo.socketKeepAlive}" />
</mongo:mongo-client>


<!-- mongo:db-factory dbname="database" mongo-ref="mongo" / -->
<mongo:db-factory id="mongoDbFactory" dbname="mongoTest"
mongo-ref="mongo" />


<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>

</beans>

無法將名稱 'repository:auditing-attributes' 解析為 'attribute group' 元件

出現這個錯誤是沒有新增http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd

Usage of 'mongo-options' is no longer supported for MongoDB Java driver version 3 and above. Please use 'mongo-client-options' and refer to chapter 'MongoDB 3.0 Support' for details

mongodb 3.0開始需要定義mongo:mongo-client,而不是mongo:mongo

編寫Controller測試程式碼:

package com.yf.controller;


import java.util.List;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.BasicUpdate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


import com.yf.bean.Person;


/**
File: MongoController.java
Description: 控制器類
@author xxx
@date 2016年11月11日 上午10:33:59
@version 1.0 
**/


@Controller
public class MongoController {

@Autowired
private MongoTemplate mongoTemplate;

@RequestMapping("/")
public String goHome(HttpServletRequest request,HttpServletResponse response){
return "index";
}

@RequestMapping("/insert")
public String home(){
mongoTemplate.insert(new Person("小吳",36,"工作"));
return "insert";
}

@RequestMapping("/find")
public String find(){
List<Person> list = null;
list = mongoTemplate.findAll(Person.class);
if(list != null){
for(Person p: list){
System.out.println(p.toString());
}
}

return "find";
}

@RequestMapping("/update")
public String update(){
//Query query = new Query(Criteria.where("age").is(36));
//Update update = new Update().set("name", "abc");
//mongoTemplate.updateFirst(query, update, Person.class);
mongoTemplate.updateFirst(new Query(new Criteria("name").in("小吳")),  
                new Update().set("name", "大笨瓜"), Person.class);
return "update";
}


@RequestMapping("remove")
public void remove(){
Query query = new Query(Criteria.where("age").is(36));
mongoTemplate.remove(query, Person.class);
}


}