Java springboot Mongodb增删改查代码实例

网友投稿 450 2022-11-30


Java springboot Mongodb增删改查代码实例

1、添加依赖

复制代码

org.springframework.boot

spring-boot-starter-data-mongodb

2.1.6.RELEASE

完整pom.xm文件

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.7.RELEASE

com.vue

demo

0.0.1-SNAPSHOT

demo

Demo project for Spring Boot

1.8

org.springframework.boot

spring-boot-starter-web

mysql

mysql-connector-java

runtime

com.alibaba

fastjson

1.2.49

com.alibaba

druid

1.0.26

org.projectlombok

lombok

1.16.20

com.baomidou

mybatis-plus-boot-starter

2.2.0

io.springfox

springfox-swagger2

2.8.0

io.springfox

springfox-swagger-ui

2.8.0

org.springframework.boot

spring-boot-starter-data-mongodb

2.1.6.RELEASE

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.7.RELEASE

com.vue

demo

0.0.1-SNAPSHOT

demo

Demo project for Spring Boot

1.8

org.springframework.boot

spring-boot-starter-web

mysql

mysql-connector-java

runtime

com.alibaba

fastjson

1.2.49

com.alibaba

druid

1.0.26

org.projectlombok

lombok

1.16.20

com.baomidou

mybatis-plus-boot-starter

2.2.0

io.springfox

springfox-swagger2

2.8.0

io.springfox

springfox-swagger-ui

2.8.0

org.springframework.boot

spring-boot-starter-data-mongodb

2.1.6.RELEASE

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.springframework.boot

spring-boot-maven-plugin

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?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

http:// 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 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 getAllArticles() {

List 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? 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 ? 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;

}

}

github地址:https://github.com/812406210/vue-demo.git


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java中正则表达式split()特殊符号使用详解
下一篇:解决IDEA2020控制台乱码的方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~