SpringBoot概述
提供了快速开发Spring的方法,秉持“约定大于配置”的思想。
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.
Spring Boot让你可以轻易的构建一个可以“直接运行”的独立的生产级的Spring项目
功能
- 自动配置:运行时自动配置
- 起步依赖:依赖打包,简化依赖
- 辅助功能:嵌入式服务器,指标,安全…
SpringBoot不是增强Spring功能,而是对Spring的优化整合
快速入门
- SpringBoot是jar打包方式
- SpringBoot的业务代码编写方式和Spring一致
- SpingBoot需要一个引导人口,通过他的main方法启动项目,不用配置tomcat
编写pom.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 继承的SpringBoot默认父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
</parent>
<!-- web依赖包 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- 插件包 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<artifactId>01HelloSpringBoot</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
Controller编写
完全等同Spring
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("hello")
public String hello(){
return "hello";
}
}
Application——程序入口
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
IDEA自动
没啥好说的,idea创建springboot项目
起步依赖
- 在spring-boot-starter-parent中定了各种技术的版本信息,组合了-套最优搭配的技术版本。
- 在各种starter中,义了完成该功能需要的坐标合集,中大部分版本信息来自于父工程。
- 我们的工程继承parent,引入starter后,通过依赖传递,就可以简单方便获得需要的jar包,并不会在版本冲突等问题。
SpringBoot配置
配置分类
基于约定,所以有很多默认值。
如果需要自定义,要使用application.properties或者application.yml/yaml文件配置
- properties 键值对配置
server.port=8080
- yml 缩进是级别关系,冒号后要有空格
server:
port: 8080
先加载yaml,之后加载yml,最后加载properties
后加载的会覆盖
也就是优先级方面是properties>yml>yaml
yaml文件格式
是一个标记性语言,比xml简洁(典中典之什么格式都比xml好)
基本语法:
- 大小写敏感
- 数据前必须有空格
- 缩进表示层级
- 不可以用tab(idea可以转换)!空格个数没关系,只要对齐即可
- #注释一行
- —三个横杠分割区域,spring: profiles: xxx标注分区
- 不要使用&转义字符代替&
数据格式
- 对象:键值对
person:
name: zhangsan
#行内写法,不常见
persion: {name: zhangsan}
- 数组:
address:
- beijing
- shanghai
address: [beijing,shanghai]
- 纯量:不可再分的单个值
msg1: 'hello \n world' #单引号忽略转义
msg2: "hello \n world" #双引号识别转义
参数引用
name: zhangsan
person:
name: ${name}
如何读取
Value注解
@Vaule(${name})
可以获取配置中name的值
@Vaule(${person.name})
可以获取配置中person变量成员name的值
@Vaule(${address[0]})
可以获取配置中address数组的第一个值
enviroment类
作为成员,使用自动装配注解
enviroment.getProperty("xxx.xxx[x]")
ConfigurationProperties注解
成员名一致,有getter,setter方法的类可以使用@ConfigurationProperties(prefix="xxx")
注解自动注解
需要加一个依赖,idea会提示
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
个人理解:可以通过这个注解使得某个组件从配置文件中寻找。因此这个注解要在需要被装配的组件上标注。prefix用于标识是配置文件中的哪个对象
需要从配置中获取的类Person
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@ConfigurationProperties(prefix = "pp.person")
public class Person {
public String name;
public int age;
public List<Integer> ints;
@Override
public String toString() {
return "person{" +
"name='" + name + '\'' +
", age=" + age +
", ints=" + ints +
'}';
}
}
application.yaml
pp:
person:
name: test1
age: 18
ints:
- 12
- 22
- 32
- 42
person:
name: test2
age: 18
ints:
- 12
- 22
- 32
- 42
测试类
@RestController
public class hello {
@Autowired
public Person person;
@RequestMapping("/hello2")
public String hello(){
System.out.println(person.toString());
return "hello222";
}
}
输出结果
person{name='test1', age=18, ints=[12, 22, 32, 42]}
profile
实际开发中,我们会有开发,测试,生产不同的环境。
我们所依赖的数据库地址,服务器端口等等配置都有所不同,我们急需一个方便动态切换的配置功能。
- application-dev.properties 开发环境
- application-test.properties 测试环境
- application-pro.properties 生产环境
需要进行激活操作
激活:
在application.properties中加入spring.profiles.active=dev
yaml也是可以的,方式类似,同时还可以配合使用—分段
可以在配置文件、虚拟机参数、命令行参数等多种方式激活
内部配置加载
Springboot程序启动时,会从以下位置加载配置文件:
- file./config/: 当前项目下的/config目录下(在idea未打包前是resources下)
- file:./ :当前项目的根目录
- classpath:/config/: classpath的/config目录(最上级父工程目录 v)
- classpath:/ : classpath的根目录
加载顺序为上文的排列顺序,高优先级配置的属性会生效
外部配置加载
可以使用--spring.config.location=d://asdasda/asdasdas/asdasd
命令行参数来指定一个外部配置文件
和jar包平级的application.properties以及同级的config目录下的application.properies也会被识别,可以进行一个互补的操作
常用配置
# 修改端口
server.port=8080
# 修改项目路径
server.servlet.content-path=/hello
整合框架
Junit整合
-
创建工程
-
引用起步依赖
-
编写测试类
-
填写测试注解
- @RunWith
- @SpringBootTest
-
编写测试方法
现在新版的似乎不用加runwith,假如是同级的,只需要在测试类上加
@SpringBootTest
,方法上使用junit的@Test
注解即可。
整合Redis
待续
整合MyBatis
- 搭建SpringBoot工程
- 引入mybatis起步依赖,添加mysq|驱动
- 编写DataSource和MyBatis相关配置
- 定义表和实体类
- 编写dao和mapper文件/纯注解开发
- 测试
application.yaml
# datasource
spring:
datasource:
url: jdbc:mysql://bj-cynosdbmysql-grp-2ny5n7eg.sql.tencentcdb.com:21436/MyBatisLearing?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username:
password:
driver-class-name: com.mysql.jdbc.Driver
# mybatis
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml #mapper目录
type-aliases-package: com.sunspot._03mybatis.pojo #别名扫描
# config-location: mybatis的核心配置文件,这里暂时没用 QAQ
之后正常写就可以了捏