1024programmer Mongodb How SpringData integrates MongoDB access

How SpringData integrates MongoDB access

Spring
Source groaning, from February 2011 to February 2011, finally released spring-data-mongo-1.0.1 to Release. Versions from 1.0.0.M1 to 1.0.0.M3 are called Spring
Data Document. 1.0.0.M4 began to be renamed Spring Data MongoDB 1.0.0
M4, but the official website does not specify it. At first glance, it is a bit inexplicable, especially when MongoTemplate moves from org.springframework.data.document.mongodb to org.springframework.data.mongodb.core, the HelloWorldExample on the official website still uses org.springframework.data.document.mongodb as an example, which really causes a lot of misleading.

Spring Data Mongo needs to rely on the Spring Framework, therefore, first you need to download the Spring
Framework jar package, create a new Web project, and import the Spring Framework jar package. This article references the following components:

Among them, in addition to several component packages of Spring, the Driver of MongoDB is also referenced: mongo-2.7.3.jar; component dwr.jar of DWR3.0; spring-data-mongdb-1.0.1.RELEASE.jar and Spring
Data public component spring-data-commons-core-1.2.1.RELEASE.jar. It should be noted that the Spring used in this article
The component version of Data Mongo needs to use Spring Framework 3.0.7 or above, otherwise an error will be reported when the program runs.

the

After introducing the above components, you need to modify the Spring configuration file ApplicationContext.xml file to introduce Spring Data
Mongo namespace, and define the server address and port number of MongoDB, initialize the MongoTemplate class, the code is as follows

1
2
3 <beans xmlns="http://www.springframework.org/schema/beans"
4 xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
5 xmlns:cOntext=”http://www.springframework.org/schema/context”
6 xmlns:mOngo=”http://www.springframework.org/schema/data/mongo”
7 xmlns:dwr=”http://www.directwebbremoting.org/schema/spring-dwr”
8 xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
10 http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
11 http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd”>
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

Let’s start with a simple example. First, define a HelloKitty bean

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}

Define the data access class and define two methods, one for inserting a record in the collection, and the other for querying a record based on the name attribute

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 * Define collection name
18 */
19 private static String HELLOKITTY = “HelloKitty”;
20
21 /**
22 * Operate MongoDB objects
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}

Simple Service method

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 }
twenty four }

Published via 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 }
twenty four }

Finally, access this DWR method in index.html

1
2
3
4
5
6
7
14
15
16

Spring 3.X with DWR

17 Retrieve test data

18
19

Start the MongoDB server and run the sample program.

Click “Retrieve test data” on the page, and “Hello
4f9fe5112d0182c5bc0a6c39”, where “4f9fe5112d0182c5bc0a6c39” is the automatically generated ID just inserted into MongoDB. So
easy, 🙂

{
35 return mongoTemplate.findOne(new Query(Criteria.where(“name”).is(name)), HelloKitty.class, HELLOKITTY);
36 }
37}

Simple Service method

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 }
twenty four }

Published via 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 }
twenty four }

Finally, access this DWR method in index.html

1
2
3
4
5
6
7
14
15
16

Spring 3.X with DWR

17 Retrieve test data

18
19

Start the MongoDB server and run the sample program.

Click “Retrieve test data” on the page, and “Hello
4f9fe5112d0182c5bc0a6c39”, where “4f9fe5112d0182c5bc0a6c39” is the automatically generated ID just inserted into MongoDB. So
easy, 🙂

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/how-springdata-integrates-mongodb-access/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索