spring MVC 整合mongodb
Spring Mongodb
目录
1 SPRING整合MONGODB 1
1.1 环境准备 1
1.2 包依赖 1
1.3 配置 2
2 案列 5
2.1 SPRING MVC整合MONGODB代码案例 5
1 Spring整合Mongodb
1.1 环境准备
1. mongodb官网 http://www.mongodb.org/,下载mongodb安装包和mongodb的java驱动包。
mongodb安装包(下载地址http://www.mongodb.org/downloads)。Mongodb的安装和使用可见mongodb权威指南。
mongodb驱动包(下载地址https://github.com/mongodb/mongo-java-driver/downloads)
2. Spring下载中心(http://www.springsource.org/download/community)下载spring,spring-data-mongodb,spring-data-commons包。
1.2 包依赖
项目所需依赖包如下:
Mongodb驱动包:
mongo-2.8.0.jar
spring包:
aopalliance-1.0.jar
commons-logging-1.1.jar
org.springframework.aop-3.1.RELEASE.jar
org.springframework.asm-3.1.RELEASE.jar
org.springframework.beans-3.1.RELEASE.jar
org.springframework.context-3.1.RELEASE.jar
org.springframework.context.support-3.1.RELEASE.jar
org.springframework.core-3.1.RELEASE.jar
org.springframework.expression-3.1.RELEASE.jar
org.springframework.transaction-3.1.RELEASE.jar
org.springframework.web-3.1.RELEASE.jar
org.springframework.web.servlet-3.1.RELEASE.jar
log4j-1.2.16.jar
slf4j-log4j12-1.6.4.jar
slf4j-api-1.6.4.jar
Spring Data Mongodb包:
spring-data-mongodb-1.1.0.M2.jar
Spring Data Commons包:
spring-data-commons-core-1.4.0.M1.jar

1.3 配置
(1)配置Web.xml
Java代码 
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?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>TestSpringMongodb</display-name> <!— spring mvc dispatcher servlet --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> |
- 注意:url-pattern的配置为‘/’。
- (2)配置applicationContext.xml
Java代码 
- 12345678910111213141516171819202122232425
<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"><!-- Activates various annotations to be detected in bean classes --><context:annotation-config /><!-- Scans the classpathforannotated components that will be auto-registered as Spring beans.For example@Controllerand@Service. Make sure to set the correct base-package--><context:component-scan base-package="bgi.contrl"/><context:component-scan base-package="bgi.service"/><!-- Configures the annotation-driven Spring MVC Controller programming model.Note that, with Spring3.0,thistag works in Servlet MVC only! --><mvc:annotation-driven /><!-- Loads MongoDB configuraton --><importresource="mongo-config.xml"/></beans>
(3)配置mongo-config.xml
Java代码 
- 1234567891011121314151617181920
<?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:p="http://www.springframework.org/schema/p"xmlns:mongo="http://www.springframework.org/schema/data/mongo"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/data/mongohttp://www.springframework.org/schema/data/mongo/spring-mongo.xsd"><!-- Default bean name is'mongo'--><mongo:mongo host="localhost"port="27017"/><!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. --><bean id="mongoTemplate"class="org.springframework.data.mongodb.core.MongoTemplate"><constructor-arg ref="mongo"/><constructor-arg name="databaseName"value="test"/></bean></beans>
注意:官方文档和案例配置都是旧版本的配置案例,spring-data-mongo从1.0.0.M1到1.0.0.M3的版本叫做Spring Data Document。1.0.0.M4开始更名为Spring Data MongoDB 1.0.0 M4,不过官网并没有特别说明,乍一看有点莫名其妙,尤其是MongoTemplate从org.springframework.data.document.mongod移动到org.springframework.data.mongodb.core,官网的HelloWorldExample却还是用org.springframework.data.document.mongodb做配置案例。多少会导致使用时的误导。
(4)配置spring-servlet.xml
Java代码 
- 123456789101112
<?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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!-- Declare a view resolver --><bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"p:prefix="/"p:suffix=".jsp"/></beans>
注意:spring-servlet.xml的命名是根据web.xml中配置spring DispatcherServlet的名字 (<servlet-name>spring</servlet-name>)加上-servlet命名的。
2 案列
2.1 Spring mvc整合mongodb代码案例
(1),control层
Java代码 
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
package bgi.contrl; import java.util.logging.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import bgi.pojo.User; import bgi.service.UserService; @Controller @RequestMapping("/user") public class UserCtrl { private static Logger logger = Logger.getLogger(UserCtrl.class.getName()); @Autowired UserService userService; @RequestMapping("/index") public ModelAndView index(){ ModelAndView mav = new ModelAndView("/user/index"); return mav; } @RequestMapping("/save") public ModelAndView saveUser(User user){ ModelAndView mav = new ModelAndView("/user/index"); logger.info("save:"+user); userService.saveUser(user); return mav; } @RequestMapping("/find") public ModelAndView findUser(User user){ ModelAndView mav = new ModelAndView("/user/index"); user = userService.findUserByName(user.getName()); logger.info("find:"+user); return mav; } } (2),service层 Java代码 package bgi.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.stereotype.Service; import bgi.pojo.User; @Service public class UserService { private static String USER_COLLECTION = "user"; @Autowired MongoTemplate mongoTemplate; /** * * @param user */ public void saveUser(User user){ mongoTemplate.save(user, USER_COLLECTION); } /** * * @param name * @return */ public User findUserByName(String name){ return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), User.class, USER_COLLECTION); } } |
Java代码 
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package bgi.pojo; import java.io.Serializable; import org.springframework.data.annotation.Id; public class User implements Serializable{ private static final long serialVersionUID = 1L; @Id String uid; String name; int age; public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "{USER:{uid:"+this.uid+",name:"+this.name+",age:"+this.age+"}}"; } } |
- http://localhost:8080/TestSpringMongodb/user/save?name=xiaohong&age=23
- save:{USER:{uid:5020bef0fe7d16eb86275c7a,name:xiaohong,age:23}}
- http://localhost:8080/TestSpringMongodb/user/find?name=xiaohong
- find:{USER:{uid:5020bef0fe7d16eb86275c7a,name:xiaohong,age:23}}
spring MVC 整合mongodb的更多相关文章
- Spring与Struts2整合VS Spring与Spring MVC整合
Spring与Struts2整合,struts.xml在src目录下 1.在web.xml配置监听器 web.xml <!-- 配置Spring的用于初始化ApplicationContext的 ...
- MyBatis+Spring+Spring MVC整合开发
MyBatis+Spring+Spring MVC整合开发课程观看地址:http://www.xuetuwuyou.com/course/65课程出自学途无忧网:http://www.xuetuwuy ...
- 【RabbitMQ系列】 Spring mvc整合RabbitMQ
一.linux下安装rabbitmq 1.安装erlang环境 wget http://erlang.org/download/otp_src_18.2.1.tar.gz tar xvfz otp_s ...
- Java基础-SSM之Spring和Mybatis以及Spring MVC整合案例
Java基础-SSM之Spring和Mybatis以及Spring MVC整合案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 能看到这篇文章的小伙伴,详细你已经有一定的Java ...
- spring mvc整合mybaitis和log4j
在上一篇博客中,我介绍了在mac os上用idea搭建spring mvc的maven工程,但是一个完整的项目肯定需要数据库和日志管理,下面我就介绍下spring mvc整合mybatis和log4j ...
- Spring MVC 整合Swagger的一些问题总结
在做Spring MVC 整合swagger的时候,遇到的两个问题: 第一个问题 在网上找了一些Spring MVC 和Swagger的例子,照着一步步的配置,结果,到最后,项目都起来了,没有任何问题 ...
- 【Java Web开发学习】Spring MVC整合WebSocket通信
Spring MVC整合WebSocket通信 目录 ========================================================================= ...
- MongoDB和Java(6):Spring Data整合MongoDB副本集、分片集群
最近花了一些时间学习了下MongoDB数据库,感觉还是比较全面系统的,涉及了软件安装.客户端操作.安全认证.副本集和分布式集群搭建,以及使用Spring Data连接MongoDB进行数据操作,收获很 ...
- MongoDB和Java(5):Spring Data整合MongoDB(注解配置)
最近花了一些时间学习了下MongoDB数据库,感觉还是比较全面系统的,涉及了软件安装.客户端操作.安全认证.副本集和分布式集群搭建,以及使用Spring Data连接MongoDB进行数据操作,收获很 ...
随机推荐
- C语言变量声明加冒号的用法
有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位.例如在存放一个开关量时,只有0和1 两种状态, 用一位二进位即可.为了节省存储空间,并使处理简便,C语言又提供了一种数据结构 ...
- dictionary ----- python
Learn of dictionary,simple example of dictionary in “Simple Python tutorial"------------------ ...
- POJ 3041 Asteroids 最小点覆盖 == 二分图的最大匹配
Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape o ...
- MATLAB灰度图、中值滤波图
x=imread(‘x.jpg’); x=rbg2gray(x); %转成灰度图像 k=medfilt2(x); %中值滤波,默认为3X3矩阵 figure, imshow(k); medfil ...
- ios开发之C语言第一天
最近在学习ios开发,先学习C语言,再学习OC和swift.正所谓"万丈高楼平地起",打好基础是很重要的,所以C语言也必须好好学习.学习中所使用的操作系统是OS X,开发工具是Xc ...
- Scut:账号服务器问题修正
姑且记录一下,以防未来出现bug回来看看今天改了哪些. 原 Scut 账服是应用于 渠道频道 的账号服务器,每天会发放大量的游客账号,它有一个"自动将已经被注册了一段时间的游客账号再重新推送 ...
- require 书写约定
使用 Sea.js 书写模块代码时,需要遵循一些简单规则. 只是书写和调试时的规范!!!构建后的代码完全不需要遵循下面的约定!!!!!! 1. 正确拼写 模块 factory 构造方法的第一个参数 必 ...
- 转-[Python 学习]2.5版yield之学习心得
在 shhgs 发布了关于< Py 2.5 what’s new 之 yield>之后,原来我不是特别关注 yield 的用法,因为对于2.3中加入的yield相对来说功能简单,它是作为一 ...
- Silicon Labs电容式触摸感应按键技术原理及应用
市场上的消费电子产品已经开始逐步采用触摸感应按键,以取代传统的机械式按键.针对此趋势,Silicon Labs公司推出了内置微控制器(MCU)功能的电容式触摸感应按键(Capacitive Touch ...
- Quartz 有状态的JobDataMap
Quartz,每次执行job,job永远是全新的对象,但是,如果job实现org.quartz.StatefulJob接口,而不是job接口. 此时JobDetail的JobDataMap将会共享一个 ...