Spring Source吭哧哼哧,从2011年2月开始到2011年2月终于把spring-data-mongo-1.0.1给Release出来了。从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.mongodb移动到org.springframework.data.mongodb.core,官网的HelloWorldExample却还是用org.springframework.data.document.mongodb做例子,实在造成不少误导。
-----垃圾的cnblogs,编辑了半天的东西,一下子乱了格式,要重来!-----
Spring Data Mongo需要依赖Spring Framework,因此,首先需要下载Spring Framework的jar包,新建一个Web工程,将Spring Framework的jar包引入。本文引用了如下组件:
其中,除了Spring的几个组件包以外,还引用了MongoDB的Driver:mongo-2.7.3.jar;DWR3.0的组件dwr.jar;spring-data-mongdb-1.0.1.RELEASE.jar以及Spring Data的公共组件spring-data-commons-core-1.2.1.RELEASE.jar。需要说明的是,本文所使用的Spring Data Mongo的组件版本,需要采用Spring Framework 3.0.7及以上版本,否则程序运行会报错。
引入上述组件后,需要修改Spring的配置文件ApplicationContext.xml文件,引入Spring Data Mongo的命名空间,并定义MongoDB的服务器地址和端口号,初始化MongoTemplate类,代码如下
View Code
1
2
3
12
13
14
15
16
17
18
19
20
21 classpath:server.properties
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 下面开始一个简单的样例。首先,定义一个HelloKitty bean
View Code
1 /**
2 *
3 */
4 package com.ics.bean;
5
6 import org.directwebremoting.annotations.DataTransferObject;
7
8 @DataTransferObject
9 public class HelloKitty
10 {
11 private String id;
12
13 private String name;
14
15 @Override
16 public String toString()
17 {
18 return "HelloKitty[" + "id=" + id + ", name=" + name + "]";
19 }
20
21 public String getId()
22 {
23 return id;
24 }
25
26 public void setId(String id)
27 {
28 this.id = id;
29 }
30
31 public String getName()
32 {
33 return name;
34 }
35
36 public void setName(String name)
37 {
38 this.name = name;
39 }
40 } 定义数据访问类,定义两个方法,一个用于在集合中插入一条记录,另一个根据name属性查询一条记录
View Code
1 /**
2 *
3 */
4 package com.ics.dao;
5
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.data.mongodb.core.MongoTemplate;
8 import org.springframework.data.mongodb.core.query.Criteria;
9 import org.springframework.data.mongodb.core.query.Query;
10
11 import com.ics.bean.HelloKitty;
12
13
14 public class HelloKittyDAO
15 {
16 /**
17 * 定义集合名称
18 */
19 private static String HELLOKITTY = "HelloKitty";
20
21 /**
22 * 操作MongoDB的对象
23 */
24 @Autowired
25 private MongoTemplate mongoTemplate;
26
27
28 public void createHelloKitty(HelloKitty hello)
29 {
30 mongoTemplate.insert(hello, HELLOKITTY);
31 }
32
33 public HelloKitty getHelloKittyByName(String name)
34 {
35 return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), HelloKitty.class, HELLOKITTY);
36 }
37 } 简单的Service方法
1 /**
2 *
3 */
4 package com.ics.service;
5
6 import org.springframework.beans.factory.annotation.Autowired;
7
8 import com.ics.bean.HelloKitty;
9 import com.ics.dao.HelloKittyDAO;
10
11 public class HelloKittyService
12 {
13 @Autowired
14 private HelloKittyDAO helloKittyDAO;
15
16 public String createHelloKitty(HelloKitty hello)
17 {
18 helloKittyDAO.createHelloKitty(hello);
19
20 HelloKitty ret = helloKittyDAO.getHelloKittyByName(hello.getName());
21
22 return ret == null ? "" : ret.getId();
23 }
24 } 通过DWR发布出去
1 /**
2 *
3 */
4 package com.ics.web.dwr;
5
6 import org.directwebremoting.annotations.RemoteMethod;
7 import org.directwebremoting.annotations.RemoteProxy;
8 import org.springframework.beans.factory.annotation.Autowired;
9
10 import com.ics.bean.HelloKitty;
11 import com.ics.service.HelloKittyService;
12
13 @RemoteProxy(name = "HelloKittyManage")
14 public class HelloKittyManage
15 {
16 @Autowired
17 private HelloKittyService helloKittyService;
18
19 @RemoteMethod
20 public String sayHello(HelloKitty hello)
21 {
22 return "hello " + helloKittyService.createHelloKitty(hello);
23 }
24 } 最后,在index.html访问这个DWR方法
1
2
3 hello DWR
4
5
6
7
8 function sayHello()
9 {
10 var helloworld = {"name":"xyzz"};
11 HelloKittyManage.sayHello(helloworld, function(data){alert(data);});
12 }
13
14
15
16 Spring 3.X with DWR
17 Retrieve test data
18
19 启动MongoDB服务器,运行样例程序。
在页面点击“Retrieve test data”,将会弹出“Hello 4f9fe5112d0182c5bc0a6c39”字样,其中“4f9fe5112d0182c5bc0a6c39”就是刚刚插入MongoDB中自动生成的ID。So easy,:)
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com