从b站学习springcloud,现在进行总结,该总结除去了视频中出现的小错误,对有些易错的地方进行了提醒
b站链接:https://www.bilibili.com/video/av55993157
资料链接:
https://pan.baidu.com/s/1o0Aju3IydKA15Vo1pP4z5w
提取码: 21ru
上一节链接:https://blog.csdn.net/qq_40893824/article/details/106867958
下一节链接:https://blog.csdn.net/qq_40893824/article/details/106872203
下面的内容总结:
mongodb 数据库:分布式文件存储 的数据库,介于 关系 和 非关系 之间
类似上1节,新建工程 springmongodb
1 在 pom 文件中,加入代码:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.southwind</groupId>
<artifactId>springmongodb</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<dependencies>
<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>
</dependency>
</dependencies>
</project>
![](https://i-blog.csdnimg.cn/blog_migrate/7129f8a901b093d2c5a048d246725c50.png)
2 在 java 中创包 com.southwind.entity,其内新建实体类 Student,加入代码:
package com.southwind.entity;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
@Data
@Document(collection = "my_student")
public class Student {
@Id
private String id;
@Field(value = "student_age")
private Integer age;
@Field(value = "student_name")
private String name;
}
![](https://i-blog.csdnimg.cn/blog_migrate/e3ca10bd1eeee605c1ac316a439d3d07.png)
3 在 southwind 中创包 repository,其内创建接口 StudentRepository,加入代码:
package com.southwind.repository;
import com.southwind.entity.Student;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRepository extends MongoRepository<Student, String> {
public Student getById(String id);
}
![](https://i-blog.csdnimg.cn/blog_migrate/a12b056e3f7635a2585e572a6e3989a9.png)
4 在 southwind 中 创包 controller,其内创建控制类 StudentHandler,加入代码:
package com.southwind.controller;
import com.southwind.entity.Student;
import com.southwind.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class StudentHandler {
@Autowired
private StudentRepository studentRepository;
@PostMapping("/save")
public Student save(@RequestBody Student student){
return studentRepository.save(student);
}
@PutMapping("/update")
public Student update(@RequestBody Student student){
return studentRepository.save(student);
}
@DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable("id")String id){
studentRepository.deleteById(id);
}
@GetMapping("/findAll")
public List<Student> findAll(){
return studentRepository.findAll();
}
@GetMapping("/findById/{id}")
public Student findById(@PathVariable("id")String id){
return studentRepository.getById(id);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/7164c83d86bd0bc816a3381f7deb6e70.png)
5 resources中创建 application.yml
spring:
data:
mongodb:
database: my_test
host: 127.0.0.1
port: 12345
![](https://i-blog.csdnimg.cn/blog_migrate/f6b0c74855dc8afa01b43773729501fb.png)
6 在 southwind 中新建启动类 Application,加入代码:
package com.southwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/904cc4292784359566feaf93c19ba0de.png)
因为没有本地的 mongodb 数据库,所以就不测试了
上一节链接:https://blog.csdn.net/qq_40893824/article/details/106867958
下一节链接:https://blog.csdn.net/qq_40893824/article/details/106872203