• Spring Boot学习之初识Spring Boot


    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.AllArgsConstructor;

        4 import lombok.Data;

        5 import lombok.NoArgsConstructor;

        6 

        7 @Data

        8 @AllArgsConstructor

        9 @NoArgsConstructor

       10 public class Student {

       11         private long id;

       12         private String name;

       13         private int age;

       14 }

    • 创建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 }

    • 创建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

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

        

  • 相关阅读:
    如何编译Linux内核
    svn
    windows live writer …
    SVN服务器配置
    使用PowerDesigner创建数据库表图文并茂版
    maven学习
    在PreparedStatement中设置空值
    今天, 我的博客正式开通啦.
    Neo4j简介
    clinit和init(转载)
  • 原文地址:https://www.cnblogs.com/wigginess/p/13454973.html
Copyright © 2020-2023  润新知