SpringBoot
1. springBoot简介
-
作用:springboot是用来简化spring的初始搭建和开发过程的
-
spring存在的问题
- 依赖繁琐
- 集成繁琐
-
springboot的好处
- 起步依赖(简化依赖)
- 简化配置
- 内置服务器
2. springBoot主要功能
- parent(规定了依赖版本 )
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.4</version> <relativePath/> <!-- lookup parent from repository --> </parent>
-
开发SpringBoot程序要继承spring-boot-starter-parent
-
spring-boot-starter-parent中定义了若干个依赖管理
-
继承parent模块可以避免多个依赖使用相同技术时出现依赖版本冲突
-
继承parent的形式也可以采用引入依赖的形式实现效果
- starter(真正定义了一组所需要的依赖)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
- 使用任意坐标时,仅书写GAV中的G和A,V由SpringBoot提供,除非parent未提供对应版本,如发生坐标错误,再指定Version(要小心版本冲突)
- 引导类
@SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
- SpringBoot的引导类是Boot工程的执行入口,运行main方法就可以启动项目
- SpringBoot工程运行后初始化Spring容器,扫描引导类所在包加载bean
- 内置tomcat
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!--依赖排除--> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
- 内置服务器
- tomcat(默认) apache出品,粉丝多,应用面广,负载了若千较重的组件
- jetty 更轻量级,负载性能远不及tomcat
- undertow 负载性能勉强跑赢tomcat
3. 补充REST风格
-
简介:REST(Representational State Transfer),表现形式状态转换
传统风格资源描述形式
http://localhost/user/getById?id=1
http://localhost/user/saveUserREST风格描述形式
http://localhost/user/1
http://localhost/user-
优点:
-
隐藏资源的访问行为,无法通过地址得知对资源是何种操作
-
书写简化
-
按照REST风格访问资源时使用行为动作区分对资源进行了何种操作
请求路径 作用 请求方式 http://localhost/users/1 查询全部用户信息 GET(查询) http://localhost/users/1 查询指定用户信息 GET(查询) http://localhost/users 添加用户信息 P0ST(新增/保存) http://localhost/users 修改用户信息 PUT(修改/更新) http://localhost/users/1 删除用户信息 DELETE(删除) ps:上述行为是约定方式,约定不是规范,可以打破,所以称REST风格,而不是REST规范
-
-
4. 基础配置
- 属性配置
- SpringBoot默认配置文件
properties(优先级最高)
# 应用名称 spring.application.name=springBoot-Rest # 应用服务 WEB 访问端口 server.port=8080 # 启动图标 #spring.banner.image.location=R-C.jpg # 日志 logging.level.root=error
YAML
简介:YAML(YAML Ain’t Markup Language),一种数据序列化格式
容易阅读
容易与脚本语言交互
以数据为核心,重数据轻格式
application.yaml
#配置数据库的连接信息 spring: datasource: url: jdbc:mysql://localhost:3306/idea_day1?useUnicode=true&characterEncoding=utf8 username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver #给实体起别名,设置映射文件的路径。 mybatis: type-aliases-package: com.baizhi.entity mapper-locations: classpath:com/baizhi/dao/*.xml
application.yml(与.yaml都是YAML的一种实现方式)
语法要求:
- 大小写敏感
- 属性层级关系使用多行描述,每行结尾使用冒号结束
- 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tb键)
- 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
- #表示注释
Java程序读取yml文件
- 第一步
# 例如 lzy: aaa
- 第二步
@Value("${lzy}") String temp;
获取所有yml属性
enterprise: name:itcast age:16 te1:4006184000 subject: -Java -前端
class TestController{ //Environment 包含了yml里面的所有属性 @Autowired Environment environment; public void method (){ //通过变量名 environment.getProperty("enterprise.itcast"); environment.getProperty("enterprise.subject[0]"); } }
获取所有yml中某个对象
datasource: driver: "com.mysql.cj.jdbc.Driver" url: jdbc:mysql://localhost:3306/db_employee name: root password: root
//可以映射到任意的对象 @Data @AllArgsConstructor @NoArgsConstructor @Component @ConfigurationProperties("datasource") public class YmlObj { private String driver; private String url; private String name; private String password; }
yml语法细节
1. 变量引用 windowsDir: d:/abc # 通过El表达式进行引用 windowsDirtemp: ${windowsDir}/efg 2. 转义字符 属性值中如果出现转移字符,需要使用双引号包裹 lesson: "Spring\tboot\nlesson"
5. 整合第三方技术
-
整合JUnit
-
从模板创建项目不需要对junit做任何操作
-
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
-
@SpringBootTest //如果测试类与启动类不在一个包里面需要指定启动类位置 //@ContextConfiguration(classes="位置")或者@SpringBootTest(classes="位置") class SpringBootRestApplicationTests { @Test void contextLoads() { } }
-
-
整合MyBatis
-
核心配置:数据库连接相关信息(连什么?连谁?什么权限)
-
映射配置:SQL映射(ML/注解)
-
<!--mybatis跟springboot集成的依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> <!--mybatis要链接mysql数据库,mysql数据库的驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> 前提要先引入mybatis的依赖
-
#配置数据库的连接信息 spring: datasource: url: jdbc:mysql://localhost:3306/idea_day1?useUnicode=true&characterEncoding=utf8 username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver #给实体起别名,设置映射文件的路径。 mybatis: type-aliases-package: com.baizhi.entity mapper-locations: classpath:com/baizhi/dao/*.xml
-
-
整合MyBatis-PIus
-
#设置Mp相关的配置设置前缀 mybatis-plus: global-config: db-config: table-prefix: tb1_
-
-
整合Druid
-
依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.6</version> </dependency>
-
配置
spring: datasource: druid: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/db_employee?useUnicode=true username: root password: root
-