springboot连接mongodb进行CRUD

Posted new-life

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot连接mongodb进行CRUD相关的知识,希望对你有一定的参考价值。

springboot连接mongodb进行CRUD的过程:

在执行以下操作前已安装了mongodb并创建了用户和数据库,使用Robo 3T可成功连接。

1.创建springboot项目,加入以下maven依赖

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

2.在application.properties中加入mongo连接配置

spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.port=27017
spring.data.mongodb.username=admin
spring.data.mongodb.password=password
spring.data.mongodb.database=DBTest
spring.data.mongodb.authentication-database=admin

3.创建实体类

package com.example.mongodb.entity;

import org.springframework.data.mongodb.core.mapping.Document;

// @Document的collection属性设置的是在mongo库中的集合名称
@Document(collection = "passenger")
public class Passenger 

    private String name;
    private String password;

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name;
    

    public String getPassword() 
        return password;
    

    public void setPassword(String password) 
        this.password = password;
    

4.创建controller

package com.example.mongodb.controller;

import com.example.mongodb.entity.Driver;
import com.example.mongodb.entity.Passenger;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
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.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TestContoller 

    @Autowired
    MongoTemplate mongoTemplate;

    @RequestMapping("/insert")
    public String insert() 
        Passenger passenger = new Passenger();
        passenger.setName("hello");
        passenger.setPassword("world1");
        passenger = mongoTemplate.insert(passenger);
        if (passenger != null) 
            return "success";
         else 
            return "false";
        
    

    @RequestMapping("/query")
    public String query() 
        Query query = Query.query(Criteria.where("name").is("hello").and("password").is("world1"));
        List<Passenger> passengers = mongoTemplate.find(query, Passenger.class);
        return passengers.size() + "";
    


    @RequestMapping("/update")
    public String update() 
        Query query = Query.query(Criteria.where("password").is("world1"));
        Update update = new Update();
        update.set("name", "world2");
        update.set("password", "world2");
        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, Passenger.class);
        return "success";
    

    @RequestMapping("/remove")
    public String remove() 
        Query query = Query.query(Criteria.where("password").is("world1"));
        DeleteResult remove = mongoTemplate.remove(query, Passenger.class);
        return "success";
    

发送CRUD请求后在mongo库中生成了对应的集合

技术图片

 

以上是关于springboot连接mongodb进行CRUD的主要内容,如果未能解决你的问题,请参考以下文章

springboot mongodb crud update 只改变了字段

SpringBoot WebFlux整合MongoDB实现CRUD及分页功能

MongoDB简介与应用场景Docker安装Mongo整合SpringBoot实现CRUD

MongoDB数据库Java MongoDB CRUD Example

连接数据库使用Model对象对数据库进行操作(CRUD)

springboot 以main方式对mybaits实现crud操作