1、软件版本
Java 1.8.0_171(大版本一致
MySQL 8.0.12(大版本一致)
Maven 3.3.9(大版本一致)
Spring Boot 2.2.1(版本需要严格一致)
2、Spring、Spring MVC和Spring Boot
Spring最初利用IOC和AOP解耦
按照这种模式搞了MVC框架
写很多样板代码很麻烦,就有了Spring Boot
Spring Cloud是在Spring Boot基础上诞生的
3、Spring Boot核心特点
开箱即用
约定优于配置
4、Spring Boot版本介绍
CURRENT 当前版本
GA 稳定版本
SNAPSHOT 快照
如何选择版本? 不一定选择最新版本,应选择稳定版本
5、Spring Boot项目创建
(1)通过官网创建
官网地址:https://start.spring.io/
填写并勾选相关信息,添加Dependencies依赖,点击GENERATE即可下载项目压缩包,解压后导入IDEA即可。
(2)通过IDEA的Initializr创建
填写相关信息即可创建项目
6、编写第一个Spring Boot程序
编写第一个Spring Boot程序并演示各种参数传递形式
package com.demo.springbootlearn;
import org.springframework.web.bind.annotation.*;
/**
* 演示各种参数传递形式
*/
@RestController
//为请求添加统一前缀
@RequestMapping("/testSpringBoot")
public class TestController {
/**
* 前端请求地址为 http://localhost:8080/testSpringBoot/testController
* @return
*/
@GetMapping("/testController")
public String testSpringBoot(){
return "测试Spring Boot项目!";
}
/**
* 前端请求地址为 http://localhost:8080/testSpringBoot/testParams1?number=4
* @param number
* @return
*/
@GetMapping("/testParams1")
public String testParams1(@RequestParam Integer number){
return "获取的参数为:"+number;
}
/**
* 前端请求地址为 http://localhost:8080/testSpringBoot/testParams2/6
* @param number
* @return
*/
@GetMapping("/testParams2/{number}")
public String testParams2(@PathVariable Integer number){
return "获取的参数为:"+number;
}
/**
* 前端请求地址为 http://localhost:8080/testSpringBoot/testParams3?number=4或
* http://localhost:8080/testSpringBoot/testParams3Copy?number=4
* @param number
* @return
*/
@GetMapping({"/testParams3","/testParams3Copy"})
public String testParams3(@RequestParam Integer number){
return "获取的参数为:"+number;
}
/**
* 前端请求地址为http://localhost:8080/testSpringBoot/testParams4?number=8或
* http://localhost:8080/testSpringBoot/testParams4(没有传递参数则使用默认值0)
* @param number
* @return
*/
@GetMapping("/testParams4")
public String testParams4(@RequestParam(required=false,defaultValue = "0") Integer number){
return "获取的参数为:"+number;
}
}
7、Spring Boot项目的配置文件
Spring Boot项目的配置文件有两种形式(两种形式可以通过在线工具相互转换【https://toyaml.com/index.html】):
(1)application.properties
//指定项目启动的端口号
server.port=8081
//为项目添加公共前缀,区分不同的项目
server.servlet.context-path=/learnSpringBoot
//指定数据库驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
//指定数据库连接用户名
spring.datasource.username=root
//指定数据库连接密码
spring.datasource.password=123456
//指定数据库连接url
spring.datasource.url=jdbc:mysql://192.168.211.128:3316/springbootlearn?serverTimeZone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
//自定义配置
student.name=tom
student.age=12
(2)application.yml
server:
port: 8081
servlet:
context-path: /learnSpringBoot
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
password: 123456
url: jdbc:mysql://192.168.211.128:3316/springbootlearn?serverTimeZone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
student:
age: 12
name: tom
8、Spring Boot项目自定义配置的使用
(1)使用方法一
a、在配置文件中添加自定义配置
student.name=tom
student.age=12
b、开始使用
package com.demo.springbootlearn;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 演示读取配置文件的controller
*/
@RestController
public class PropertiesController {
@Value("${student.name}")
String name;
@Value("${student.age}")
int age;
@RequestMapping("/getStudent")
public String getStudent(){
return "学生姓名:"+name+",年龄:"+age;
}
}
(2)使用方法二
a、在配置文件中添加自定义配置
student.name=tom
student.age=12
b、编写配置文件对应的实体类
package com.demo.springbootlearn;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "student")
public class StudentConfig {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
c、注入配置文件对象,开始使用
package com.demo.springbootlearn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PropertiesController2 {
@Autowired
StudentConfig studentConfig;
@RequestMapping("/getStudent2")
public String getStudent(){
return "学生的姓名:"+studentConfig.getName()+",年龄:"+studentConfig.getAge();
}
}
9、Spring Boot连接数据库并查询数据
(1)添加Mybatis和MySql的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 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>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.imooc</groupId>
<artifactId>spring-boot-learn</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-learn</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--添加mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!--添加MySQL依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(2)编写数据库表对应的实体类
package com.demo.springbootlearn;
/**
* 学生类
*/
public class Student {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
(2)编写Mapper
package com.demo.springbootlearn;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
/**
* 学生类Mapper
*/
@Mapper
@Repository
public interface StudentMapper {
@Select("select * from student where id=#{id}")
Student getStudent(int id);
}
(3)编写Service
package com.demo.springbootlearn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class StudentService {
@Autowired
StudentMapper studentMapper;
public Student getStudentById(int id){
return studentMapper.getStudent(id);
}
}
(4)编写Controller
package com.demo.springbootlearn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentController {
@Autowired
StudentService studentService;
@RequestMapping("/getStudentInfo")
public String getStudent(@RequestParam int id){
Student student = studentService.getStudentById(id);
return student.toString();
}
}