javascript
SpringBoot2.0 基础案例(15):配置MongoDB数据库,实现增删改查逻辑
一、NoSQL簡(jiǎn)介
1、NoSQL 概念
NoSQL( Not Only SQL ),意即"不僅僅是SQL"。對(duì)不同于傳統(tǒng)的關(guān)系型數(shù)據(jù)庫的數(shù)據(jù)庫管理系統(tǒng)的統(tǒng)稱。NoSQL用于超大規(guī)模數(shù)據(jù)的存儲(chǔ)。這些類型的數(shù)據(jù)存儲(chǔ)不需要固定的模式,無需多余操作就可以橫向擴(kuò)展。
2、NoSQL的優(yōu)點(diǎn)/缺點(diǎn)
--優(yōu)點(diǎn): 高可擴(kuò)展性 分布式計(jì)算 低成本 架構(gòu)的靈活性,半結(jié)構(gòu)化數(shù)據(jù) 沒有復(fù)雜的關(guān)系 --缺點(diǎn): 沒有標(biāo)準(zhǔn)化 有限的查詢功能(到目前為止) 數(shù)據(jù)展現(xiàn)不直觀二、MongoDB數(shù)據(jù)庫
1、MongoDB簡(jiǎn)介
MongoDB 是一個(gè)介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。他支持的數(shù)據(jù)結(jié)構(gòu)非常松散,是類似 json 的 bjson 格式,因此可以存儲(chǔ)比較復(fù)雜的數(shù)據(jù)類型。MongoDB 最大的特點(diǎn)是他支持的查詢語言非常強(qiáng)大,其語法有點(diǎn)類似于面向?qū)ο蟮牟樵冋Z言,幾乎可以實(shí)現(xiàn)類似關(guān)系數(shù)據(jù)庫單表查詢的絕大部分功能,而且還支持對(duì)數(shù)據(jù)建立索引。
2、MongoDB特點(diǎn)
1)MongoDB 是由C++語言編寫的,是一個(gè)基于分布式文件存儲(chǔ)的開源數(shù)據(jù)庫系統(tǒng)。
2)在高負(fù)載的情況下,添加更多的節(jié)點(diǎn),可以保證服務(wù)器性能。
3)MongoDB 旨在為WEB應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲(chǔ)解決方案。
4)MongoDB 將數(shù)據(jù)存儲(chǔ)為一個(gè)文檔,數(shù)據(jù)結(jié)構(gòu)由鍵值(key=>value)對(duì)組成。MongoDB 文檔類似于 JSON 對(duì)象。字段值可以包含其他文檔,數(shù)組及文檔數(shù)組。
三、與SpringBoot2.0整合
1、MongoDB基礎(chǔ)環(huán)境
# 打開命令行 MongoDB4.0\bin>mongo # 展示所有數(shù)據(jù)庫 > show databases # 新建一個(gè)admin數(shù)據(jù)庫,命令比較難為情 > db.admin.insert({"name":"管理員數(shù)據(jù)庫"}); # 使用admin數(shù)據(jù)庫 > use admin # 創(chuàng)建root用戶,具有讀寫權(quán)限 > db.createUser({user:"root",pwd:"root",roles:[{role:"readWrite",db:"admin"}]})Successfully added user:2、核心依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>3、配置文件
用戶名:root
密碼:root
數(shù)據(jù)庫:admin
4、封裝應(yīng)用接口
public interface ImgInfoRepository {void saveImg(ImgInfo imgInfo) ;ImgInfo findByImgTitle(String imgTitle);long updateImgInfo(ImgInfo imgInfo) ;void deleteById(Integer imgId); }5、核心代碼塊
MongoDB的使用方式如下。
import com.boot.mongodb.entity.ImgInfo; import com.boot.mongodb.repository.ImgInfoRepository; import com.mongodb.client.result.UpdateResult; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class ImgInfoRepositoryImpl implements ImgInfoRepository {@Resourceprivate MongoTemplate mongoTemplate;@Overridepublic void saveImg(ImgInfo imgInfo) {mongoTemplate.save(imgInfo) ;}@Overridepublic ImgInfo findByImgTitle(String imgTitle) {Query query=new Query(Criteria.where("imgTitle").is(imgTitle));return mongoTemplate.findOne(query,ImgInfo.class);}@Overridepublic long updateImgInfo(ImgInfo imgInfo) {Query query = new Query(Criteria.where("imgId").is(imgInfo.getImgId()));Update update= new Update().set("imgTitle", imgInfo.getImgTitle()).set("imgUrl", imgInfo.getImgUrl());UpdateResult result = mongoTemplate.updateFirst(query,update,ImgInfo.class);return result.getMatchedCount();}@Overridepublic void deleteById(Integer imgId) {Query query = new Query(Criteria.where("imgId").is(imgId));mongoTemplate.remove(query,ImgInfo.class);} }6、測(cè)試代碼塊
import com.boot.mongodb.MongoDBApplication; import com.boot.mongodb.entity.ImgInfo; import com.boot.mongodb.repository.ImgInfoRepository; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; import java.util.Date; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = MongoDBApplication.class) public class MongoTest {@Resourceprivate ImgInfoRepository imgInfoRepository ;@Testpublic void test1 (){ImgInfo record = new ImgInfo() ;record.setImgId(1);record.setUploadUserId("A123");record.setImgTitle("博文圖片");record.setSystemType(1) ;record.setImgType(2);record.setImgUrl("https://avatars0.githubusercontent.com/u/50793885?s=460&v=4");record.setLinkUrl("https://avatars0.githubusercontent.com/u/50793885?s=460&v=4");record.setShowState(1);record.setCreateDate(new Date());record.setUpdateDate(record.getCreateDate());record.setRemark("知了");record.setbEnable("1");imgInfoRepository.saveImg(record);}@Testpublic void test2 (){ImgInfo imgInfo = imgInfoRepository.findByImgTitle("博文圖片") ;System.out.println("imgInfo === >> " + imgInfo);}@Testpublic void test3 (){ImgInfo record = new ImgInfo() ;record.setImgId(1);record.setUploadUserId("A123");record.setImgTitle("知了圖片");record.setSystemType(1) ;record.setImgType(2);record.setImgUrl("https://avatars0.githubusercontent.com/u/50793885?s=460&v=4");record.setLinkUrl("https://avatars0.githubusercontent.com/u/50793885?s=460&v=4");record.setShowState(1);record.setCreateDate(new Date());record.setUpdateDate(record.getCreateDate());record.setRemark("知了");record.setbEnable("1");long result = imgInfoRepository.updateImgInfo(record) ;System.out.println("result == >> " + result);}@Testpublic void test4 (){imgInfoRepository.deleteById(1);} }四、源代碼地址
GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 碼云地址:知了一笑 https://gitee.com/cicadasmile/spring-boot-base
總結(jié)
以上是生活随笔為你收集整理的SpringBoot2.0 基础案例(15):配置MongoDB数据库,实现增删改查逻辑的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 跟JBPM学设计模式之适配器模式
- 下一篇: Spring依赖注入方式