MongoDB現在用的非常非常多,如何和Spring整合也是經常碰到的問題。
Spring提供了MongoTemplate
這樣一個模板類的實現方法,簡化了具體操作。
下面講一下具體實現:
添加依賴
1
2
3
4
5
|
< dependency > < groupId >org.springframework.data</ groupId > < artifactId >spring-data-mongodb</ artifactId > < version >1.10.3.RELEASE</ version > </ dependency > |
其余Spring相關的忽略
Spring的配置applicationContext-mongo.xml
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
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:util = "http://www.springframework.org/schema/util" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:context = "http://www.springframework.org/schema/context" xmlns:mongo = "http://www.springframework.org/schema/data/mongo" xsi:schemaLocation=" http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd"> < context:property-placeholder ignore-unresolvable = "true" location = "classpath:/mongodb.properties" /> < mongo:mongo-client id = "mongoClient" host = "${mongo.host}" port = "${mongo.port}" > <!-- credentials="${mongo.user}:${mongo.pwd}@${mongo.defaultDbName}"--> </ mongo:mongo-client > < mongo:db-factory id = "mongoDbFactory" dbname = "${mongo.database}" mongo-ref = "mongoClient" /> <!-- 默認Mongodb類型映射 --> < bean id = "defaultMongoTypeMapper" class = "org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper" > < constructor-arg name = "typeKey" > <!-- 這里設置為空,可以把 spring data mongodb 多余保存的_class字段去掉 --> < null /> </ constructor-arg > </ bean > < mongo:repositories base-package = "com.critc.mongo" /> <!-- 自動掃描以下包的有Doucment注解的類 --> < mongo:mapping-converter id = "mappingConverter" base-package = "com.critc.mongo.model" type-mapper-ref = "defaultMongoTypeMapper" > </ mongo:mapping-converter > <!-- Mongodb的模板 --> < bean id = "mongoTemplate" class = "org.springframework.data.mongodb.core.MongoTemplate" > < constructor-arg name = "mongoDbFactory" ref = "mongoDbFactory" /> < constructor-arg name = "mongoConverter" ref = "mappingConverter" /> </ bean > </ beans > |
mongo:mongo-client
是定義MongoDB的客戶端連接,需要host和port參數,如果需要賬號密碼的話,需要增加credentials配置
mongo:db-factory
配置連接工廠,指定具體的連接數據庫,本例默認為test
defaultMongoTypeMapper
默認Mongodb類型映射
mongo:mapping-converterMongoDB
的實體映射
mongoTemplate
這是最主要的,定義模板類,依賴連接工廠和實體映射
這里舉一個article的增刪改查的例子。
相關實體Article.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@Document (collection = "article_info" ) public class Article { @Id private String id; //id @Field ( "title" ) private String title; //標題 @Field ( "url" ) private String url; //鏈接 @Field ( "author" ) private String author; //作者 @Field ( "tags" ) private List<String> tags; //tag 標簽 @Field ( "visit_count" ) private Long visitCount; //訪問次數 @Field ( "add_time" ) private Date addTime; //添加時間 // get set方法省略 |
@Document(collection = "article_info")
這個注解和Hibernate的注解Entiry
非常相似,就是定義一個文檔,對象MongoDB存儲的Collection的名稱是article_info
@Id指該字段是主鍵,不能缺少
@Field("add_time")指該字段映射MongoDB的實際字段,如果一致可以省略
ArticleRepository實際訪問接口
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
|
@Repository ( "ArticleRepository" ) public interface ArticleRepository extends PagingAndSortingRepository<Article, String> { //分頁查詢 public Page<Article> findAll(Pageable pageable); //根據author查詢 public List<Article> findByAuthor(String author); //根據作者和標題查詢 public List<Article> findByAuthorAndTitle(String author, String title); //忽略參數大小寫 public List<Article> findByAuthorIgnoreCase(String author); //忽略所有參數大小寫 public List<Article> findByAuthorAndTitleAllIgnoreCase(String author, String title); //排序 public List<Article> findByAuthorOrderByVisitCountDesc(String author); public List<Article> findByAuthorOrderByVisitCountAsc(String author); //自帶排序條件 public List<Article> findByAuthor(String author, Sort sort); } |
Spring的data repository封裝了一套增刪改查的方法,就和JPA實現的一樣,ArticleRepository
繼承PagingAndSortingRepository
,就集成了常用的增刪改查方法,比如save、findOne、exists、findAll、delete等等,可以采用默認實現方式來完成常用的增刪改查操作。
測試上述各個方法ArticleRepositoryTest.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
@RunWith (SpringJUnit4ClassRunner. class ) @ContextConfiguration (locations = { "classpath*:applicationContext-mongo.xml" }) public class ArticleRepositoryTest { @Autowired private ArticleRepository articleRepository; /** * 新增記錄 */ @Test public void add() { //增加一條記錄 Article article = new Article(); article.setId( "1" ); article.setTitle( "MongoTemplate的基本使用" ); article.setAuthor( "kcy" ); article.setUrl( "http://jianshu.com/" ); article.setTags(Arrays.asList( "java" , "mongodb" , "spring" )); article.setVisitCount(0L); article.setAddTime( new Date()); articleRepository.save(article); //批量添加 List<Article> articles = new ArrayList<>( 10 ); for ( int i = 0 ; i < 10 ; i++) { Article article2 = new Article(); article2.setId(String.valueOf(i + 1 )); article2.setTitle( "MongoTemplate的基本使用" ); article2.setAuthor( "kcy" ); article2.setUrl( "http://jianshu.com" + i); article2.setTags(Arrays.asList( "java" , "mongodb" , "spring" )); article2.setVisitCount(0L); article2.setAddTime( new Date()); articles.add(article2); } articleRepository.save(articles); } /** * 修改記錄,修改id為1的訪問次數+1 */ @Test public void update() { Article article = articleRepository.findOne( "1" ); article.setVisitCount(article.getVisitCount() + 1 ); articleRepository.save(article); } /** * 批量修改,查看author為kcy的統一修改為kcy2 */ @Test public void batchUpdate() { List<Article> articles = articleRepository.findByAuthor( "kcy" ); articles.forEach(article -> { article.setAuthor( "kcy2" ); }); articleRepository.save(articles); } /** * 刪除記錄,刪除id為10的 */ @Test public void delete() { Article article = articleRepository.findOne( "10" ); articleRepository.delete(article); } @Test public void batchDelete() { List<Article> articles = articleRepository.findByAuthor( "kcy2" ); articleRepository.delete(articles); } /** * 查詢所有 * * @author 孔垂云 */ @Test public void findAll() { Iterable<Article> articles = articleRepository.findAll(); articles.forEach(article -> { System.out.println(article.toString()); }); } /** * 根據author查詢 * * @author 孔垂云 */ @Test public void findByAuthor() { List<Article> articles = articleRepository.findByAuthor( "kcy" ); articles.forEach(article -> { System.out.println(article.toString()); }); } /** * 按照author和title查詢 * * @author 孔垂云 */ @Test public void findByAuthorAndTitle() { List<Article> articles = articleRepository.findByAuthorAndTitle( "kcy" , "MongoTemplate的基本使用" ); articles.forEach(article -> { System.out.println(article.toString()); }); } /** * 根據作者查詢,忽略大小寫 * * @author 孔垂云 */ @Test public void findByAuthorIgnoreCase() { List<Article> articles = articleRepository.findByAuthorIgnoreCase( "JASON" ); articles.forEach(article -> { System.out.println(article.getId()); }); } /** * 忽略所有參數的大小寫 * * @author 孔垂云 */ @Test public void findByAuthorAndTitleAllIgnoreCase() { List<Article> articles = articleRepository.findByAuthorAndTitleAllIgnoreCase( "KCY" , "MONGOTEMPLATE的基本使用" ); articles.forEach(article -> { System.out.println(article.toString()); }); } /** * 根據author查詢,并且以訪問次數降序排序顯示 * * @author 孔垂云 */ @Test public void findByAuthorOrderByVisitCountDesc() { List<Article> articles = articleRepository.findByAuthorOrderByVisitCountDesc( "kcy" ); articles.forEach(article -> { System.out.println(article.toString()); }); } /** * 根據作者查詢,并且以訪問次數升序排序顯示 * * @author 孔垂云 */ @Test public void findByAuthorOrderByVisitCountAsc() { List<Article> articles = articleRepository.findByAuthorOrderByVisitCountAsc( "kcy" ); articles.forEach(article -> { System.out.println(article.toString()); }); } /** * 自帶排序條件 * * @author 孔垂云 */ @Test public void findByAuthorBySort() { List<Article> articles = articleRepository.findByAuthor( "kcy" , new Sort(Direction.ASC, "VisitCount" )); articles.forEach(article -> { System.out.println(article.toString()); }); } /** * 分頁查詢所有,并且排序 */ @Test public void findByPage() { int page = 1 ; int size = 2 ; Pageable pageable = new PageRequest(page, size, new Sort(Direction.ASC, "VisitCount" )); Page<Article> pageInfo = articleRepository.findAll(pageable); //總數量 System.out.println(pageInfo.getTotalElements()); //總頁數 System.out.println(pageInfo.getTotalPages()); for (Article article : pageInfo.getContent()) { System.out.println(article.toString()); } } } |
上面一段代碼較長,基本上MongoDB常用的各種例子都講清楚了,比如增加、批量增加、修改、刪除、按id查找、按標題查詢、分頁等等。
源碼下載
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.jianshu.com/p/cba5d31ea70e