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)



目录
相关文章
|
存储 开发者
国标GB28181协议客户端开发(二)程序架构和注册
国标GB28181协议客户端开发(二)程序架构和注册
1273 0
|
3月前
|
安全 Java 数据库连接
SpringBoot使用小汇总
Spring Boot基于Spring框架,通过“约定优于配置”和丰富Starter依赖,简化企业级Java应用开发。具备零配置、内嵌服务器、自动依赖管理及生产级特性,适用于微服务与单体架构。本文从核心特性、开发实践、性能优化与生态扩展四方面深入解析。
254 2
|
6月前
|
IDE Java 开发工具
说一说 SpringBoot 中 CommandLineRunner
我是小假 期待与你的下一次相遇 ~
385 42
说一说 SpringBoot 中 CommandLineRunner
|
API 开发工具 Android开发
简述大疆无人机对接
【2月更文挑战第7天】本文介绍了对接大疆无人机的主要目的,包括实时画面获取、飞行数据监测、操控飞行、媒体管理和业务功能开发等,并列举了多种开发接口如MobileSDK、UXSDK、云开发API等。重点讨论了MobileSDK在Android平台的应用,包括SDK集成步骤、直播推流和获取飞机实时数据的细节。另外,UXSDK用于加速应用开发,提供预设UI组件。上云API则简化了无人机与第三方云平台的集成,支持MQTT、HTTPS和WebSocket协议,适用于行业级无人机。对接流程涉及Pilot2和Dock的配置,以及数据传输和业务功能处理。文章还提及了如何对接多个飞机的方法。
11168 0
简述大疆无人机对接
|
12月前
|
存储 IDE JavaScript
【HarmonyOS Next开发】端云一体化初始化项目
端云一体化开发是HarmonyOS对云端开发的支持、实现端云联动。云开发服务提供了云函数、云数据库、云存储等服务,可以使开发者专注于应用的业务逻辑开发,无需关注基础设施,例如:服务器、操作系统等问题。
256 6
【HarmonyOS Next开发】端云一体化初始化项目
|
存储 负载均衡 监控
HBase分布式数据库架构及原理
Client是操作HBase集群的入口,对于管理类的操作,如表的增、删、改操纵,Client通过RPC与HMaster通信完成,对于表数据的读写操作,Client通过RPC与RegionServer交互,读写数据。
1042 0
HBase分布式数据库架构及原理
|
网络协议 算法 Java
04SpringCloud 之 Consul 简介
04SpringCloud 之 Consul 简介
385 0
|
SQL 存储 安全
使用 SpringBoot 访问 MySQL 数据库
创建一个 MySQL 数据库,构建一个 Spring 应用程序,并将其连接到新创建的数据库。
506 0
|
负载均衡 Java Maven
SpringBoot整合SpringCloud基础实践入门
SpringBoot整合SpringCloud基础实践入门
402 1
|
Linux
Linux命令(94)之tail
Linux命令(94)之tail
484 0