Spring Boot入门1

Spring Boot入门1

如何使用
  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>
  1. 创建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;
}
  1. 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);
}
  1. 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);
    }
}
  1. 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);
    }
}
  1. 配置文件application.yml
server:
  port: 9090
  1. 启动类
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测试业务。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值