掌握的步骤总结
1. 什么是Springboot以及Springboot的特点。
2. 快速搭建springboot项目
3. springboot常用的配置文件类型.
4. 读取springboot配置文件的内容
5. springboot整合数据源。
1. 什么是Springboot以及Springboot的特点。
1.1 什么是springboot?
Spring Boot 是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程 .
理解:spring框架搭建的步骤:[1]依赖 [2]配置文件。 使用springboot可以简化上面的两个步骤。
1.2 springboot的特点
① 创建独立的 Spring 应用程序
② 嵌入的 Tomcat,无需部署 WAR 文件
③ 简化 Maven 配置
④ 自动配置 Spring
⑤ 提供生产就绪型功能,如指标,健康检查和外部配置
⑥ 开箱即用,没有代码生成,也无需 XML 配置。
2. idea快速搭建springboot工程
2.1 有网络的情况
创建一个controller包
2.2 无网络的情况
3.pringboot常用的配置文件类型.
properties和yml格式。他们的区别就是格式上不同。
properties格式如下:
yml结构
不管使用哪个配置文件,他们的名字必须叫application.
如果上面两个配置文件同时存在,而且里面有相同的配置。
则properties优先级高于yml优先级。
4. java如何读取配置文件中的内容
java为什么需要读取配置文件的内容,我们开发时需要把哪些内容放入配置文件。
OSS:上传文件。accessKeyId,accessKeySecret等,这些内容能写在java源代码中。
硬编码文件,不利维护。 我们需要把信息写入配置文件。
读取方式有两种:
第一种方式: 在类上@ConfigurationProperties(prefix="")
第二种使用@Value读取属性:---他只能读取基本类型和String类型。加在属性上
5. springboot整合数据源
druid数据源: ----连接数据库
(1)引入相关的依赖。
<!--mysql的驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
(2)配置数据源信息
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://localhost:3306/db1
spring.datasource.druid.username=root
spring.datasource.druid.password=123456
spring.datasource.druid.initial-size=6
(3)测试
@SpringBootTest
class Springboot01ApplicationTests {
@Autowired
private DataSource dataSource;
@Test
void contextLoads() throws Exception{
System.out.println(dataSource);
}
}