SpringBoot
一、引言
1.1 什么是SpringBoot?
Struts2 + hibernate + spring (.java < .xml)
默认 > 约定 > 配置 > 编码 “零配置”
SpringBoot是帮助开发者进行快速开发Spring轻应用的一种框架。很多的第三方框架和组件都会和SpringBoot整合(反向整合),方便SpringBoot进行第三方框架和组件的调用。类似Maven整合了所有jar包,SpringBoot整合了所有框架。
SpringBoot是基于Maven(gradle)构建的。Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。换句话说,就是spring boot其实不是什么新的框架,它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,spring boot整合了所有的框架。
二、SpringBoot的构建
2.1 手动将Maven工程改造成SpringBoot(后面完全可以自动化)
1)创建一个干净的maven工程
2)继承SpringBoot父工程(工程的pom.xml)
<!-- maven工程继承SpringBoot父工程 -->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.0.RELEASE</version>
</parent>
3)添加SpringBoot的核心依赖
<!-- 添加SpringBoot的核心依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
4)添加Maven和Springboot打包插件(如果不打包只运行可以不加这个)
<!-- 编译打包插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
5)准备核心配置文件
6)准备一个启动类(开启SpringBoot工程)
package com.qf.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
7)启动SpringBoot工程
注意:如果springboot的启动类没有放在项目的顶层,需要加上包扫描注解
@SpringBootApplication(scanBasePackages = "com.jh")
1.2 将SpringMVC整合进SpringBoot
1)添加SpringMVC和SpringBoot整合依赖(起步依赖)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2)配置Controller
package com.qf.application.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class TestController {
@RequestMapping("/hello")
public String test(){
System.out.println("Controller的方法被调用了!");
return "SUCC";
}
}
3)访问controller
三、yml配置文件
3.1 什么是yml?
在 spring boot 中,有两种配置文件,一种是application.properties,另一种是application.yml,
两种都可以配置spring boot 项目中的一些变量的定义,参数的设置等
3.1 yml配置文件的格式(注意冒号后面的空格)
properties配置格式:
jdbc.url=xxxxxx
jdbc.username=xxxxx
yml的配置格式:
jdbc:
url: xxxxx
username: xxxx
3.2 代码手动读取yml配置
1)单值读取
#配置
com:
name: 张三
//代码
@Value("${com.name}")
private String name;
2)多值读取
com:
person:
name: 张三
age: 18
like:
- 唱
- 跳
- rap
- 篮球
address:
prod: 广东省
city: 深圳市
@Data
@Accessors(chain = true)
@Component
@ConfigurationProperties(prefix = "com.person")
public class Person {
private String name;
private Integer age;
private List<String> like;
private Map<String, String> address;
}
TestController.java
@RestController
@RequestMapping("/test")
public class TestController {
@Value("${com.person.name}")
private String name;
@Autowired
private Person person;
@Autowired
private StudentMapper studentMapper;
@RequestMapping("/hello")
@Transactional
public String test1(){
System.out.println("Controller的test1方法被调用了");
// System.out.println("**************"+name);
// System.out.println(person);
Student student = new Student(0,"张武",44);
// int i = 1/0;
studentMapper.insert(student);
return "成功";
}
}
四、SpringBoot整合Mybatis
4.1 曾经SSM整合的步骤
1、添加依赖(Mybatis.jar、Mybatis-spring.jar)
2、添加映射接口 + 映射xml
3、配置datasource(url、password、username、driver、连接池的配置)
4、配置SqlSessionFactorBean(注入datasource、扫描映射文件、配置mybatis其他的配置)
5、扫描映射接口
6、在需要的地方注入mybatis代理对象
4.2 SpringBoot整合MyBatis
1)添加依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2)准备映射接口+映射xml
interface
public interface StudentMapper {
/**
*
* @return
*/
int insert(Student student);
}
mapper
<?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.jh.dao.StudentMapper">
<insert id="insert">
insert into student values(null,#{name},#{age})
</insert>
</mapper>
3)配置数据源(application.yml)
#数据源
spring:
datasource:
url: jdbc:mysql://192.168.195.188:3306/mydb?serverTimezone=GMT%2B8
username: root
password: Root123_
driver-class-name: com.mysql.cj.jdbc.Driver
4)配置映射文件扫描(application.yml)
#配置mybatis扫描映射文件
mybatis:
mapper-locations: classpath:com/qf/dao/mapper/*.xml
5)启动类添加注解(扫描映射接口,不然需要在一个个映射接口上面加@Mapper)
@MapperScan("com.jh.dao")
6)配置数据库事务
//启动类添加注解
@EnableTransactionManagement
//需要的业务方法添加注解
@Transactional
五、SpringBoot整合Thymeleaf
5.1 SpringBoot整合Thymeleaf的流程
1)添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2)配置application.yml(完全可以用默认的)
spring:
thymeleaf:
#关闭页面缓存
cache: false
3)编写页面(.html)
六、SpringBoot整合MyBatis-Plus
1)添加Mybatis-plus依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
2)配置数据源、实体类
3)编写映射接口
public interface ClsMapper extends BaseMapper<Classes> {
}
4)启动类配置映射接口扫描
@MapperScan("com.jh.dao")
5)找到实体类,给主键添加注解
@TableId(type = IdType.AUTO)
private Integer id;
6)在service层注入mapper操作数据库
PS 思考
1 没有配置扫描,SpringBoot是如何找到controller的?
SpringBoot默认扫描启动类所在包下的所有包(类)
可以通过修改启动类的注解,实现自定义扫包
@SpringBootApplication(scanBasePackages = “com.a”)