目录
一、SpringBoot简介
传统Spring开发缺点:
1、导入依赖繁琐
2、项目配置繁琐
Spring Boot是全新框架(更像是一个工具,脚手架),是Spring提供的一个子项目,用于快速构建Spring应用程序。
随着Spring 3.0的发布,Spring 团队逐渐开始摆脱XML配置文件,并且在开发过程中大量使用"约定优先配置"(convention over configuration)的思想来摆脱Spring框架中各类繁复纷杂的配置。
SpringBoot的特性:
1.起步依赖 jar包的管理 starter
2.自动配置 SpringBoot做了很多默认的配置
3.内置tomcat
二、创建SpringBoot项目
1、pom.xml
xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.easy</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</name>
<description>springboot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<!--Spring相关依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--SpringMVC相关依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<!--thymeleaf相关依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--lombok帮助我们生成实体类的构造方法、get、set、toString-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.14</version>
<scope>provided</scope>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、SpringbootApplication启动类
java
//SpringBoot项目启动类
@SpringBootApplication
@MapperScan("com.easy.springboot.mapper")
public class SpringbootApplication {
public static void main(String[] args) {s
SpringApplication.run(SpringbootApplication.class, args);
}
}
java
@Controller
@RequestMapping("/student")
public class StudentController {
@RequestMapping("/selectAll")
@ResponseBody
public List<Student> selectAll() {
System.out.println("StudentController.selectAll");
List<Student> list = new ArrayList<>();
Student student1 = new Student(1, "zhangsan1", 23, "男");
Student student2 = new Student(2, "zhangsan2", 24, "男");
Student student3 = new Student(3, "zhangsan3", 25, "男");
list.add(student1);
list.add(student2);
list.add(student3);
return list;
}
}
3、application.properties
html
server.port=8080
#DB Configuration
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/study?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2b8&zeroDateTimeBehavior=CONVERT_TO_NULL
spring.datasource.username=root
spring.datasource.password=1234
spring.mvc.static-path-pattern=/static/**
#Spring MyBatis
mybatis.type-aliases-package=com.easy.springboot.pojo
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.configuration.map-underscore-to-camel-case=true
#log
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
4、StudentMapper.xml
xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.easy.springboot.mapper.StudentMapper">
<select id="selectAll" resultType="Student">
SELECT id,name,age,gender FROM student
</select>
</mapper>
代码:
java
package com.easy.springboot.controller;
import com.easy.springboot.pojo.Student;
import com.easy.springboot.service.IStudentService;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private IStudentService studentService;
@RequestMapping(value = "/util")
public String set(Model model) {
Set<String> names = new HashSet<String>() ;
List<Integer> ids = new ArrayList<Integer>() ;
for (int i = 0 ; i < 5 ; i ++) {
names.add("boot-" + i) ;
ids.add(i) ;
}
model.addAttribute("names", names) ;
model.addAttribute("ids", ids) ;
model.addAttribute("mydate", new Date()) ;
return "util" ;
}
@RequestMapping("/data")
public String data(HttpServletRequest request, HttpSession session) {
Student student1 = new Student(1, "张三1", 23 , "男");
request.setAttribute("student1", student1);
Student student2 = new Student(2, "张三2", 23, "男");
session.setAttribute("student2", student2);
Student student3 = new Student(3, "张三3", 23, "男");
ServletContext application = request.getServletContext();
//application是ServletContext创建的对象
application.setAttribute("student3", student3);
return "data";
}
@RequestMapping("/selectAll")
public String selectAll(Model model) {
System.out.println("StudentController.selectAll");
model.addAttribute("message", "HelloWorld");
Student student = new Student(1, "李四", 23, "男");
model.addAttribute("student", student);
/*List<Student> list = new ArrayList<>();
Student student1 = new Student(1, "zhangsan1", 23, "男");
Student student2 = new Student(2, "zhangsan2", 24, "男");
Student student3 = new Student(3, "zhangsan3", 25, "男");
list.add(student1);
list.add(student2);
list.add(student3);*/
List<Student> list = studentService.selectAll();
//放入域对象
model.addAttribute("list",list);
//springboot默认配置了视图解析器,前缀templates后缀.html都不用写
return "student_list";
}
@RequestMapping("/selectAll2")
@ResponseBody
public List<Student> selectAll2() {
System.out.println("StudentController.selectAll2");
/*List<Student> list = new ArrayList<>();
Student student1 = new Student(1, "zhangsan1", 23, "男");
Student student2 = new Student(2, "zhangsan2", 24, "男");
Student student3 = new Student(3, "zhangsan3", 25, "男");
list.add(student1);
list.add(student2);
list.add(student3);*/
return studentService.selectAll();
}
}
java
package com.easy.springboot.mapper;
import com.easy.springboot.pojo.Student;
import java.util.List;
public interface StudentMapper {
List<Student> selectAll();
}
java
package com.easy.springboot.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Integer id;
private String name;
private Integer age;
private String gender;
}
java
package com.easy.springboot.service.impl;
import com.easy.springboot.mapper.StudentMapper;
import com.easy.springboot.pojo.Student;
import com.easy.springboot.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
//因为到时候需要从容器中拿出一个StudentServiceImpl对象,
//所以@Service是在StudentServiceImpl上面而不是IStudentImpl
@Service
public class StudentServiceImpl implements IStudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public List<Student> selectAll() {
return studentMapper.selectAll();
}
}
java
package com.easy.springboot.service;
import com.easy.springboot.pojo.Student;
import java.util.List;
public interface IStudentService {
List<Student> selectAll();
}
java
package com.easy.springboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//该类需与pojo等包平级
//启动类
//运行main方法即可启动tomcat(内置)
@SpringBootApplication
@MapperScan("com.easy.springboot.mapper")
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
java
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.easy.springboot.mapper.StudentMapper">
<!-- public List<Student> selectAll(); -->
<select id="selectAll" resultType="Student">
SELECT id,name,age,gender FROM student
</select>
</mapper>
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
request:
<div>
编号:<span th:text="${student1.id}"></span><br>
姓名:<span th:text="${student1.name}"></span><br>
年龄:<span th:text="${student1.age}"></span><br>
</div>
session:
<div>
编号:<span th:text="${session.student2.id}"></span><br>
姓名:<span th:text="${session.student2.name}"></span><br>
年龄:<span th:text="${session.student2.age}"></span><br>
</div>
application:
<div>
编号:<span th:text="${application.student3.id}"></span><br>
姓名:<span th:text="${application.student3.name}"></span><br>
年龄:<span th:text="${application.student3.age}"></span><br>
</div>
</body>
</html>
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:fragment="common_resource">
<link rel="stylesheet" th:href="@{/static/bootstrap-3.4.1-dist/css/bootstrap.css}">
<script type="text/javascript" src="/static/jquery-2.1.4.js"></script>
<script type="text/javascript" th:src="@{/static/mylayer.js}"></script>
</div>
</body>
</html>
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!-- 使用th:text属性输出 -->
<div th:text="${message}" ></div>
<input type="text" th:id="${student.id}" th:name="${student.name}" th:value="${student.name}"><br/>
<!--循环-->
<table border="1">
<tr>
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
<td>生日</td>
</tr>
<tr th:each="student:${list}">
<td th:text="${student.id}">1</td>
<td th:text="${student.name}">张三</td>
<td th:text="${student.age}">23</td>
<td th:text="${student.gender}">男</td>
</tr>
</table>
<!--判断
Thymeleaf支持四种判断:th:if/th:unless、逻辑运算符(and、or、not)、三目运算符、switch。-->
<!--第一种:if & unless-->
<!-- 如果条件为真,执行标签内的内容 -->
<div th:if="${list[0].name eq '张三1'}">
张三1
</div>
<div th:if="${list[0].name == '张三1'}">
张三1
</div>
<!-- 如果条件为假,执行标签内的内容 -->
<div th:unless="${false}">
unless
</div>
<!--第二种:and、or、not-->
<div th:if="${true or false}">
真的18岁
</div>
<div th:if="${not false}">
真的别做梦
</div>
<!--第三种:三目运算符-->
<span th:text="${student.age eq 23} ? '今年是23' : '不是23'"></span>
<span th:text="${student.age eq 23 ? '今年23' : '不是23'}"></span>
<!--第四种:switch-->
<div th:switch="${student.age}">
<div th:case="16">我今年16岁</div>
<div th:case="17">我今年17岁</div>
<div th:case="18">我今年18岁</div>
<div th:case="20">我今年20岁</div>
<div th:case="23">我今年23岁</div>
<div th:case="*">我年年18岁</div>
</div>
<!--不需要为了switch额外创建一个div标签-->
<th:block th:switch="${student.age}">
<div th:case="16">我今年16岁</div>
<div th:case="17">我今年17岁</div>
<div th:case="18">我今年18岁</div>
<div th:case="20">我今年20岁</div>
<div th:case="23">我今年23岁</div>
<div th:case="*">我年年18岁</div>
</th:block>
</body>
</html>
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--原来写法中路径前要加上/static,springboot中不用加,加了反而报错,
因为static也是一个约定俗成的路径,相当于已经加了/static前缀-->
<!--<link rel="stylesheet" th:href="@{/bootstrap-3.3.7-dist/css/bootstrap.css}">
<script type="text/javascript" src="/jquery-2.1.4.js"></script>
<script type="text/javascript" th:src="@{/mylayer.js}"></script>-->
<div th:replace="header::common_resource"></div>
</head>
<body>
<p th:text="${#dates.format(mydate,'yyyy-MM-dd')}"/>
<p th:text="${#dates.format(mydate,'yyyy-MM-dd HH:mm:ss.SSS')}"/>
<hr/>
<p th:text="${#strings.replace('www.baidu.cn','.','$')}"/>
<p th:text="${#strings.toUpperCase('www.baidu.cn')}"/>
<p th:text="${#strings.trim('www.baidu.cn')}"/>
<hr/>
<p th:text="${#sets.contains(names,'boot-0')}"/>
<p th:text="${#sets.contains(names,'boot-9')}"/>
<p th:text="${#sets.size(names)}"/>
<hr/>
<p th:text="${#lists.contains(ids,0)}"/>
</body>
</html>
html
spring.application.name=springboot
server.port=8080
#DB Configuration
#?????
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/study?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2b8&zeroDateTimeBehavior=CONVERT_TO_NULL
spring.datasource.username=root
spring.datasource.password=1234
spring.mvc.static-path-pattern=/static/**
#Spring MyBatis
#alias???
mybatis.type-aliases-package=com.easy.springboot.pojo
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.configuration.map-underscore-to-camel-case=true
#log
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl