Spring Data for MongoDBhtml
Spring Data for MongoDB提供了一個相似於基於Sping編程模型的NoSQL數據存儲。Spring Data for MongoDB提供了不少特性,它使不少MongoDB的Java開發者解放了不少。MongoTemplate helper類支持通用的Mongo操做。它整合了文檔和POJO之間的對象映射。一般,他會轉換數據庫訪問異常到Spring中的異常結構。使用起來很是的方便。
你能夠點擊這裏下載。java
五步安裝MongoDBspring
最清楚的安裝步驟固然是MongoDB官方的安裝說明了。安裝說明。mongodb
1 |
<strong>C:\mongodb\data\db</strong> |
1 |
C:\mongodb\bin\mongod.exe –dbpath C:\mongodb\data\db |
若是你的路徑包含空格,請使用雙引號引發來。數據庫
到這裏MongoDB的安裝就完成了,接下來使用java搞CRUD。express
五步使用Spring Data建立一個應用。編程
01 |
package com.orangeslate.naturestore.domain; |
02 |
03 |
import org.springframework.data.annotation.Id; |
04 |
import org.springframework.data.mongodb.core.mapping.Document; |
05 |
06 |
@Document |
07 |
public class Tree { |
08 |
09 |
@Id |
10 |
private String id; |
11 |
12 |
private String name; |
13 |
14 |
private String category; |
15 |
16 |
private int age; |
17 |
18 |
public Tree(String id, String name, int age) { |
19 |
this .id = id; |
20 |
this .name = name; |
21 |
this .age = age; |
22 |
} |
23 |
24 |
public String getId() { |
25 |
return id; |
26 |
} |
27 |
28 |
public void setId(String id) { |
29 |
this .id = id; |
30 |
} |
31 |
32 |
public String getName() { |
33 |
return name; |
34 |
} |
35 |
36 |
public void setName(String name) { |
37 |
this .name = name; |
38 |
} |
39 |
40 |
public String getCategory() { |
41 |
return category; |
42 |
} |
43 |
44 |
public void setCategory(String category) { |
45 |
this .category = category; |
46 |
} |
47 |
48 |
public int getAge() { |
49 |
return age; |
50 |
} |
51 |
52 |
public void setAge( int age) { |
53 |
this .age = age; |
54 |
} |
55 |
56 |
@Override |
57 |
public String toString() { |
58 |
return "Person [id=" + id + ", name=" + name + ", age=" + age |
59 |
+ ", category=" + category + "]" ; |
60 |
} |
61 |
} |
01 |
package com.orangeslate.naturestore.repository; |
02 |
03 |
import java.util.List; |
04 |
05 |
import com.mongodb.WriteResult; |
06 |
07 |
public interface Repository<T> { |
08 |
09 |
public List<T> getAllObjects(); |
10 |
11 |
public void saveObject(T object); |
12 |
13 |
public T getObject(String id); |
14 |
15 |
public WriteResult updateObject(String id, String name); |
16 |
17 |
public void deleteObject(String id); |
18 |
19 |
public void createCollection(); |
20 |
21 |
public void dropCollection(); |
22 |
} |
01 |
package com.orangeslate.naturestore.repository; |
02 |
03 |
import java.util.List; |
04 |
05 |
import org.springframework.data.mongodb.core.MongoTemplate; |
06 |
import org.springframework.data.mongodb.core.query.Criteria; |
07 |
import org.springframework.data.mongodb.core.query.Query; |
08 |
import org.springframework.data.mongodb.core.query.Update; |
09 |
10 |
import com.mongodb.WriteResult; |
11 |
import com.orangeslate.naturestore.domain.Tree; |
12 |
13 |
public class NatureRepositoryImpl implements Repository<Tree> { |
14 |
15 |
MongoTemplate mongoTemplate; |
16 |
17 |
public void setMongoTemplate(MongoTemplate mongoTemplate) { |
18 |
this .mongoTemplate = mongoTemplate; |
19 |
} |
20 |
21 |
/** |
22 |
* Get all trees. |
23 |
*/ |
24 |
public List<Tree> getAllObjects() { |
25 |
return mongoTemplate.findAll(Tree. class ); |
26 |
} |
27 |
28 |
/** |
29 |
* Saves a {<span class="referer">@link</span> Tree}. |
30 |
*/ |
31 |
public void saveObject(Tree tree) { |
32 |
mongoTemplate.insert(tree); |
33 |
} |
34 |
35 |
/** |
36 |
* Gets a {<span class="referer">@link</span> Tree} for a particular id. |
37 |
*/ |
38 |
public Tree getObject(String id) { |
39 |
return mongoTemplate.findOne( new Query(Criteria.where( "id" ).is(id)), |
40 |
Tree. class ); |
41 |
} |
42 |
43 |
/** |
44 |
* Updates a {<span class="referer">@link</span> Tree} name for a particular id. |
45 |
*/ |
46 |
public WriteResult updateObject(String id, String name) { |
47 |
return mongoTemplate.updateFirst( |
48 |
new Query(Criteria.where( "id" ).is(id)), |
49 |
Update.update( "name" , name), Tree. class ); |
50 |
} |
51 |
52 |
/** |
53 |
* Delete a {<span class="referer">@link</span> Tree} for a particular id. |
54 |
*/ |
55 |
public void deleteObject(String id) { |
56 |
mongoTemplate |
57 |
.remove( new Query(Criteria.where( "id" ).is(id)), Tree. class ); |
58 |
} |
59 |
60 |
/** |
61 |
* Create a {<span class="referer">@link</span> Tree} collection if the collection does not already |
62 |
* exists |
63 |
*/ |
64 |
public void createCollection() { |
65 |
if (!mongoTemplate.collectionExists(Tree. class )) { |
66 |
mongoTemplate.createCollection(Tree. class ); |
67 |
} |
68 |
} |
69 |
70 |
/** |
71 |
* Drops the {<span class="referer">@link</span> Tree} collection if the collection does already exists |
72 |
*/ |
73 |
public void dropCollection() { |
74 |
if (mongoTemplate.collectionExists(Tree. class )) { |
75 |
mongoTemplate.dropCollection(Tree. class ); |
76 |
} |
77 |
} |
78 |
} |
01 |
<? xml version = "1.0" encoding = "UTF-8" ?> |
02 |
< beans xmlns = "http://www.springframework.org/schema/beans" |
03 |
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:context = "http://www.springframework.org/schema/context" |
04 |
xsi:schemaLocation="http://www.springframework.org/schema/beans |
05 |
06 |
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd |
07 |
08 |
09 |
http://www.springframework.org/schema/context |
10 |
11 |
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> |
12 |
13 |
< bean id = "natureRepository" |
14 |
class = "com.orangeslate.naturestore.repository.NatureRepositoryImpl" > |
15 |
< property name = "mongoTemplate" ref = "mongoTemplate" /> |
16 |
</ bean > |
17 |
18 |
< bean id = "mongoTemplate" class = "org.springframework.data.mongodb.core.MongoTemplate" > |
19 |
< constructor-arg name = "mongo" ref = "mongo" /> |
20 |
< constructor-arg name = "databaseName" value = "nature" /> |
21 |
</ bean > |
22 |
23 |
<!-- Factory bean that creates the Mongo instance --> |
24 |
< bean id = "mongo" class = "org.springframework.data.mongodb.core.MongoFactoryBean" > |
25 |
< property name = "host" value = "localhost" /> |
26 |
< property name = "port" value = "27017" /> |
27 |
</ bean > |
28 |
29 |
<!-- Activate annotation configured components --> |
30 |
< context:annotation-config /> |
31 |
32 |
<!-- Scan components for annotations within the configured package --> |
33 |
< context:component-scan base-package = "com.orangeslate.naturestore" > |
34 |
< context:exclude-filter type = "annotation" |
35 |
expression = "org.springframework.context.annotation.Configuration" /> |
36 |
</ context:component-scan > |
37 |
38 |
</ beans > |
01 |
package com.orangeslate.naturestore.test; |
02 |
03 |
import org.springframework.context.ConfigurableApplicationContext; |
04 |
import org.springframework.context.support.ClassPathXmlApplicationContext; |
05 |
06 |
import com.orangeslate.naturestore.domain.Tree; |
07 |
import com.orangeslate.naturestore.repository.NatureRepositoryImpl; |
08 |
import com.orangeslate.naturestore.repository.Repository; |
09 |
10 |
public class MongoTest { |
11 |
12 |
public static void main(String[] args) { |
13 |
14 |
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext( |
15 |
"classpath:/spring/applicationContext.xml" ); |
16 |
17 |
Repository repository = context.getBean(NatureRepositoryImpl. class ); |
18 |
19 |
// cleanup collection before insertion |
20 |
repository.dropCollection(); |
21 |
22 |
// create collection |
23 |
repository.createCollection(); |