欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot集成mongodb數(shù)據(jù)庫過程解析

 更新時間:2020年05月16日 10:28:06   作者:墨營  
這篇文章主要介紹了Spring Boot集成mongodb數(shù)據(jù)庫過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

一.認(rèn)識mongodb

MongoDB是一個介于關(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的bson格式,因此可以存儲比較復(fù)雜的數(shù)據(jù)類型。Mongo最大的特點(diǎn)是它支持的查詢語言非常強(qiáng)大,其語法有點(diǎn)類似于面向?qū)ο蟮牟樵冋Z言,幾乎可以實(shí)現(xiàn)類似關(guān)系數(shù)據(jù)庫單表查詢的絕大部分功能,而且還支持對數(shù)據(jù)建立索引。

二.Spring boot項(xiàng)目集成mongodb

1.添加mongodb依賴

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>

2.配置mongodb的連接

spring:
data:
mongodb:
#uri: mongodb://localhost:27017/data_exploration
uri: mongodb://root:dhcc-mongodb@192.168.100.87:27017/data_exploration?authSource=admin&authMechanism=SCRAM-SHA-1

解析:以上uri分別代表本地配置和遠(yuǎn)程連接

3.操作數(shù)據(jù)庫

(1)保存

@Repository
public class ExplorationJobDao {

  @Autowired
  MongoTemplate mongoTemplate;

  public void save(ExplorationJob explorationJob) {
    mongoTemplate.save(explorationJob);
  }
}

(2)根據(jù)ID修改一條數(shù)據(jù)(其原理先符合ID的數(shù)據(jù),然后刪除查詢結(jié)果的第一條)

public void updateExecutionStatusById(int executionStatus, String jobId) {
      Query query = new Query(Criteria.where("jobId").is(jobId));
      Update update = new Update().set("executionStatus", executionStatus);
      mongoTemplate.updateFirst(query, update, ExplorationJob.class);
  }

(3)根據(jù)條修改多條數(shù)據(jù)(查詢符合ID的所有數(shù)據(jù),然后將所有數(shù)據(jù)修改)

public void update(BusinessExploration businessExploration) {
    Query query = new Query(Criteria.where("_id").is(businessExploration.getId()));
    Update update = new Update().set("sourceUnit", businessExploration.getSourceUnit())
        .set("appSystem", businessExploration.getAppSystem())
        .set("businessImplication", businessExploration.getBusinessImplication())
        .set("safetyRequire", businessExploration.getSafetyRequire());
    mongoTemplate.updateMulti(query, update, TableExploration.class);
  }

(4)刪除(根據(jù)ID刪除)

public void delExplorationJobById(String jobId) {
     Query query=new Query(Criteria.where("jobId").is(jobId));
     mongoTemplate.remove(query,ExplorationJob.class);
  }

(5)根據(jù)條件查詢(根據(jù)ID查詢)

public ExplorationJob getExplorationJobByJobId(String jobId) {
    Query query = new Query(Criteria.where("jobId").is(jobId));
    ExplorationJob explorationJob = mongoTemplate.findOne(query, ExplorationJob.class);
    return explorationJob;
  }

(6)查詢所有

mongoTemplate.findAll(TableExploration.class);

(7)多條件動態(tài)查詢

public List<ExplorationJob> getExplorationByCondition(ExplorationJob explorationJob) {
    Query query = new Query();
    if (explorationJob.getJobName() != null) {
       Pattern pattern = Pattern.compile("^.*" + explorationJob.getJobName() + ".*$", Pattern.CASE_INSENSITIVE);
       query.addCriteria(Criteria.where("jobName").regex(pattern));
    }
    if (explorationJob.getDsType() != null) {
      query.addCriteria(Criteria.where("dsType").is(explorationJob.getDsType()));
    }
    if (explorationJob.getExecutionStatus() != null) {
       query.addCriteria(Criteria.where("executionStatus").lte(explorationJob.getExecutionStatus()));
    }
    List<ExplorationJob> explorationJobs=mongoTemplate.find(query, ExplorationJob.class);
    return explorationJobs;
  }

(8)查詢最大值

public Date getMaxExplorationDate(String tableName) {
    FindIterable<Document> iterable = mongoTemplate.getCollection("tableExploration")
        .find(new BasicDBObject("tableName", tableName)).sort(new BasicDBObject("explorationDate", -1)).skip(0)
        .limit(1);
    Document doc =null;
    if (getDocuments(iterable).size()>0) {
      doc=getDocuments(iterable).get(0);
      Date date = doc.getDate("explorationDate");
      return date;
    }else {
      return null;
    }
  }

  /**
   * 工具方法
   * 
   * @param iterable
   * @return
   */
  public static List<Document> getDocuments(FindIterable<Document> iterable) {
    List<Document> results = new ArrayList<Document>();
    if (null != iterable) {
      MongoCursor<Document> cursor = iterable.iterator();
      Document doc = null;
      while (cursor.hasNext()) {
        doc = cursor.next();
        results.add(doc);
      }
    }
    return results;
  }

