SpringBoot整合Mongodb實(shí)現(xiàn)增刪查改的方法
一、什么是MongoDB
MongoDB與我們之前熟知的關(guān)系型數(shù)據(jù)庫(MySQL、Oracle)不同,MongoDB是一個(gè)文檔數(shù)據(jù)庫,它具有所需的可伸縮性和靈活性,以及所需的查詢和索引。
MongoDB將數(shù)據(jù)存儲(chǔ)在靈活的、類似JSON的文檔中,這意味著文檔的字段可能因文檔而異,數(shù)據(jù)結(jié)構(gòu)也會(huì)隨著時(shí)間的推移而改變。文檔模型映射到應(yīng)用程序代碼中的對(duì)象,使數(shù)據(jù)易于處理。MongoDB是一個(gè)以分布式數(shù)據(jù)庫為核心的數(shù)據(jù)庫,因此高可用性、橫向擴(kuò)展和地理分布是內(nèi)置的,并且易于使用。況且,MongoDB是免費(fèi)的,開源的。
二、在Window10上安裝MongoDB
下載MSI版本(安裝版)
下載的時(shí)候選擇Custom
安裝的時(shí)候,注意不要勾上安裝可視化插件,否則安裝會(huì)非常慢(除非你網(wǎng)速夠快)
三、配置MongoDB服務(wù)
配置環(huán)境變量
復(fù)制當(dāng)前路徑
我的電腦->右鍵->高級(jí)系統(tǒng)設(shè)置->環(huán)境變量->系統(tǒng)變量
在系統(tǒng)變量找到Path,編輯,將上面復(fù)制的路徑增加進(jìn)去
四、啟動(dòng)服務(wù)
win+R->輸入services.msc
服務(wù)啟動(dòng)后,在瀏覽器輸入 127.0.0.1:2701
出現(xiàn)這行英語則代表服務(wù)啟動(dòng)成功。
五、SpringBoot整合MongoDB
環(huán)境準(zhǔn)備
操作系統(tǒng):Window10
IDE:IntelliJ IDEA 2018.2.4
數(shù)據(jù)庫:MongoDB
1)引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
2)在application.yml添加如下配置
spring: data: mongodb: uri: mongodb://localhost/test_mongodb
完整的配置信息如下:
spring: data: mongodb: authentication-database: # Authentication database name. database: # Database name. field-naming-strategy: # Fully qualified name of the FieldNamingStrategy to use. grid-fs-database: # GridFS database name. host: # Mongo server host. Cannot be set with URI. password: # Login password of the mongo server. Cannot be set with URI. port: # Mongo server port. Cannot be set with URI. repositories: type: # Type of Mongo repositories to enable. uri: # Mongo database URI. Cannot be set with host, port and credentials. username: # Login user of the mongo server. Cannot be set with URI.
3)新增實(shí)體類UserEntity
public class UserEntity { @Id private String uid; private String username; private String password; public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "UserEntity{" + "uid='" + uid + '\'' + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }
4)新建測(cè)試。這里我用navicat作為MongoDB的可視化工具進(jìn)行查看。
測(cè)試一:插入操作
@Autowired private MongoTemplate mongoTemplate; @Test public void saveUser(){ UserEntity userEntity1 = new UserEntity(); UserEntity userEntity2 = new UserEntity(); UserEntity userEntity3 = new UserEntity(); userEntity1.setUid("111"); userEntity1.setUsername("用戶1"); userEntity1.setPassword("密碼1"); userEntity2.setUid("222"); userEntity2.setUsername("用戶2"); userEntity2.setPassword("密碼2"); userEntity3.setUid("333"); userEntity3.setUsername("用戶3"); userEntity3.setPassword("密碼3"); mongoTemplate.save(userEntity1); mongoTemplate.save(userEntity2); mongoTemplate.save(userEntity3); }
數(shù)據(jù)庫信息:
可以看到,MongoDB自動(dòng)創(chuàng)建了數(shù)據(jù)庫以及通過實(shí)體類生成了集合(也就是我們經(jīng)常說的數(shù)據(jù)表),而且我們已經(jīng)通過MongoTemplate往數(shù)據(jù)庫的userEntity集合插入了幾條文檔(也就是插入了幾條記錄)。而 _id 為主鍵,_class 則為實(shí)體類包名+類名
測(cè)試二:查詢操作
@Autowired private MongoTemplate mongoTemplate; @Test public void findUserByUserName(){ String username = "用戶1"; Query query=new Query(Criteria.where("username").is(username)); UserEntity user = mongoTemplate.findOne(query , UserEntity.class); System.out.println(user); }
輸出結(jié)果:
UserEntity{uid='111', username='用戶1', password='密碼1'}
測(cè)試三:更新操作
@Autowired private MongoTemplate mongoTemplate; @Test public void updateUser(){ UserEntity userEntity = new UserEntity(); userEntity.setUid("111"); userEntity.setUsername("更新后的用戶名"); userEntity.setPassword("更新后的密碼"); Query query = new Query(Criteria.where("_id").is(userEntity.getUid())); Update update = Update.update("username",userEntity.getUsername()).set("password",userEntity.getPassword()); //更新返回結(jié)果集的第一條 mongoTemplate.updateFirst(query,update,UserEntity.class); //更新返回結(jié)果集的所有 //mongoTemplate.updateMulti(query,update,UserEntity.class); }
更新后數(shù)據(jù)庫如圖所示:
測(cè)試四:刪除操作
@Autowired private MongoTemplate mongoTemplate; @Test public void DeleteByUserId(){ String id = "222"; Query query=new Query(Criteria.where("_id").is(id)); mongoTemplate.remove(query,UserEntity.class); }
刪除后數(shù)據(jù)庫如圖所示:
到此這篇關(guān)于SpringBoot整合Mongodb實(shí)現(xiàn)簡(jiǎn)單的增刪查改的文章就介紹到這了,更多相關(guān)SpringBoot整合Mongodb增刪查改內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Centos中yum方式安裝java的實(shí)現(xiàn)示例
這篇文章主要介紹了Centos中yum方式安裝java的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04springboot如何讀取application.yml文件
這篇文章主要介紹了springboot如何讀取application.yml文件的方法,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-12-12Java詳解對(duì)象終止方法finalize()的用法
在前面的 jvm 中, 需要補(bǔ)充幾個(gè)部分的內(nèi)容, 接著來看 finalize() 機(jī)制, 它可以使接近死亡的對(duì)象復(fù)活, 下來我們來看是怎么一回事2022-05-05Java 實(shí)戰(zhàn)項(xiàng)目錘煉之校園宿舍管理系統(tǒng)的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+jsp+javaweb+mysql+ajax實(shí)現(xiàn)一個(gè)校園宿舍管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11Spring中@Configuration注解的Full模式和Lite模式詳解
這篇文章主要介紹了Spring中@Configuration注解的Full模式和Lite模式詳解,準(zhǔn)確來說,Full?模式和?Lite?模式其實(shí)?Spring?容器在處理?Bean?時(shí)的兩種不同行為,這兩種不同的模式在使用時(shí)候的表現(xiàn)完全不同,今天就來和各位小伙伴捋一捋這兩種模式,需要的朋友可以參考下2023-09-09JDBC用IDEA連接SQLServer數(shù)據(jù)庫的超實(shí)用教程
JDBC是Java連接數(shù)據(jù)庫的一種接口,它由各個(gè)數(shù)據(jù)庫廠商為開發(fā)者提供的接口,要使用它需要到相應(yīng)廠商下載對(duì)應(yīng)的jar包,下面這篇文章主要給大家介紹了關(guān)于JDBC用IDEA連接SQLServer數(shù)據(jù)庫的超實(shí)用教程,需要的朋友可以參考下2023-05-05