springmvc整合mongodb 增查改删操作

7 篇文章 0 订阅
2 篇文章 0 订阅

  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);
}


}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值