SpringBoot2中如何配置MongoDB數(shù)據(jù)庫(kù)-創(chuàng)新互聯(lián)

這篇文章主要介紹“SpringBoot2中如何配置MongoDB數(shù)據(jù)庫(kù)”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“SpringBoot2中如何配置MongoDB數(shù)據(jù)庫(kù)”文章能幫助大家解決問(wèn)題。

創(chuàng)新互聯(lián)為您提適合企業(yè)的網(wǎng)站設(shè)計(jì)?讓您的網(wǎng)站在搜索引擎具有高度排名,讓您的網(wǎng)站具備超強(qiáng)的網(wǎng)絡(luò)競(jìng)爭(zhēng)力!結(jié)合企業(yè)自身,進(jìn)行網(wǎng)站設(shè)計(jì)及把握,最后結(jié)合企業(yè)文化和具體宗旨等,才能創(chuàng)作出一份性化解決方案。從網(wǎng)站策劃到成都網(wǎng)站建設(shè)、網(wǎng)站制作, 我們的網(wǎng)頁(yè)設(shè)計(jì)師為您提供的解決方案。

一、NoSQL簡(jiǎn)介

1、NoSQL 概念

NoSQL( Not Only SQL ),意即”不僅僅是SQL”。對(duì)不同于傳統(tǒng)的關(guān)系型數(shù)據(jù)庫(kù)的數(shù)據(jù)庫(kù)管理系統(tǒng)的統(tǒng)稱。NoSQL用于超大規(guī)模數(shù)據(jù)的存儲(chǔ)。這些類型的數(shù)據(jù)存儲(chǔ)不需要固定的模式,無(wú)需多余操作就可以橫向擴(kuò)展。

2、NoSQL的優(yōu)點(diǎn)/缺點(diǎn)

--優(yōu)點(diǎn):
高可擴(kuò)展性
分布式計(jì)算
低成本
架構(gòu)的靈活性,半結(jié)構(gòu)化數(shù)據(jù)
沒(méi)有復(fù)雜的關(guān)系
--缺點(diǎn):
沒(méi)有標(biāo)準(zhǔn)化
有限的查詢功能(到目前為止)
數(shù)據(jù)展現(xiàn)不直觀

二、MongoDB數(shù)據(jù)庫(kù)

1、MongoDB簡(jiǎn)介

MongoDB 是一個(gè)介于關(guān)系數(shù)據(jù)庫(kù)和非關(guān)系數(shù)據(jù)庫(kù)之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫(kù)當(dāng)中,功能最豐富,最像關(guān)系數(shù)據(jù)庫(kù)的。他支持的數(shù)據(jù)結(jié)構(gòu)非常松散,是類似 json 的 bjson 格式,因此可以存儲(chǔ)比較復(fù)雜的數(shù)據(jù)類型。MongoDB 較大的特點(diǎn)是他支持的查詢語(yǔ)言非常強(qiáng)大,其語(yǔ)法有點(diǎn)類似于面向?qū)ο蟮牟樵冋Z(yǔ)言,幾乎可以實(shí)現(xiàn)類似關(guān)系數(shù)據(jù)庫(kù)單表查詢的絕大部分功能,而且還支持對(duì)數(shù)據(jù)建立索引。

2、MongoDB特點(diǎn)

1)MongoDB 是由C++語(yǔ)言編寫的,是一個(gè)基于分布式文件存儲(chǔ)的開源數(shù)據(jù)庫(kù)系統(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整合

1、MongoDB基礎(chǔ)環(huán)境

# 打開命令行
MongoDB4.0\bin>mongo
# 展示所有數(shù)據(jù)庫(kù)
> show databases
# 新建一個(gè)admin數(shù)據(jù)庫(kù),命令比較難為情
> db.admin.insert({"name":"管理員數(shù)據(jù)庫(kù)"});
# 使用admin數(shù)據(jù)庫(kù)
> 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ù)庫(kù):admin

spring:
  data:
    mongodb:
      uri: mongodb://root:root@localhost:27017/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 {
    @Resource
    private MongoTemplate mongoTemplate;
    @Override
    public void saveImg(ImgInfo imgInfo) {
        mongoTemplate.save(imgInfo) ;
    }
    @Override
    public ImgInfo findByImgTitle(String imgTitle) {
        Query query=new Query(Criteria.where("imgTitle").is(imgTitle));
        return mongoTemplate.findOne(query,ImgInfo.class);
    }
    @Override
    public 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();
    }
    @Override
    public 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 {
    @Resource
    private ImgInfoRepository imgInfoRepository ;
    @Test
    public void test1 (){
        ImgInfo record = new ImgInfo() ;
        record.setImgId(1);
        record.setUploadUserId("A123");
        record.setImgTitle("博文圖片");
        record.setSystemType(1) ;
        record.setImgType(2);
        record.setImgUrl("/tupian/20230522/50793885.png
        record.setLinkUrl("/tupian/20230522/50793885.png
        record.setShowState(1);
        record.setCreateDate(new Date());
        record.setUpdateDate(record.getCreateDate());
        record.setRemark("知了");
        record.setbEnable("1");
        imgInfoRepository.saveImg(record);
    }
    @Test
    public void test2 (){
        ImgInfo imgInfo = imgInfoRepository.findByImgTitle("博文圖片") ;
        System.out.println("imgInfo === >> " + imgInfo);
    }
    @Test
    public void test3 (){
        ImgInfo record = new ImgInfo() ;
        record.setImgId(1);
        record.setUploadUserId("A123");
        record.setImgTitle("知了圖片");
        record.setSystemType(1) ;
        record.setImgType(2);
        record.setImgUrl("/tupian/20230522/50793885.png
        record.setLinkUrl("/tupian/20230522/50793885.png
        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);
    }
    @Test
    public void test4 (){
        imgInfoRepository.deleteById(1);
    }
}

關(guān)于“SpringBoot2中如何配置MongoDB數(shù)據(jù)庫(kù)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。

本文名稱:SpringBoot2中如何配置MongoDB數(shù)據(jù)庫(kù)-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://bm7419.com/article22/djhdjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)手機(jī)網(wǎng)站建設(shè)、網(wǎng)站收錄外貿(mào)建站、Google、企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)