springboot概述和快速搭建

本文介绍SpringBoot框架,一种基于Spring的快速开发工具,简化了应用搭建过程,无需web.xml等配置,支持REST架构,个性化配置简单。文章详细讲解了从创建Maven项目到实现RESTful API的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Boot

Spring Boot 是一个快速开发框架,可以迅速搭建出一套基于 Spring 框架体系的应用,是 SpringCloud 的基础。
Spring Boot 开启了各种自动装配,从而简化代码的开发,不需要编写各种配置文件,只需要引用相关依赖就可以迅速搭建一个应用。
特点
1、不需要 web.xml
2、不需要 springmvc.xml
3、不需要 tomcat,Spring Boot 内嵌了 tomcat,端口号等配置在yml文件中写入
4、不需要配置 JSON 解析,支持持 REST 架构
5、个性化配置非常简单

如何使用

  1. 创建Mavn工程,导入相关依赖
<!-- 继承⽗包 -->
<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.ztb.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
//@NoArgsConstructor  无参构造方法
//@AllArgsConstructor有参构造方法

public class Student {
        private long id;
        private String name;
        private int age;


3.repository持久层接口编写

package com.ztb.repository;

import com.ztb.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.repositoryImpl持久层实现

package com.ztb.repository.impl;

import com.ztb.entity.Student;
import com.ztb.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));
    }
    //还未整合数据库  先写入静态代码块  填充数据
    


    @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 cotroller 层代码实现

package com.ztb.controller;

import com.ztb.entity.Student;
import com.ztb.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")//查找所有student对象
    public Collection<Student> findAll(){
        return studentRepository.findAll();
    }
    @GetMapping("/findById/{id}")//通过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")//传入student  java对象
    public void update(@RequestBody Student student){
        studentRepository.saveOrUpdate(student);
    }
    @DeleteMapping("/deleteById/{id}")//通过id删除数据
    public void deleteById(@PathVariable("id") long id){
        studentRepository.deleteById(id);
    }
}

6.在resource 文件夹下新建appplication.yml文件
配置端口号,视图解析器等相关配置

server:
  port: 9090

7.在项目全局目录下创建springboot启动类 ,

package com.ztb;


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

在输入框键入rest风格地址

运行结果显示:
在这里插入图片描述

到此为止 一个简易的springboot就搭建完成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值