SpringBoot基本使用

Spring Boot 是⼀个快速开发框架,可以迅速搭建出⼀套基于 Spring 框架体系的应⽤,是 Spring

Cloud 的基础。

Spring Boot 开启了各种⾃动装配,从⽽简化代码的开发,不需要编写各种配置⽂件,只需要引⼊相关

依赖就可以迅速搭建⼀个应⽤。

  • 特点

1、不需要 web.xml

2、不需要 springmvc.xml

3、不需要 tomcat,Spring Boot 内嵌了 tomcat

4、不需要配置 JSON 解析,⽀持 REST 架构

5、个性化配置⾮常简单

  • 如何使用

1、创建 Maven ⼯程,导⼊相关依赖。

<?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>org.example</groupId>
    <artifactId>SPringboot_01</artifactId>
    <version>1.0-SNAPSHOT</version>
     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>2.3.0.RELEASE</version>
     </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>

2、创建 Student 实体类

package com.yin.entity;


public class Student {
    private  long id;
    private  String name;
    private  int age;

    public Student() {
    }

    public Student(long id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public void put(long id, Student student) {
    }
}

3、StudentRepository

package com.yin.repository;

import com.yin.entity.Student;

import java.util.Collection;


public interface StudentRepositroy {

    public  Collection<Student> findAll() ;
    public Student findById(long id);
    public void saveOrUpdate(Student student);
    public void deleteById(long id);

}

4、StudentRepositoryImpl

package com.yin.repository;

import com.yin.entity.Student;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class StudentRepositoryImpl  implements StudentRepositroy {
    private  static Map<Long, Student> studentMap;
    static {
        studentMap=new HashMap<>();
        studentMap.put(1L,new Student(1L,"zhangsan",17));
        studentMap.put(2L,new Student(2L,"lisi",18));
        studentMap.put(3L,new Student(3L,"haha",20));
    }

    @Override
    public Collection<Student> findAll() {
        return studentMap.values();
    }
    @Override
    public Student findById(long id) {
        return studentMap.get(id);
    }

    @Override
    public void saveOrUpdate(Student student) {
        student.put(student.getId(),student);
    }

    @Override
    public void deleteById(long id) {
        studentMap.remove(id);

    }
}

5、StudentHandler

package com.yin.controller;

import com.yin.entity.Student;
import com.yin.repository.StudentRepositroy;
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  StudentRepositroy studentRepositroy;
    @GetMapping("/findAll")
    public Collection <Student> findAll(){
        return studentRepositroy.findAll();
    }
    @GetMapping("/findById/{id}")
    public  Student findById(@PathVariable("id") long id){
        return  studentRepositroy.findById(id);

    }
    @PostMapping("/save")
    public  void  save(@RequestBody Student student){
        studentRepositroy.saveOrUpdate(student);

    }
    @DeleteMapping("/deleteById/{id}")
    public  void  deleteById(@PathVariable("id") long id){
        studentRepositroy.deleteById(id);
    }
}

6、application.yml

server:
  port: 9090

​ 7、启动类

package com.yin;

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);
    }
}

@SpringBootApplication 表示当前类是 Spring Boot 的⼊⼝,Application 类的存放位置必须是其他

相关业务类的存放位置的⽗级。

最后修改:2021 年 05 月 10 日 09 : 07 PM
如果觉得我的文章对你有用,请随意赞赏