• 1 Spring Boot快速入门


    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.bootgroupId>
        <version>2.7.5version>
        <artifactId>spring-boot-starter-parentartifactId>
    parent>
    
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 创建实体类
    package com.southwind.entiry;
    
    import lombok.Data;
    
    @Data
    public class Student {
        private long id;
        private String name;
        private int age;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 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);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • application.yml

    进行端口号的修改:

    server:
      port: 9090
    
    • 1
    • 2
    • 启动类
    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);
        }
    }
    // 名字不固定,可以自取
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    @SpringBootApplication 
    表示当前类是 Spring Boot 的入口,Application 类的存放位置必须是其他相关业务类的存放位置的父级。
    
    • 1
    • 2

    其他注解可参考:https://blog.csdn.net/qq_52077925/article/details/126732070
    https://blog.csdn.net/qq_52077925/article/details/126775590

    • 运行
      在这里插入图片描述在这里插入图片描述
      在这里插入图片描述
      在这里插入图片描述
  • 相关阅读:
    Java扫描区块链的工具包|Java扫块|监听token转账
    每日一个设计模式之【工厂模式】
    iPhone苹果手机来电收到消息闪光灯闪烁通知提醒功能怎么开启?
    Qt程序打包成安装包exe
    (memcpy,memmove...)内存函数还不会??别怕,我来助你一臂之力
    [RK-Linux] recovery分区详解(一)
    了解稀疏数组
    项目后端环境和前端环境的搭建
    图像相似度识别算法aHash|dHash|PHash
    NAT+ACL+mstp小综合
  • 原文地址:https://blog.csdn.net/qq_52077925/article/details/127668914