Spring Boot入门1
如何使用
- 创建Maven工程,在pom.xml文件中导入相关依赖
<!-- 继承父包 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
</parent>
<dependencies>
<!-- web启动jar包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 使用lombok插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
- 创建Student实体类,需要下载lombok插件后才能使用下列注释
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
//@Data:不用再去手写Getter,Setter,equals,hasCode,toString等方法了,注解后在编译时会自动加进去
//@AllArgsConstructor:添加一个构造函数,该构造函数含有所有已声明字段属性参数
public class Student {
private long id;
private String name;
private int age;
}
- StudentRepository【相当于Service层】
import com.southwind.entity.Student;
import java.util.Collection;
public interface StudentRepository {
public Collection<Student> finAll();
public Student findById(long id);
public void saveOrUpdate(Student student);
public void deleteById(long id);
}
- StudentRepositoryImpl【相当于Service的实现层】
import com.southwind.entity.Student;
import com.southwind.repository.StudentRepository;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class StudentRepositoryImpl implements StudentRepository {
//使用map代替数据库
private static Map<Long, Student> studentMap;
//静态代码块实例化map
static {
studentMap = new HashMap<>();
studentMap.put(1L, new Student(1L, "张三", 23));
studentMap.put(2L, new Student(2L, "李四", 24));
studentMap.put(3L, new Student(3L, "王五", 25));
}
@Override
public Collection<Student> finAll() {
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【相当于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.Collection;
@RestController
@RequestMapping("/student")
public class StudentHandler {
@Autowired
private StudentRepository studentRepository;
@GetMapping("/findAll")
public Collection<Student> findAll(){
return studentRepository.finAll();
}
@GetMapping("/findById/{id}")
public Student findById(@PathVariable("id") long id){
return studentRepository.findById(id);
}
@PostMapping("/save")
public void save(@RequestBody Student student){
studentRepository.saveOrUpdate(student);
}
@PutMapping("/update")
public void update(@RequestBody Student student){
studentRepository.saveOrUpdate(student);
}
@DeleteMapping("/delete/{id}")
public void delete(@PathVariable long id){
studentRepository.deleteById(id);
}
}
- 配置文件application.yml
server:
port: 9090
- 启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//@SpringBootApplication:表示当前类是Spring Boot的入口
//启动类的存放位置必须是其他相关业务类的存放位置的父级
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
运行结果
使用postman
测试业务。