91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

用示例解析Java springboot Mongodb增刪改查

發布時間:2020-07-21 13:38:35 來源:億速云 閱讀:207 作者:小豬 欄目:開發技術

這篇文章主要用示例解析Java springboot Mongodb增刪改查,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。

1、添加依賴

復制代碼

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

完整pom.xm文件

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.7.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.vue</groupId>
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>demo</name>
  <description>Demo project for Spring Boot</description>

  <properties>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.49</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.26</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.16.20</version>
    </dependency>
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>2.2.0</version>
    </dependency>

    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.8.0</version>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.8.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-mongodb</artifactId>
      <version>2.1.6.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

2、applicaiton.yml

server:
 port: 8081

mybatis-plus:
   typeAliasesPackage: com.vue.demo.entity
   mapperLocations: classpath:mapper/*.xml
spring:
 datasource:
  url: jdbc:mysql://localhost:3306/test&#63;useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
  username: root
  password: yang156122
  driver-class-name: com.mysql.jdbc.Driver
  # 使用druid數據源
  type: com.alibaba.druid.pool.DruidDataSource
 redis:
  #redis單機配置
  host: localhost
  port: 6379
  # 選擇redis的數據庫的分庫
  database: 5
  #redis連接池配置
  jedis:
   pool:
    max-idle: 10
    min-idle: 5
    max-active: 100
    max-wait: 3000
    timeout: 6005
 data:
  mongodb:
   uri: mongodb://localhost:27017/userArticle

3、Mongodb增刪改查

package com.vue.demo.service.serviceimpl;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.vue.demo.entity.UserArticle;
import com.vue.demo.service.UserArticleService;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.List;

/**
 * @author yangwj
 * @date 2020/3/28 11:07
 */

@Service
public class UserArticleServiceImpl implements UserArticleService {
  @Autowired
  private MongoTemplate mongoTemplate;

  @Override
  public UserArticle getArticleByTitle(String title) {
    Query query = new Query();
    query.addCriteria(Criteria.where("articleTitle").is(title));
    UserArticle article = mongoTemplate.findOne(query, UserArticle.class);
    return article;
  }

  @Override
  public UserArticle insertArticle(UserArticle userArticle) {
    List<UserArticle> userArticleList = mongoTemplate.findAll(UserArticle.class);
    if (userArticleList.isEmpty()) {
      userArticle.setId(1);
    } else {
      userArticle.setId(userArticleList.size() + 1);
    }
    UserArticle article = mongoTemplate.save(userArticle);
    return article;
  }

  @Override
  public List<UserArticle> getAllArticles() {
    List<UserArticle> userArticles = mongoTemplate.findAll(UserArticle.class);
    return userArticles;
  }

  @Override
  public DeleteResult delByName(String name) {
    Query query = new Query();
    query.addCriteria(Criteria.where("articleTitle").is(name));
    DeleteResult result =mongoTemplate.remove(query,UserArticle.class);
    return result;
  }

  @Override
  public UpdateResult updateReadNumByTitle(String title) {
    Query query = new Query();
    query.addCriteria(Criteria.where("articleTitle").is(title));
    UserArticle article = mongoTemplate.findOne(query, UserArticle.class);
    Integer readNum = article.getReadNum() == null&#63; 1: article.getReadNum()+1;
    article.setReadNum(readNum);
    Update update = new Update();
    update.set("readNum",article.getReadNum());
    UpdateResult result = mongoTemplate.updateFirst(query,update,UserArticle.class);
    return result;
  }

  @Override
  public UpdateResult addUserComment(String title, String comment) {
    JSONObject jsonObject = new JSONObject();
    Query query = new Query();
    query.addCriteria(Criteria.where("articleTitle").is(title));
    UserArticle article = mongoTemplate.findOne(query, UserArticle.class);
    if(article.getComment() == null){
      jsonObject.put(title,comment);
    }else {
      jsonObject = (JSONObject) JSONObject.parse(article.getComment());
      jsonObject.put(title,comment);
    }
    Integer commentNum = article.getCommentNum() == null &#63; 1:article.getCommentNum()+1;
    Update update = new Update();
    update.set("comment",jsonObject.toJSONString());
    update.set("commentNum",commentNum);
    UpdateResult result = mongoTemplate.updateFirst(query,update,UserArticle.class);
    return result;
  }
}

看完上述內容,是不是對用示例解析Java springboot Mongodb增刪改查有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

凉山| 泽普县| 都兰县| 太仓市| 西宁市| 财经| 阳曲县| 甘洛县| 平乐县| 孟连| 墨脱县| 理塘县| 秭归县| 深水埗区| 嘉鱼县| 陆河县| 佳木斯市| 日土县| 长兴县| 炉霍县| 扬州市| 浪卡子县| 三门县| 韶关市| 女性| 鄂温| 库伦旗| 舞钢市| 万州区| 宜兰县| 嘉义市| 临城县| 西丰县| 浮梁县| 开鲁县| 高邑县| 八宿县| 太康县| 常山县| 佛教| 合山市|