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库中生成了对应的集合
原文地址:https://www.cnblogs.com/new-life/p/11111792.html
时间: 2024-10-09 21:04:51