SpringBoot学习笔记
Spring Boot
Spring Boot 是一个集成了Spring各种组件的快速开发框架
,可以迅速搭建出一套基于Spring 框架体系的应用,是 Spring Cloud 的基础
Spring Boot 开启各种自动装配,从而简化代码的开发,不需要编写各种配置文件,只需要引入相关依赖就可以迅速搭建一个应用
特点
- 不需要web.xml
- 不需要Springmvc.xml
- 不需要tomcat,Spring Boot内嵌了tomcat
- 不需要配置JSON解析
- 个性化配置风采简单
如何操作
创建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等。