标签:col let path 小伙伴 find 需要 必须 oca dep
<dependencies>
<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>
<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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- mongodb驱动 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<!-- mongodb驱动结束 -->
</dependencies>
package com.example.mongodb.entity;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;
@Data
@Document("user")
public class User {
private String id;
private String name;
private Integer age;
}
注:@Document注解是mongodb带的,用于标注该类对应的哪个集合
package com.example.mongodb;
import com.example.mongodb.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@SpringBootTest
@RunWith(SpringRunner.class)
public class UserTest {
@Resource
private MongoTemplate mongoTemplate;
@Test
public void getAll(){
mongoTemplate.findAll(User.class).forEach(System.out::println);
}
}
注:自动注入类MongoTemplate也是由spring-boot-starter-data-mongodb管理,提供对应集合的增删改查操作。使用时需要指定集合对应类是哪一个。
package com.example.mongodb.controller;
import com.example.mongodb.entity.User;
import com.mongodb.client.result.DeleteResult;
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.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/rest")
public class UserController {
@Resource
private MongoTemplate mongoTemplate;
@GetMapping("/user")
public List<User> getAll() {
return mongoTemplate.findAll(User.class, "user");
}
@PostMapping("/user")
public User add(@RequestBody User user) {
return mongoTemplate.insert(user, "user");
}
@DeleteMapping("/user/{id}")
public DeleteResult delete(@PathVariable String id) {
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(id));
return mongoTemplate.remove(query, User.class, "user");
}
}
注:Query类实际上就是mongodb的查询类,此处作用是当作过滤条件,先查出,再删除。当然也可当作查询直接使用,例如:
@GetMapping("/user/{id}")
public User get(@PathVariable String id){
Query query=new Query(Criteria.where("_id").is(id));
return mongoTemplate.findOne(query,User.class);
}
spring:
data:
mongodb:
# mongodb://username:password@serverIp:port/dbname
uri: "mongodb://test_admin:welcome@localhost:27017/test"
这里笔者使用idea-build编译不过,必须使用mvn idea:idea命令进行编译才能通过,原因暂不明了,如果有知道的小伙伴欢迎再下面留言。
在控制台进入到mongodb的模块目录,直接输入mvn idea:idea命令即可用maven编译。
我的spring-boot-study之mongodb的应用
标签:col let path 小伙伴 find 需要 必须 oca dep
原文地址:https://www.cnblogs.com/zhoushiya/p/12868312.html