(9)分組查詢(這里還是用到了排序)

public List<TableExploration> getAllTableExplorationGroupByTableName(String jobId){
    Aggregation aggregation = Aggregation.newAggregation(
        Aggregation.match(Criteria.where("jobId").is(jobId)),
        Aggregation.sort(new Sort(Direction.DESC,"explorationDate")),
        Aggregation.group("tableName")
        .first("_id").as("tableName")
        .first("databaseType").as("databaseType")
        .first("databaseName").as("databaseName")
        .first("networkSituation").as("networkSituation")
        .first("userName").as("userName")
        .first("password").as("password")
        .first("url").as("url")
        .first("dataStorage").as("dataStorage")
        .first("dataIncrement").as("dataIncrement")
        .first("explorationDate").as("explorationDate")
        //.push("columnExplorations").as("columnExplorations")
        .first("jobId").as("jobId")
        );
    AggregationResults<TableExploration> aggregationResults= mongoTemplate.aggregate(aggregation, "tableExploration", TableExploration.class);
    List<TableExploration> tableExplorations=aggregationResults.getMappedResults();
    return tableExplorations;

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java自定義線程池的原理簡介

    java自定義線程池的原理簡介

    這篇文章主要介紹了java自定義線程池的原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • Spring cloud Gateway簡介及相關(guān)配置方法

    Spring cloud Gateway簡介及相關(guān)配置方法

    這篇文章主要介紹了Spring cloud Gateway簡介及相關(guān)配置方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • Java連接數(shù)據(jù)庫oracle中文亂碼解決方案

    Java連接數(shù)據(jù)庫oracle中文亂碼解決方案

    這篇文章主要介紹了Java連接數(shù)據(jù)庫oracle中文亂碼解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • Java經(jīng)典排序算法之快速排序代碼實(shí)例

    Java經(jīng)典排序算法之快速排序代碼實(shí)例

    這篇文章主要介紹了Java經(jīng)典排序算法之快速排序代碼實(shí)例,快速排序?qū)崿F(xiàn)的思想是指通過一趟排序?qū)⒁判虻臄?shù)據(jù)分割成獨(dú)立的兩部分,其中一部分的所有數(shù)據(jù)都比另外一部分的所有數(shù)據(jù)都要小,然后再按此方法對這兩部分?jǐn)?shù)據(jù)分別進(jìn)行快速排序,需要的朋友可以參考下
    2023-10-10
  • Java字符串拼接效率測試過程解析

    Java字符串拼接效率測試過程解析

    這篇文章主要介紹了Java字符串拼接效率測試過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • JAVA中堆、棧,靜態(tài)方法和非靜態(tài)方法的速度問題

    JAVA中堆、棧,靜態(tài)方法和非靜態(tài)方法的速度問題

    這篇文章主要介紹了JAVA中堆、棧,靜態(tài)方法和非靜態(tài)方法的速度問題,堆和棧得速度性能分析多角度給大家分析,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-08-08
  • springboot實(shí)現(xiàn)rabbitmq消息確認(rèn)的示例代碼

    springboot實(shí)現(xiàn)rabbitmq消息確認(rèn)的示例代碼

    RabbitMQ的消息確認(rèn)有兩種, 一種是消息發(fā)送確認(rèn),第二種是消費(fèi)接收確認(rèn),本文主要介紹了springboot實(shí)現(xiàn)rabbitmq消息確認(rèn)的示例代碼,具有一定的參考價值,感興趣的可以了解一下
    2023-09-09
  • C++字符串的處理詳解

    C++字符串的處理詳解

    這篇文章主要介紹了C++ string字符串類,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • SpringMVC整合,出現(xiàn)注解沒有起作用的情況處理

    SpringMVC整合,出現(xiàn)注解沒有起作用的情況處理

    這篇文章主要介紹了SpringMVC整合,出現(xiàn)注解沒有起作用的情況及處理,具有很好的參考價值,希望對大家有所幫助。
    2023-05-05
  • 一次因HashSet引起的并發(fā)問題詳解

    一次因HashSet引起的并發(fā)問題詳解

    這篇文章主要給大家介紹了一次因HashSet引起的并發(fā)問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11

最新評論