MongoDB與我們之前熟知的關係型資料庫(MySQL、Oracle)不同,MongoDB是一個文件資料庫,它具有所需的可擴展性和靈活性,以及所需的查詢和索引。
MongoDB將資料儲存在靈活的、類似JSON的文件中,這表示文件的欄位可能因文件而異,資料結構也會隨著時間的推移而改變。文檔模型映射到應用程式程式碼中的對象,使資料易於處理。 MongoDB是一個以分散式資料庫為核心的資料庫,因此高可用性、橫向擴展和地理分佈是內建的,並且易於使用。況且,MongoDB是免費的,開源的。
#開啟MongoDB官網
下載MSI版本(安裝版)
#下載的時候選擇Custom
#安裝的時候,注意不要勾上安裝視覺化插件,否則安裝會非常慢(除非你網速夠快)
設定環境變數
複製目前路徑
我的電腦->右鍵->進階系統設定->環境變數->系統變數
在系統變數找到Path,編輯,將上面複製的路徑增加進去
win R->輸入services.msc
服務啟動後,在瀏覽器輸入127.0.0.1:2701
出現這行英語則代表服務啟動成功。
環境準備
作業系統:Window10
IDE:IntelliJ IDEA 2018.2.4
資料庫: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); }
資料庫資訊:
可以看到,MongoDB自動建立了資料庫以及透過實體類別生成了集合(也就是我們常說的資料表),而且我們已經透過MongoTemplate在資料庫的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); }
輸出結果:
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()); //更新返回结果集的第一条 mongoTemplate.updateFirst(query,update,UserEntity.class); //更新返回结果集的所有 //mongoTemplate.updateMulti(query,update,UserEntity.class); }
更新後資料庫如圖所示:
測試四:刪除動作
@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); }
刪除後資料庫如圖所示:
#
以上是SpringBoot怎麼整合Mongodb實現增刪查改的詳細內容。更多資訊請關注PHP中文網其他相關文章!