ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Spring Boot学习之初识Spring Boot

2020-08-07 20:00:53  阅读:243  来源: 互联网

标签:student Spring studentMap Boot id 初识 Student import public


Spring Boot

Spring Boot 是一个快速开发框架,可以迅速搭建出一套基于Spring框架体系的应用,是Spring Cloud的基础,Spring Boot开启了各种自动装配,从而简化了代码的开发,不需要编写各种配置文件,只要引入相关依赖就可以迅速搭建一个应用。

  • 特点
  • 不需要web.xml
  • 不需要springmvc.xml
  • 不需要tomcat,Spring Boot内嵌了tomcat
  • 不需要配置JSON解析,支持REST架构
  • 个性化的配置非常简单
  • 如何使用
  • 创建maven工程,导入相关依赖

    1 <?xml version="1.0" encoding="UTF-8"?>

    2 <project xmlns="http://maven.apache.org/POM/4.0.0"

    3                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    4                   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    5         <modelVersion>4.0.0</modelVersion>

    6 

    7         <groupId>com.wiggin</groupId>

    8         <artifactId>aispringboot</artifactId>

    9         <version>1.0-SNAPSHOT</version>

   10 

   11         <!-- 继承父包 -->

   12         <parent>

   13                 <groupId>org.springframework.boot</groupId>

   14                 <artifactId>spring-boot-starter-parent</artifactId>

   15                 <version>2.0.7.RELEASE</version>

   16         </parent>

   17         <dependencies>

   18                 <!-- web启动的jar包 -->

   19                 <dependency>

   20                         <groupId>org.springframework.boot</groupId>

   21                         <artifactId>spring-boot-starter-web</artifactId>

   22                         <version>2.3.2.RELEASE</version>

   23                 </dependency>

   24                 <dependency>

   25                         <groupId>org.projectlombok</groupId>

   26                         <artifactId>lombok</artifactId>

   27                         <version>1.18.12</version>

   28                 </dependency>

   29 

   30         </dependencies>

   31 </project>

  • 创建Student实体类

    1 package com.wiggin.entity;

    2 

    3 import lombok.Data;

    4 

    5 @Data

    6 public class Student {

    7         private long id;

    8         private String name;

    9         private int age;

   10 }

  • 创建StudentRepository功能仓

    1 package com.wiggin.repository;

    2 

    3 import com.wiggin.entity.Student;

    4 

    5 import java.util.Collection;

    6 import java.util.List;

    7 

    8 public interface StudentRepository {

    9         public Collection<Student> findAll();

   10         public Student findById(long id);

   11         public void saveOrUpdate(Student student);

   12         public void deleteById(long id);

   13 }

  • 创建StudentRepositoryImpl功能仓实现类实体类

    1 package com.wiggin.repository.impl;

    2 

    3 import com.wiggin.entity.Student;

    4 import com.wiggin.repository.StudentRepository;

    5 import org.springframework.stereotype.Repository;

    6 

    7 import java.util.Collection;

    8 import java.util.HashMap;

    9 import java.util.List;

   10 import java.util.Map;

   11 

   12 @Repository

   13 public class StudentRepositoryImpl implements StudentRepository {

   14         private static Map<Long,Student> studentMap;

   15         static {

   16                 studentMap = new HashMap<>();

   17                 studentMap.put(1L,new Student(1L,"张三",22));

   18                 studentMap.put(2L,new Student(2L,"李四",23));

   19                 studentMap.put(3L,new Student(3L,"王五",24));

   20         }

   21         @Override

   22         public Collection<Student> findAll() {

   23                 return studentMap.values();

   24         }

   25 

   26         @Override

   27         public Student findById(long id) {

   28                 return studentMap.get(id);

   29         }

   30 

   31         @Override

   32         public void saveOrUpdate(Student student) {

   33                 studentMap.put(student.getId(),student);

   34         }

   35 

   36         @Override

   37         public void deleteById(long id) {

   38                 studentMap.remove(id);

   39         }

   40 }

  • 创建StudentRepositoryImpl功能仓实现类实体类

    1 package com.wiggin.repository.impl;

    2 

    3 import com.wiggin.entity.Student;

    4 import com.wiggin.repository.StudentRepository;

    5 import org.springframework.stereotype.Repository;

    6 

    7 import java.util.Collection;

    8 import java.util.HashMap;

    9 import java.util.List;

   10 import java.util.Map;

   11 

   12 @Repository

   13 public class StudentRepositoryImpl implements StudentRepository {

   14         private static Map<Long,Student> studentMap;

   15         static {

   16                 studentMap = new HashMap<>();

   17                 studentMap.put(1L,new Student(1L,"张三",22));

   18                 studentMap.put(2L,new Student(2L,"李四",23));

   19                 studentMap.put(3L,new Student(3L,"王五",24));

   20         }

   21         @Override

   22         public Collection<Student> findAll() {

   23                 return studentMap.values();

   24         }

   25 

   26         @Override

   27         public Student findById(long id) {

   28                 return studentMap.get(id);

   29         }

   30 

   31         @Override

   32         public void saveOrUpdate(Student student) {

   33                 studentMap.put(student.getId(),student);

   34         }

   35 

   36         @Override

   37         public void deleteById(long id) {

   38                 studentMap.remove(id);

   39         }

   40 }

  • 创建StudentHandler控制器

    1 package com.wiggin.controller;

    2 

    3 import com.wiggin.entity.Student;

    4 import com.wiggin.repository.StudentRepository;

    5 import org.springframework.beans.factory.annotation.Autowired;

    6 import org.springframework.web.bind.annotation.*;

    7 

    8 import java.util.Collection;

    9 

   10 @RestController

   11 @RequestMapping("/student")

   12 public class StudentHandler {

   13         @Autowired

   14         private StudentRepository studentRepository;

   15 

   16         @GetMapping("/findAll")

   17         public Collection<Student> findAll(){

   18                 return studentRepository.findAll();

   19         }

   20         @GetMapping("/findById/{id}")

   21         public Student findById(@PathVariable long id){

   22                 return studentRepository.findById(id);

   23         }

   24         @PostMapping("/save")

   25         public void save(@RequestBody Student student){

   26             studentRepository.saveOrUpdate(student);

   27         }

   28         @PostMapping("/update")

   29         public void update(@RequestBody Student student){

   30                 studentRepository.saveOrUpdate(student);

   31         }

   32         @DeleteMapping("/delete/{id}")

   33         public void deleteById(@PathVariable long id){

   34                 studentRepository.deleteById(id);

   35         }

   36 }

  • 创建Application启动类

    1 package com.wiggin;

    2 

    3 import org.springframework.boot.SpringApplication;

    4 import org.springframework.boot.autoconfigure.SpringBootApplication;

    5 

    6 // Application文件要在所有类的上级

    7 @SpringBootApplication

    8 public class Application {

    9         public static void main(String[] args) {

   10                 SpringApplication.run(Application.class,args);

   11         }

   12 }

注意:启动类一定要放在所有功能类文件的上级,这样才能被扫描。

  • 创建application.yml更改接口地址

    1 server:

    2     port: 9090

注意:层与层之间要有缩进的不同,以反映层级关系。

    1         private static Map<Long,Student> studentMap;

    2         static {

    3                 studentMap = new HashMap<>();

    4                 studentMap.put(1L,new Student(1L,"张三",22));

    5                 studentMap.put(2L,new Student(2L,"李四",23));

    6                 studentMap.put(3L,new Student(3L,"王五",24));

    7         }

    8         @Override

    9         public Collection<Student> findAll() {

   10                 return studentMap.values();

   11         }

   12 

   13         @Override

   14         public Student findById(long id) {

   15                 return studentMap.get(id);

   16         }

   17 

   18         @Override

   19         public void saveOrUpdate(Student student) {

   20                 studentMap.put(student.getId(),student);

   21         }

   22 

   23         @Override

   24         public void deleteById(long id) {

   25                 studentMap.remove(id);

   26         }

   

标签:student,Spring,studentMap,Boot,id,初识,Student,import,public
来源: https://www.cnblogs.com/wigginess/p/13454933.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有