1 Spring Boot
spring Boot 是一个快速开发框架,可以迅速搭建出一套基于Spring框架体系的应用,是Spring Cloud的基础。
Spring Boot 开启了各种自动装配,从而简化代码开发,不需要编写各种配置文件,只需要引入相关依赖就可以迅速搭建一个应用。
- 特点
- 不需要 web.xml
- 不需要 springmvc.xml
- 不需要 tomcat、SpringBoot内嵌tomcat
- 不需要配置 JSON 解析,支持 REST 架构
- 个性化配置非常简单
2 如何使用
- 创建 Maven 工程,导入相关依赖
<!-- 基础父包 -->
<parent>
<groupId>org.springframework.boot</groupId>
<version>2.7.5</version>
<artifactId>spring-boot-starter-parent</artifactId>
</parent>
<dependencies>
<!-- Web启动jar -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
- 创建实体类
package com.southwind.entiry;
import lombok.Data;
@Data
public class Student {
private long id;
private String name;
private int age;
}
- StudentRepository
package com.southwind.repository;
import com.southwind.entiry.Student;
import java.util.Collection;
public interface StudentRepository {
public Collection<Student> findAll();
public Student findById(long id);
public void saveOrUpdate(Student student);
public void deleteById(long id);
}
package com.southwind.repository;
import com.southwind.entiry.Student;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class StudentRepositoryImpl implements StudentRepository{
private static Map<Long,Student> studentMap;
static{
studentMap = new HashMap<>();
studentMap.put(1L,new Student(1L,"张三",22));
studentMap.put(2L,new Student(2L,"李四",23));
}
@Override
public Collection<Student> findAll() {
return studentMap.values();
}
@Override
public Student findById(long id) {
return studentMap.get(id);
}
@Override
public void saveOrUpdate(Student student) {
studentMap.put(student.getId(),student);
}
@Override
public void deleteById(long id) {
studentMap.remove(id);
}
}
- StudentHandler
package com.southwind.controller;
import com.southwind.entiry.Student;
import com.southwind.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
@RestController // 返数据
@RequestMapping("/student")
public class StudentHandler {
@Autowired
private StudentRepository studentRepository;
@GetMapping("/findAll")
public Collection<Student> findAll(){
return studentRepository.findAll();
}
@GetMapping("/findById/{id}")
public Student findById(@PathVariable("id") Long id){
return studentRepository.findById(id);
}
@PostMapping("/save") // 因为前端会以JSON数据返回,所以要用 @RequestBody
public void save(@RequestBody Student student){
studentRepository.saveOrUpdate(student);
}
@PutMapping("/update")
public void update(@RequestBody Student student){
studentRepository.saveOrUpdate(student);
}
@DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable("id") Long id){
studentRepository.deleteById(id);
}
}
- application.yml
进行端口号的修改:
server:
port: 9090
- 启动类
package com.southwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
// run(类名.class,arg)
SpringApplication.run(Application.class,args);
}
}
// 名字不固定,可以自取
@SpringBootApplication
表示当前类是 Spring Boot 的入口,Application 类的存放位置必须是其他相关业务类的存放位置的父级。
其他注解可参考:https://blog.csdn.net/qq_52077925/article/details/126732070
https://blog.csdn.net/qq_52077925/article/details/126775590
- 运行