SpringBoot整合Mongodb實現(xiàn)增刪查改的方法
一、什么是MongoDB
MongoDB與我們之前熟知的關(guān)系型數(shù)據(jù)庫(MySQL、Oracle)不同,MongoDB是一個文檔數(shù)據(jù)庫,它具有所需的可伸縮性和靈活性,以及所需的查詢和索引。
MongoDB將數(shù)據(jù)存儲在靈活的、類似JSON的文檔中,這意味著文檔的字段可能因文檔而異,數(shù)據(jù)結(jié)構(gòu)也會隨著時間的推移而改變。文檔模型映射到應(yīng)用程序代碼中的對象,使數(shù)據(jù)易于處理。MongoDB是一個以分布式數(shù)據(jù)庫為核心的數(shù)據(jù)庫,因此高可用性、橫向擴展和地理分布是內(nèi)置的,并且易于使用。況且,MongoDB是免費的,開源的。
二、在Window10上安裝MongoDB

下載MSI版本(安裝版)

下載的時候選擇Custom

安裝的時候,注意不要勾上安裝可視化插件,否則安裝會非常慢(除非你網(wǎng)速夠快)

三、配置MongoDB服務(wù)
配置環(huán)境變量
復(fù)制當(dāng)前路徑

我的電腦->右鍵->高級系統(tǒng)設(shè)置->環(huán)境變量->系統(tǒng)變量
在系統(tǒng)變量找到Path,編輯,將上面復(fù)制的路徑增加進去

四、啟動服務(wù)
win+R->輸入services.msc


服務(wù)啟動后,在瀏覽器輸入 127.0.0.1:2701

出現(xiàn)這行英語則代表服務(wù)啟動成功。
五、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)新增實體類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)新建測試。這里我用navicat作為MongoDB的可視化工具進行查看。
測試一:插入操作
@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自動創(chuàng)建了數(shù)據(jù)庫以及通過實體類生成了集合(也就是我們經(jīng)常說的數(shù)據(jù)表),而且我們已經(jīng)通過MongoTemplate往數(shù)據(jù)庫的userEntity集合插入了幾條文檔(也就是插入了幾條記錄)。而 _id 為主鍵,_class 則為實體類包名+類名
測試二:查詢操作
@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'}
測試三:更新操作
@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ù)庫如圖所示:

測試四:刪除操作
@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實現(xiàn)簡單的增刪查改的文章就介紹到這了,更多相關(guān)SpringBoot整合Mongodb增刪查改內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot如何讀取application.yml文件
這篇文章主要介紹了springboot如何讀取application.yml文件的方法,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-12-12
Java 實戰(zhàn)項目錘煉之校園宿舍管理系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+jsp+javaweb+mysql+ajax實現(xiàn)一個校園宿舍管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11
Spring中@Configuration注解的Full模式和Lite模式詳解
這篇文章主要介紹了Spring中@Configuration注解的Full模式和Lite模式詳解,準(zhǔn)確來說,Full?模式和?Lite?模式其實?Spring?容器在處理?Bean?時的兩種不同行為,這兩種不同的模式在使用時候的表現(xiàn)完全不同,今天就來和各位小伙伴捋一捋這兩種模式,需要的朋友可以參考下2023-09-09
JDBC用IDEA連接SQLServer數(shù)據(jù)庫的超實用教程
JDBC是Java連接數(shù)據(jù)庫的一種接口,它由各個數(shù)據(jù)庫廠商為開發(fā)者提供的接口,要使用它需要到相應(yīng)廠商下載對應(yīng)的jar包,下面這篇文章主要給大家介紹了關(guān)于JDBC用IDEA連接SQLServer數(shù)據(jù)庫的超實用教程,需要的朋友可以參考下2023-05-05

