前言
mongodb 是一個介于關系數據庫和非關系數據庫之間的產品,是非關系數據庫當中功能最豐富,最像關系數據庫的。
提示:以下是本篇文章正文內容,下面案例可供參考
一、技術介紹
1.mongodb是什么?
mongodb(來自于英文單詞“humongous”,中文含義為“龐大”)是可以應用于各種規模的企業、各個行業以及各類應用程序的開源數據庫。作為一個適用于敏捷開發的數據庫,mongodb的數據模式可以隨著應用程序的發展而靈活地更新。與此同時,它也為開發人員 提供了傳統數據庫的功能:二級索引,完整的查詢系統以及嚴格一致性等等。 mongodb能夠使企業更加具有敏捷性和可擴展性,各種規模的企業都可以通過使用mongodb來創建新的應用,提高與客戶之間的工作效率,加快產品上市時間,以及降低企業成本。
mongodb是專為可擴展性,高性能和高可用性而設計的數據庫。它可以從單服務器部署擴展到大型、復雜的多數據中心架構。利用內存計算的優勢,mongodb能夠提供高性能的數據讀寫操作。 mongodb的本地復制和自動故障轉移功能使您的應用程序具有企業級的可靠性和操作靈活性。
二、使用步驟
1.引入maven庫
代碼如下(示例):
1
2
3
4
5
6
7
8
9
10
11
12
|
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 2.4 . 1 </version> <relativepath/> </parent> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-mongodb</artifactid> </dependency> </dependencies> |
2.具體使用示例
mongodb封裝:
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
|
/** * mongo db助手 * * @author: heyuhua * @date: 2021/1/19 9:52 */ @component public class mongodbhelper { @autowired private mongotemplate mongotemplate; /** * 保存 * * @param t * @param <t> * @return */ public <t> t save(t t) { return mongotemplate.save(t); } /** * 保存 * * @param t * @param collectionname * @param <t> * @return */ public <t> t save(t t, string collectionname) { return mongotemplate.save(t, collectionname); } /** * 查詢 * * @param query * @param tclass * @param <t> * @return */ public <t> list<t> find(query query, class <t> tclass) { return mongotemplate.find(query, tclass); } /** * 查詢所有 * * @param tclass * @param <t> * @return */ public <t> list<t> findall( class <t> tclass) { return mongotemplate.findall(tclass); } } |
3.配置文件
代碼如下(示例):
1
2
3
4
5
6
7
8
9
|
server: port: 8088 spring: #mongodb配置 data: mongodb: |
4.單元測試
測試代碼如下(示例):
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
|
package com.hyh.core.test; import com.hyh.core.test.base.hyhtest; import com.hyh.core.test.po.person; import com.hyh.mongodb.helper.mongodbhelper; import org.junit.test; import org.springframework.beans.factory.annotation.autowired; import org.springframework.data.mongodb.core.query.criteria; import org.springframework.data.mongodb.core.query.criteriadefinition; import org.springframework.data.mongodb.core.query.query; import org.springframework.data.mongodb.core.query.textcriteria; import java.util.list; /** * mongodb test * * @author: heyuhua * @date: 2021/1/19 10:28 */ public class mongodbtest extends hyhtest { @autowired private mongodbhelper mongodbhelper; @test public void testsave() { person person = new person(); person.setname( "heyuhua" ); person.setage( 25 ); mongodbhelper.save(person); person person2 = new person(); person2.setname( "hyh" ); person2.setage( 52 ); mongodbhelper.save(person2); } @test public void testsavecollection() { person person = new person(); person.setname( "heyuhua" ); person.setage( 25 ); mongodbhelper.save(person, "personcollection" ); person person2 = new person(); person2.setname( "hyh" ); person2.setage( 52 ); mongodbhelper.save(person2, "personcollection" ); } @test public void testfindall() { list<person> list = mongodbhelper.findall(person. class ); for (person person : list) { system.out.println( "name=" + person.getname() + ",age=" + person.getage()); } } @test public void testfind() { criteria criteria = new criteria(); criteria.and( "age" ).gte( "25" ); query query = new query(criteria); list<person> list = mongodbhelper.find(query,person. class ); for (person person : list) { system.out.println( "name=" + person.getname() + ",age=" + person.getage()); } } @test @override public void test() { system.out.println( "---mongodb test---" ); } } |
總結
是不是感覺很簡單?更多用法請點擊下方查看源碼,關注我帶你揭秘更多高級用法
源碼地址:點此查看源碼.
到此這篇關于springboot輕松整合mongodb的文章就介紹到這了,更多相關springboot輕松整合mongodb內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://juejin.cn/post/6922634986856120327