Spring Boot学习笔记

Spring Boot

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

特点

  1. 不需要web.xml
  2. 不需要Springmvc.xml
  3. 不需要tomcat,Spring Boot内嵌了tomcat
  4. 不需要配置JSON解析
  5. 个性化配置风采简单

如何操作

创建Maven工程,导入相关依赖

在这里插入图片描述

所有文件层级关系如图

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>springboot1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- 继承父包 -->
    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.0.7.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <!-- web启动jar包 -->
            <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>

</project>

创建实体类

在这里插入图片描述

package com.makerjack.entity;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Student {
    private long id;
    private String name;
    private int age;
}

业务方法

在这里插入图片描述

package com.makerjack.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public interface StudentRepository {
    public Collection<Student> findAll();
    public Student findById(long id);
    public void saveOrUpdate(Student student);
    public void deleteById(long id);
}

类数据库及方法实现

package com.makerjack.repository.impl;

import com.makerjack.entity.Student;
import com.makerjack.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);
    }
}

以往在类前面添加注解@Repository后,还需要在配置文件配置自动扫描,现在Springboot自动帮我们扫描

Handler

package com.makerjack.controller;

import com.makerjack.entity.Student;
import com.makerjack.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 {
    
    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);
    }
}

接下来就是启动项目,以往是使用tomcat来作为服务器容器
现在Springboot自己内置了tomcat

application.yml

可以改端口,在resource目录下
在这里插入图片描述

server:
  port: 9090

启动类

在这里插入图片描述

@SpringBootApplication 表示当前类是SpringBoot的入口,application类存放位置必须是其他相关业务类同级或父级
启动类内部是固定写法

package com.makerjack;

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

接下来就可以用airpost或者postman等前端测试工具来进行测试

Spring Boot 整合JSP

新建包含webapp的工程,否则无法访问jsp
在这里插入图片描述

在pom.xml中添加依赖


<!--父类依赖-->
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.0.7.RELEASE</version>
</parent>

<dependencies>
  <!--web相关组件-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <!--整合jsp-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
  </dependency>
  <dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
  </dependency>

  <!--JSTL-->
  <dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
  </dependency>

  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>

  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
  </dependency>
</dependencies>

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>1.7</maven.compiler.source>
  <maven.compiler.target>1.7</maven.compiler.target>
<!--    <tomcat.version>9.0.31</tomcat.version>-->
</properties>

如果遇到tomcat-embed-jasper爆红,可以尝试更改tomcat的版本

在这里插入图片描述

编写application.yml

在这里插入图片描述
把之前的Student一套文件复制过来
在这里插入图片描述

编写handler

package com.makerjack.controller;

import com.makerjack.entity.Student;
import com.makerjack.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/hello")
public class HelloHandler {
    @Autowired
    private StudentRepository studentRepository;

    @GetMapping("/index")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        modelAndView.addObject("list",studentRepository.findAll());
        return modelAndView;
    }

    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") long id){
        studentRepository.deleteById(id);
        return "redirect:/hello/index";
    }

    @PostMapping("/save")
    public String save(Student student){
        studentRepository.saveOrUpdate(student);
        return "redirect:/hello/index";
    }

    @PostMapping("/update")
    public String update(Student student){
        studentRepository.saveOrUpdate(student);
        return "redirect:/hello/index";
    }

    @GetMapping("/findById/{id}")
    public ModelAndView findById(@PathVariable("id") long id){
        Student student = studentRepository.findById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/update");
        modelAndView.addObject("student",student);
        return modelAndView;
    }
}

创建前端页面

首页用于展示数据并且提供增删改的操作

<%--
  Created by IntelliJ IDEA.
  User: M
  Date: 2021/12/9
  Time: 11:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>学生信息</h1>
    <table>
        <tr>
            <th>学生编号</th>
            <th>学生姓名</th>
            <th>学生年龄</th>
            <th>操作</th>
        </tr>
        <c:forEach items="${list}" var="student">
            <tr>
                <td>${student.id}</td>
                <td>${student.name}</td>
                <td>${student.age}</td>
                <td>
                    <a href="/hello/findById/${student.id}">修改</a>
                    <a href="/hello/deleteById/${student.id}">删除</a>
                </td>
            </tr>
        </c:forEach>
    </table>
    <a href="/save.jsp">添加学生</a>
</body>
</html>

修改页面先接收后端传来的学生对象数据并且显示(其中id不可改变)

<%--
  Created by IntelliJ IDEA.
  User: M
  Date: 2021/12/9
  Time: 14:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>update</title>
</head>
<body>
<form action="/hello/update" method="post">
    ID:<input type="text" name="id" value="${student.id}" readonly><br>
    name:<input type="text" name="name" value="${student.name}"><br>
    age:<input type="text" name="age" value="${student.age}"><br>
    <input type="submit" value="提交">
</form>
</body>
</html>

新增页面类似,但没有预设值

<%--
  Created by IntelliJ IDEA.
  User: M
  Date: 2021/12/9
  Time: 14:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>save</title>
</head>
<body>
    <form action="/hello/save" method="post">
        ID:<input type="text" name="id" ><br>
        name:<input type="text" name="name" ><br>
        age:<input type="text" name="age" ><br>
        <input type="submit" value="提交">
    </form>

</body>
</html>

在这里插入图片描述

Spring Boot整合HTML

Spring Boot 可以结合Thymeleaf模板来整合HTML,使用原生的HTML作为视图。
Thymeleaf模板是面向Web和独立环境的java模板引擎,能够处理HTML、XMLJavaScript、CSS等。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值