Springboot搭建简单demo

简介: Springboot搭建简单demo

**Spring boot是用来简化Spring的,是对Spring框架的一个再封装

(所以需要有spring和maven等基础)**

**解决:**

“spring全家桶”时代

Spring boot->J2EE一站式解决方案

Spring Cloud->分布式整体解决方案

**微服务

2014,Martin flowler**

微服务:架构风格(服务微化)

一个应用应该是一组小型服务,可以通过HTTP互联互通。


每一个单元元素最终都是可独立替换和独立升级的软件功能单元


**特点**

不需要web.xml

不需要springmvc.xml

不需要tomcat(内嵌了)

不需要配置JSON解析,支持rest架构

个性化配置非常简单

**demo最终目录结构(代码下面给出了)**

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200528165843983.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0OTY5NjQz,size_16,color_FFFFFF,t_70)

**由于Spring boot使用了大量注解,所以很简单。最后,只需要浏览器:localhost:8080/student/方法  就OK了!**

**使用步骤**

**1,创建maven工程(先不整合其他组件),导入相关依赖**


```java

<!--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>

   <dependency>

       <groupId>org.projectlombok</groupId>

       <artifactId>lombok</artifactId>

       <version>1.18.6</version>

       <scope>provided</scope>

   </dependency>

</dependencies>

```


**2,创建Student实体类**

**package com.shuang.entity;

import lombok.Data;

@Data

public class Student {

   private long id;

   private String name;

   private int age;

}**


**3,StudentRepository**


```java

package com.shuang.repository;


import com.shuang.entity.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);


}

```


**4,StudentRespository**


```java

package com.shuang.repository.impl;


import com.shuang.entity.Student;

import com.shuang.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 {


  private static Map<Long,Student> studentMap;


  static{

      studentMap=new HashMap<>();

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

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

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

      studentMap.put(4L,new Student(4L,"赵六",25));

  }



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

   }

}

```


**5,studentHandler**


```java

package com.shuang.controller;


import com.shuang.entity.Student;

import com.shuang.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")

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

   }



}

```


**6,application.yml**


```java

server:

 port: 9090

```


**7,启动类(此为纯java项目,但由于springboot本身内嵌了tomcat服务器,故本身就可以做web项目)**


```java

package com.shuang;


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类存放位置必须是其他相关业务类的存放位置的父级(向下扫描)。**


**ok,基础掌握了**


推荐1(带源码):[Springboot 整合Thymeleaf](https://blog.csdn.net/qq_44969643/article/details/106441931)


推荐2(带源码):[springboot整合jsp快速搭建增删改查的学生管理系统](https://blog.csdn.net/qq_44969643/article/details/106423406)



目录
相关文章
|
Android开发
Autox.js 脚本开发环境搭建,从案例到打包apk(详细流程)
Autox.js 脚本开发环境搭建,从案例到打包apk(详细流程)
3493 0
|
2月前
|
安全 Java 数据库连接
SpringBoot使用小汇总
Spring Boot基于Spring框架,通过“约定优于配置”和丰富Starter依赖,简化企业级Java应用开发。具备零配置、内嵌服务器、自动依赖管理及生产级特性,适用于微服务与单体架构。本文从核心特性、开发实践、性能优化与生态扩展四方面深入解析。
174 2
|
机器学习/深度学习 人工智能 监控
开箱即用|基于YOLOv8的农作视觉AI——农民与农用车检测系统实战
本项目以YOLOv8为核心,结合PyQt5可视化界面,完整实现了“劳动人民”与“农用汽车”在农作场景中的智能识别功能。无论是图片、视频还是实时摄像头输入,系统都能实现高效、稳定的识别与展示,具备良好的实用性与拓展性。
|
5月前
|
IDE Java 开发工具
说一说 SpringBoot 中 CommandLineRunner
我是小假 期待与你的下一次相遇 ~
306 42
说一说 SpringBoot 中 CommandLineRunner
|
5月前
|
安全 Java 数据库
Spring Boot 框架深入学习示例教程详解
本教程深入讲解Spring Boot框架,先介绍其基础概念与优势,如自动配置、独立运行等。通过搭建项目、配置数据库等步骤展示技术方案,并结合RESTful API开发实例帮助学习。内容涵盖环境搭建、核心组件应用(Spring MVC、Spring Data JPA、Spring Security)及示例项目——在线书店系统,助你掌握Spring Boot开发全流程。代码资源可从[链接](https://pan.quark.cn/s/14fcf913bae6)获取。
591 2
|
API 开发工具 Android开发
简述大疆无人机对接
【2月更文挑战第7天】本文介绍了对接大疆无人机的主要目的,包括实时画面获取、飞行数据监测、操控飞行、媒体管理和业务功能开发等,并列举了多种开发接口如MobileSDK、UXSDK、云开发API等。重点讨论了MobileSDK在Android平台的应用,包括SDK集成步骤、直播推流和获取飞机实时数据的细节。另外,UXSDK用于加速应用开发,提供预设UI组件。上云API则简化了无人机与第三方云平台的集成,支持MQTT、HTTPS和WebSocket协议,适用于行业级无人机。对接流程涉及Pilot2和Dock的配置,以及数据传输和业务功能处理。文章还提及了如何对接多个飞机的方法。
10035 0
简述大疆无人机对接
|
Docker 容器
轻松搞定Docker!教你一键删除所有镜像!
轻松搞定Docker!教你一键删除所有镜像!
|
11月前
|
存储 IDE JavaScript
【HarmonyOS Next开发】端云一体化初始化项目
端云一体化开发是HarmonyOS对云端开发的支持、实现端云联动。云开发服务提供了云函数、云数据库、云存储等服务,可以使开发者专注于应用的业务逻辑开发,无需关注基础设施,例如:服务器、操作系统等问题。
234 6
【HarmonyOS Next开发】端云一体化初始化项目
|
网络协议 算法 Java
04SpringCloud 之 Consul 简介
04SpringCloud 之 Consul 简介
316 0
|
Web App开发 安全 Linux
FFmpeg开发笔记(二十六)Linux环境安装ZLMediaKit实现视频推流
《FFmpeg开发实战》书中介绍轻量级流媒体服务器MediaMTX,但其功能有限,不适合生产环境。推荐使用国产开源的ZLMediaKit,它支持多种流媒体协议和音视频编码标准。以下是华为欧拉系统下编译安装ZLMediaKit和FFmpeg的步骤,包括更新依赖、下载源码、配置、编译、安装以及启动MediaServer服务。此外,还提供了通过FFmpeg进行RTSP和RTMP推流,并使用VLC播放器拉流的示例。
1462 3
FFmpeg开发笔记(二十六)Linux环境安装ZLMediaKit实现视频推流