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进行数据操作,收获很 ...
随机推荐
- smali 语法之try catch语句
# virtual methods .method public onClick(Landroid/view/View;)V .locals 4 .parameter "v" .p ...
- MyEclipse10
1.配置tomcat Windows->Preferences->My Eclipse->Servers->Tomcat,对于64位操作系统而言,Tomcat home dir ...
- wcf安全
http://www.cnblogs.com/artech/archive/2011/07/07/customauthorization01.html 安全 http://www.cnblogs.co ...
- python关键字
python有多少关键字? >>> import keyword >>> keyword.kwlist ['and', 'as', 'assert', 'break ...
- [BZOJ 3282] Tree 【LCT】
题目链接:BZOJ - 3282 题目分析 这道题是裸的LCT,包含 Link , Cut 和询问两点之间的路径信息. 写代码时出现的错误:Access(x) 的循环中应该切断的是原来的 Son[x] ...
- [BZOJ 1907] 树的路径覆盖 【树形DP】
题目链接:BZOJ - 1907 题目分析 使用树形 DP,f[x][0] 表示以 x 为根的子树不能与 x 的父亲连接的最小路径数(即 x 是一个折线的拐点). f[x][1] 表示以 x 为根的子 ...
- 2015 年 Ruby 大盘点
2015 年 Ruby 圈发生了很多有趣的事,让我们跟随 Glenn Goodrich 来回顾一下 15 年 Ruby 的年度标志性事件. 2015 将要结束,这一年对于 Ruby 来说非常重要.如果 ...
- March of the Penguins
poj3498:http://poj.org/problem?id=3498 题意:某个冰块上有a只企鹅,总共可以跳出去b只,问是否可能所有的企鹅都跳到某一块冰块上,输出所有的可能的冰块的编号. 由于 ...
- Keil 的调试命令、在线汇编与断点设置
上一讲中我们学习了如何建立工程.汇编.连接工程,并获得目标代码,但是做到这一 步仅仅代表你的源程序没有语法错误,至于源程序中存在着的其它错误,必须通过调试才能 发现并解决,事实上,除了极简单的程序以外 ...
- 2.5.2 使用alertdialog 创建列表对话框
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...