<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
spring:
data:
mongodb:
database: rayc
host: 127.0.0.1
port: 27017
server:
port: 8087
entity
@Data
//表名映射
@Document(collection = "rayc")
public class Student {
@Id
private String id;
//字段映射
// @Field(value = "student_age")
// private Integer age;
@Field(value = "name")
private String name;
}
repository
@Repository
public interface StudentRepository extends MongoRepository<Student,String> {
}
controller
@RestController
public class StudentHandler {
@Autowired
private StudentRepository studentRepository;
@GetMapping("/findAll")
public List<Student> findAll(){
return studentRepository.findAll();
}
@GetMapping("/findById/{id}")
public Student findById(@PathVariable("id") String id){
return studentRepository.findById(id).get();
}
@PostMapping("/save")
public Student save(@RequestBody Student student){
return studentRepository.save(student);
}
@PostMapping("/update")
public Student update(@RequestBody Student student){
return studentRepository.save(student);
}
@DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable("id") String id){
studentRepository.deleteById(id);
}
}
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
安装mongdb
新建文件夹
存放
mongo.bat启动文件
内容: mongod.exe --dbpath=D:\environment\mongoDB\mongoDBdata
mongoDBClient.bat客户端文件
内容:mongo.exe 127.0.0.1:27017/admin
启动mongo.bat 然后启动mongoDBClient.bat操作
查看数据库 show dbs
use rayc
db.rayc.find()
BSON格式数据
postman测试
save
delete
update
findAll