SpringBoot整合Mongodb进行CRUD

简介

功能

  1. 添加mongodb依赖。
  2. 使用springboot中的mongodb库操作mongodb进行增删改查操作。

创建项目

创建项目springboot_mongo

  • 开发工具:idea

  • Create New Project -> Spring Initializr ->next-> 添加项目信息

添加依赖

再pom.xml中添加

       <!--mongodb使用-->
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        
        <!--Web必要的-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

entity层

新建entity目录,在这个目录下新建实体类Alg

package indi.xzw.springboot_mongo.entity;

import lombok.Data;

@Data
public class Alg {
    private String uid;
    private Integer heartRate;
    private double[] aus;
}

service层

AlgService

新建service目录在这个目录下新建AlgService 接口

package indi.xzw.springboot_mongo.service;

import com.mongodb.client.result.UpdateResult;
import indi.xzw.springboot_mongo.entity.Alg;



public interface AlgService {
    void addAlg(Alg alg);
    void deleteAlg(String uuid);
    UpdateResult updateAlg(String uuid, int heartRate);
    void findAlg(String uuid);
}

AlgServiceImpl

在service目录下新建impl目录,并新建AlgServiceImpl类实现接口AlgService

package indi.xzw.springboot_mongo.service.impl;

import com.mongodb.client.result.UpdateResult;
import indi.xzw.springboot_mongo.entity.Alg;
import indi.xzw.springboot_mongo.service.AlgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
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;

@Service
public class AlgServiceImpl implements AlgService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void addAlg(Alg alg){
        //将alg数据存储到alg_data集合中
        mongoTemplate.save(alg, "alg_data");
    }

    @Override
    public void deleteAlg(String uuid){
        Criteria criteria = Criteria.where("uid").is(uuid);
        Query query = new Query().addCriteria(criteria);
        mongoTemplate.remove(query, "alg_data");
    }

    @Override
    public UpdateResult updateAlg(String uuid, int heartRate){
        Criteria criteria = Criteria.where("uid").is(uuid);
        Query query = new Query().addCriteria(criteria);
        Update update= new Update().set("heartRate", heartRate);
        //更新查询返回结果集的第一条
        UpdateResult ret = mongoTemplate.updateFirst(query,update,"alg_data");
        return ret;
    }

    @Override
    public void findAlg(String uuid){
        Criteria criteria = Criteria.where("uid").is(uuid).and("heartRate").lte(80);
        Query querys = new Query().addCriteria(criteria).with(Sort.by(Sort.Direction.DESC, "heartRate")).limit(1);
        List<Alg> algs = this.mongoTemplate.find(querys, Alg.class, "alg_data");
        System.out.print(algs);
    }
}

controller层

新建conntroller目录,在这个目录下新建控制类MongoController

package indi.xzw.springboot_mongo.controller;

import indi.xzw.springboot_mongo.entity.Alg;
import indi.xzw.springboot_mongo.service.AlgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MongoController {
    @Autowired
    private AlgService algService;

    @GetMapping(value="{param}")
    public void process(@PathVariable("param") String param){
        if(param.equals("add") ){
            Alg alg1 = new Alg();
            Alg alg2= new Alg();
            Alg alg3 = new Alg();
            double[] aus={1.1,1.2,4.3,6.7};
            double[] aus1={1.2,6.2,4.3,999.7,9.999};
            alg1.setAus(aus);
            alg1.setHeartRate(72);
            alg1.setUid("1");
            alg2.setAus(aus1);
            alg2.setHeartRate(40);
            alg2.setUid("2");
            alg3.setAus(aus);
            alg3.setHeartRate(80);
            alg3.setUid("3");
            //新增三组数据
            algService.addAlg(alg1);
            algService.addAlg(alg2);
            algService.addAlg(alg3);
            
            
        }else if(param.equals("del")){
            //删除uid为3的所有数据
            algService.deleteAlg("3");
            
            
        }else if(param .equals("update")){
            //将第一个uid为2的heartRate值改为99
            algService.updateAlg("2",99);
            
            
        }else if(param.equals("find")){
            //查找uid为1的数据
            algService.findAlg("1");
        }
    }
}

修改配置项

server.port=8080
spring.data.mongodb.database=test
spring.data.mongodb.host=192.168.16.166
spring.data.mongodb.port=27017

测试

在mongodb中新建一个test数据库

130

_id 可以自己设置,如果不想要数据重复,可以自己设置_id

131

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×