前言
SpringBoot用来简化Spring应用的初始搭建以及开发过程。
- 回顾SpringMVC开发过程、坐标
- ServletConfig配置类
- SpringMVCConfig配置类
- Controller类
SpringBoot入门案例
- 创建
然后next + OK
- 删除无用的包(除了pom和src,其他的都删)
- 书写控制器类/itheima/controller/BookController
@RestController
@RequestMapping("/books")
public class BookController{
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println("id==>"+ id);
return "hello,Spring Boot!";
}
}
- 运行(不再使用tomcat服务器)
官网创建方式
如果没有网的话,就需要在Spring官网中进行下载。
- 点击后,创建
- 然后点击下方的GENERATING,生成zip文件
SpringBoot项目快速启动
前端想做测试,就需要连接后端的电脑才能做测试。
此时前端人员就要装IDE和Tomcat,但是用了SpringBoot就不用这么麻烦。直接通过jar包就可以运行(不包括数据库)。
- 执行package,会将此工程打包
- 打好包后,在/target/xx.jar包
将文件所在的文件夹中,打开cmd:
java -jar jar的文件名
- 然后就可以直接用了
然后前端人员在cmd中,就会得到他成功测试后的结果
注意:
SpringBoot简介
为什么SpringBoot这么好用?因为他继承了一个东西:
在底层中,SpringBoot把好多个依赖所能够兼容的互相的不同的版本进行了整合,比如Spring依赖可以搭配Junit的2个版本,那么我就整合。大大减少了我们版本冲突的问题,我们只需要直接选择SpringBootx.x版本即可。
整合了这么多,谁引用?【具体用哪个版本就依靠他】
SpringBoot辅助依赖
让程序内置一个tomcat服务器
引导类是项目的入口
SpringBoot基础配置
修改端口格式:localhost:8080==>localhost/book
- 创建/resources/application.properties
server.port = 80
- 创建/resources/application.yml
server:
port: 81
...: xx
...: xx
- 创建/resources/application.yaml 格式和yml一样,只是扩展名不同
server:
port: 82
如果存在没有代码提示的情况:
点击添加后,这两个就是配置文件了,就会有提示
后续我们主要写.yml格式
如果这三个文件都存在,谁的优先级高?
properties > yml > yaml
给日志的等级配置为info,只输出info级别的信息。
yaml格式
- 语法规则:
核心规则:数据前面要加空格与冒号隔开
- 数组数据
读取yaml数据
- application.yaml中的数据: 属性以空格开头
lesson: SpringBoot
server:
port: 80
enterprise:
name: itcast
age: 16
tel: 123456789
subjet:
- Java
- 前端
- BookController.java读取数据 通过@Value("{根层.父层.[下标]})`
@RestController
@RequestMapping("/books")
public class BookController{
@Value("${lesson}")
private String lesson;
@Value("${enterprise.subject[0]}")
private String subject_00;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println(lesson);
System.out.println(subject_00);
return "hello,SpringBoot!";
}
}
当我在postman根据/books/1发送请求时,IDEA收到请求后,打印lesson的值,并且返回JSON数据给客户端(postman);
- 获取数据改进方法
上方的获取数据方法,需要对每个数据进行@Value,一旦数据过多时,就会存在问题。
通过环境类,来定义属性,通过自动装配,获取值;
@Autowired
private Environment environment;
此时,yaml中定义的所有数据,都可以获取到:
environment.getProperty("lesson");
environment.getProperty("server.port");
environment.getProperty("enterprise.age");
environment.getProperty("enterprise.subject[1]");
- 通过实体类加载属性【最常用】 Enterprise.java
//让他受Spring控制、并让他从配置中读出属性
@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise{
private String name;
private Integer age;
private String tel;
private String[] subject;
......
toString()方法、set、get方法
}
prefix = "enterprise"中的enterprise必须和yaml中的名字相同,此时他就可以让enterprise中的属性和实体类中的数据进行一一匹配并得到数据;
BookController.java使用:
@Autowired//注入数据
private Enterprise enterprise;
System.out.println(enterprise);
多环境开发配置
在一个设置中,配置多个环境,并且让这些环境都能够切换使用。
- application.yml通过横线,区分环境
//设置启用的环境:测试
spring:
profiles:
active: test
//设置成:开发环境
spring:
profile: dev
server:
port: 80
---
//设置成:生产环境
spring:
profiles: pro
server:
port: 81
---
//设置成:测试环境
spring:
profiles: test
server:
port: 82
2. 第二种方式:
创建application-dev.properties、application-pro.properties两个文件
application.properties比application.yml优先级高,所以会先执行application.properties文件
application.properties
//设置启用的环境
spring.profiles.active = dev
application-dev.properties
server.port=80
application-pro.properties
server.port=81
多环境启动命令格式
后端人员设置环境时,一般默认是运行环境,但是测试人员想要调试环境时,必须是测试环境。如果再让后端人员修改的话会比较麻烦,其实测试人员是可以收到jar包后,自己来修改的。
在我们进行package操作时,最好先clean一下。
配置文件中,我们会写中文,这样是会打包失败的,需要修改配置
执行步骤
-
target/springboot.jar文件,在这个目录中进行cmd
-
执行启动
java -jar 文件名 -
切换环境
java -jar 文件名 --spring.profiles.active=test
- 补充 优先级:
多环境开发兼容问题
Maven做最终的控制,SpringBoot只是给Maven打工的人。
因为最终生成的是jar包,执行的也是jar包,jar包就是由maven工程创建的。
在pom.xml中设置环境开发
<profiles>
<!--开发环境-->
<profile>
<id>dev</id>
<properties>
</properties>
</profile>
<!--默认是:生产环境-->
<profile>
<id>pro</id>
<properties>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!--测试环境-->
<profile>
<id>test</id>
<properties>
</properties>
</profile>
</profiles>
并且同时创建/resources/application.yml
# 设置默认启用的环境
spring:
profiles:
active: dev
---
# 开发环境
spring:
profiles: dev
server:
port: 80
---
# 生成环境
spring:
profiles: pro
server:
port: 81
---
# 测试环境
spring:
profiles: test
server:
port 82
---
当我们执行jar包时,就会通过设置的环境,来判断谁被优先执行了。
如果我们的pom.xml文件中的数据,想要给Spring配置文件(resources中的资源)使用,需要在pom.xml中添加一个插件。(配置文件想用pom数据)
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<encoding>UTF-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
</build>
配置文件分类
-
resources/application.yml是最普通的配置文件目录
-
创建一个新的目录:/resources/config/application.yml;这个文件中的配置文件会覆盖resources/application.yml。
-
如果在/target/application.yml中再配置,那么这个文件会覆盖resources中的两个配置文件
-
如果在/target/config/application.yml中又写了一个config文件夹包含的配置文件,那么他的优先级是最高的,会覆盖上方3个配置文件
有些特殊版本可能会报错(存在bug2.5.0和2.4.6版本会存在),需要在target的config中除了书写yml文件,还需要写一个文件夹。
整合第三方技术
整合JUnit
前期回顾:
- 创建项目
- 创建服务层com.itheima.service/BookService.java
public interface BookService{
public void save();
}
com.itheima.service/impl/BookServiceImpl.java
@Service
public class BookServiceImpl implements BookService{
@Override
public void save(){
System.out.println()
}
}
- 测试类中书写测试(这个已经帮我们建好了) test/java/com.itheima.SpringBootTestApplicationTests
默认打开的样子
@SpringBootTest
class SpringBootTestApplicationTests{
@Autowired
private BookService bookService;
//这个方法是SpringBoot提供的
@Test
public void contextLoads(){
bookService.save();
}
//也可以是这个:
@Test
public void save(){
bookService.save();
}
}
- 自动生成的配置类SpringBootTestAppplication
他会自动加载bean,测试类也会默认加载引导类,初始化环境。
前提:必须在同层包!如果不在同层包里,就需要指定。
自动生成的配置类:
@SpringBootTest(classes = 测试类SpringbootTestApplication.class)
整合mybatis
前两步只需要在一开始打上勾,引进依赖即可
复习Spring整合MyBatis
开始整合MyBatis
- 创建项目
- 新建 com.itheima/domain/Book.java
public class Book{
private Integer id;
private String name;
private String type;
private String description;
...
set、get方法,以及toString方法
}
com.itheima/dao/BookDao.java
//定义数据层接口与映射配置
@Mapper
public interface BookDao{
@Select("select * from tb_book where id = #{id}")
public Book getById(Integer id);
}
配置数据源resources/application.yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysq://localhost:3306/ssm_db
username: root
password: root
测试类java/com.itheima/SpringBootMyBatisApplicationTest
@SpringBootTest
class SpringBootMyBatisApplicationTest{
@Autowired
private BookDao bookDao;
@Test
void testGetById(){
Book book = bookDao.getById();
System.out.println(book);
}
}
和druid搭配使用:
- 导入坐标
- 通过type属性来指定数据源类型(重新刷新一下maven)
- 使用druid的话,需要注意的一个点:mysql8以后需要加上“cj”,并且指定时间
整合SSM
- pom.xml
配置起步依赖,必要的资源坐标(druid)
- application.yml
设置数据源、端口等
- 配置类
全部删除
- dao
设置@Mapper
-
测试类
-
页面