黑马程序员SSM_SpringBoot笔记(自用)

SpringBoot_1_简介

1.入门案例

  1. 新建模块
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
  2. 开发控制器类
    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!";
    }
}
  1. 启动服务器
    运行 SpringBoot 工程不需要使用本地的 Tomcat 和 插件,只运行项目包下的 Application 类,我们就可以在控制台看出如下信息
    在这里插入图片描述
  2. 使用 Postman 工具来测试我们的程序
  3. 对比
    做完 SpringBoot 的入门案例后,接下来对比一下 Spring 程序和 SpringBoot 程序。如下图
    在这里插入图片描述
  • 坐标

    Spring 程序中的坐标需要自己编写,而且坐标非常多

    SpringBoot 程序中的坐标是我们在创建工程时进行勾选自动生成的

  • web3.0配置类

    Spring 程序需要自己编写这个配置类。这个配置类大家之前编写过,肯定感觉很复杂

    SpringBoot 程序不需要我们自己书写

  • 配置类

    Spring/SpringMVC 程序的配置类需要自己书写。而 SpringBoot 程序则不需要书写。

注意:基于Idea的 Spring Initializr 快速构建 SpringBoot 工程时需要联网。

2.官网构建工程(不用idea构建工程)

  1. 进入springboot官网
  • 然后点击 Spring Initializr 超链接就会跳转到如下页面
    在这里插入图片描述
  • 打开下载好的压缩包可以看到工程结构和使用 Idea 生成的一模一样

3.SpringBoot工程快速启动

  1. 打包
    由于我们在构建 SpringBoot 工程时已经在 pom.xml 中配置了如下插件
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

所以我们只需要使用 Mavenpackage 指令打包就会在 target 目录下生成对应的 Jar 包。
在这里插入图片描述
2. 启动
进入 jar 包所在位置,在 命令提示符 中输入如下命令

jar -jar springboot_01_quickstart-0.0.1-SNAPSHOT.jar
//输入jar -jar s再按tab键自动补全

执行上述命令就可以看到 SpringBoot 运行的日志信息
在这里插入图片描述

4.SpringBoot概述

SpringBoot 是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程

大家已经感受了 SpringBoot 程序,回过头看看 SpringBoot 主要作用是什么,就是简化 Spring 的搭建过程和开发过程。

原始 Spring 环境搭建和开发存在以下问题:

  • 配置繁琐
  • 依赖设置繁琐

SpringBoot 程序优点恰巧就是针对 Spring 的缺点

  • 自动配置。这个是用来解决 Spring 程序配置繁琐的问题
  • 起步依赖。这个是用来解决 Spring 程序依赖设置繁琐的问题
  • 辅助功能(内置服务器,…)。我们在启动 SpringBoot 程序时既没有使用本地的 tomcat 也没有使用 tomcat 插件,而是使用 SpringBoot 内置的服务器。

1.起步依赖

  • 我们使用 Spring Initializr 方式创建的 Maven 工程的的 pom.xml 配置文件中自动生成了很多包含 starter 的依赖,如下图
    在这里插入图片描述
  • 这些就是启动依赖,它定义了当前项目使用的所有项目坐标,以达到减少依赖配置的目的
  • parent:所有 SpringBoot 项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的

2.程序启动

  • 创建的每一个 SpringBoot 程序时都包含一个类似于下面的类,我们将这个类称作引导类
@SpringBootApplication
public class Springboot01QuickstartApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(Springboot01QuickstartApplication.class, args);
    }
}

注意:

  • SpringBoot 在创建项目时,采用jar的打包方式

  • SpringBoot 的引导类是项目的入口,运行 main 方法就可以启动项目

    因为我们在 pom.xml 中配置了 spring-boot-starter-web 依赖,而该依赖通过前面的学习知道它依赖 tomcat ,所以运行 main 方法就可以使用 tomcat 启动咱们的工程。

    3.切换web服务器

    1. 现在我们启动工程使用的是 tomcat 服务器,那能不能不使用 tomcat 而使用 jetty 服务器,jetty 在我们 maven 高级时讲 maven 私服使用的服务器。而要切换 web 服务器就需要将默认的 tomcat 服务器给排除掉,怎么排除呢?使用 exclusion 标签
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
    </exclusions>
</dependency>
  1. 引入 jetty 服务器。在 pom.xml 中因为 jetty 的起步依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

SpringBoot_2_配置文件

1.配置文件格式

  • 我们现在启动服务器默认的端口号是 8080,但在线上环境我们还是希望将端口号改为 80,这样在访问的时候就可以不写端口号了,修改方式如下:
http://localhost/books/1
  • SpringBoot程序修改方式:
  • application.properties
server.port=80
  • application.yml
server:
	port: 81
  • application.yaml
server:
	port: 82

注意:SpringBoot 程序的配置文件名必须是 application ,只是后缀名不同而已
在开发过程中,配置文件中如果没有提示,可以使用以下方式

  1. 点击 File 选中 Project Structure
    在这里插入图片描述

  2. 弹出如下窗口,按图中标记红框进行选择
    在这里插入图片描述

  3. 通过上述操作,会弹出如下窗口
    在这里插入图片描述

  4. 点击上图的 + 号,弹出选择该模块的配置文件
    在这里插入图片描述

  5. 通过上述几步后,就可以看到如下界面。properties 类型的配合文件有一个,ymal 类型的配置文件有两个

  6. 这三种配置的优先级为:application.properties > application.yml > application.yaml

2.yaml格式

1.yaml格式优点

**YAML(YAML Ain’t Markup Language),一种数据序列化格式。**这种格式的配置文件在近些年已经占有主导地位,这种配置文件和前期使用的配置文件是有一些优势的.
优点:

  • 容易阅读

    yaml 类型的配置文件比 xml 类型的配置文件更容易阅读,结构更加清晰

  • 容易与脚本语言交互

  • 以数据为核心,重数据轻格式

    yaml 更注重数据,而 xml 更注重格式

YAML 文件扩展名:

  • .yml (主流)
  • .yaml

上面两种后缀名都可以,以后使用更多的还是 yml 的。

2.yaml格式语法规则

  • 大小写敏感

  • 属性层级关系使用多行描述,每行结尾使用冒号结束

  • 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)

    空格的个数并不重要,只要保证同层级的左侧对齐即可。

  • 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)

  • # 表示注释

核心规则:数据前面要加空格与冒号隔开

3.yaml配置文件数据读取

1.使用 @Value注解

  • 使用 @Value("表达式") 注解可以从配合文件中读取数据,注解中用于读取属性名引用方式是:${一级属性名.二级属性名……}
  • 例如:我们可以在 BookController 中使用 @Value 注解读取配合文件数据,如下
@RestController
@RequestMapping("/books")
public class BookController {
    
    @Value("${lesson}")
    private String lesson;
    @Value("${server.port}")
    private Integer port;
    @Value("${enterprise.subject[0]}")
    private String subject_00;

    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println(lesson);
        System.out.println(port);
        System.out.println(subject_00);
        return "hello , spring boot!";
    }
}

2.Environment对象

  • 上面方式读取到的数据特别零散,SpringBoot 还可以使用 @Autowired 注解注入 Environment 对象的方式读取数据。这种方式 SpringBoot 会将配置文件中所有的数据封装到 Environment 对象中,如果需要使用哪个数据只需要通过调用 Environment 对象的 getProperty(String name) 方法获取。具体代码如下:
@RestController
@RequestMapping("/books")
public class BookController {
    
    @Autowired
    private Environment env;
    
    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println(env.getProperty("lesson"));
        System.out.println(env.getProperty("enterprise.name"));
        System.out.println(env.getProperty("enterprise.subject[0]"));
        return "hello , spring boot!";
    }
}

注意:这种方式,框架内容大量数据,而在开发中我们很少使用。

3.自定义对象

SpringBoot 还提供了将配置文件中的数据封装到我们自定义的实体类对象中的方式。具体操作如下:

  • 将实体类 bean 的创建交给 Spring 管理。

    在类上添加 @Component 注解

  • 使用 @ConfigurationProperties 注解表示加载配置文件

    在该注解中也可以使用 prefix 属性指定只加载指定前缀的数据

  • BookController 中进行注入,具体代码如下:

//Enterprise内容如下:
@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {
    private String name;
    private int age;
    private String tel;
    private String[] subject;
    getter...
    setter...
    toString...
    }
    
//BookController内容如下:
@RestController
@RequestMapping("/books")
public class BookController {
    
    @Autowired
    private Enterprise enterprise;

    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println(enterprise.getName());
        System.out.println(enterprise.getAge());
        System.out.println(enterprise.getSubject());
        System.out.println(enterprise.getTel());
        System.out.println(enterprise.getSubject()[0]);
        return "hello , spring boot!";
    }
}

注意:
使用第三种方式,在实体类上有如下警告提示
在这里插入图片描述
这个警告提示解决是在 pom.xml 中添加如下依赖即可

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

4.多环境配置

1.yaml文件配置

application.yml 中使用 --- 来分割不同的配置,内容如下

#启用的配置
spring:
  profiles:
    active: dev  #表示使用的是开发环境的配置
---
#开发
spring:
  profiles: dev #给开发环境起的名字
server:
  port: 80
---
#生产
spring:
  profiles: pro #给生产环境起的名字
server:
  port: 81
---
#测试
spring:
  profiles: test #给测试环境起的名字
server:
  port: 82
---

上述配置中的spring.profiles 配置项已经过时。最新用来起名字的配置项是

#开发
spring:
  config:
    activate:
      on-profile: dev

2. properties文件

properties 类型的配置文件配置多环境需要定义不同的配置文件

  • application-dev.properties 是开发环境的配置文件。我们在该文件中配置端口号为 80
server.port=80
  • application-test.properties 是测试环境的配置文件。我们在该文件中配置端口号为 81
server.port=81
  • application-pro.properties 是生产环境的配置文件。我们在该文件中配置端口号为 82
server.port=82

3.多环境启动命令格式

1.方式
  • 使用 SpringBoot 开发的程序以后都是打成 jar 包,通过 java -jar xxx.jar 的方式启动服务的.而通过命令行有以下三种方式来指定环境:
  1. SpringBoot 提供了在运行 jar 时设置开启指定的环境的方式
java –jar xxx.jar –-spring.profiles.active=test
  1. 指定端口号
java –jar xxx.jar –-server.port=88
  1. 同时设置多个配置,比如即指定启用哪个环境配置,又临时指定端口
java –jar springboot.jar –-server.port=88 –-spring.profiles.active=test
2.在打包到指定环境启动容易遇见的坑
  1. 在执行package命令前执行一次clean命令,防止之前执行结果影响打包
  2. 文件编码问题可能导致打包失败,去setting->Editor->File Encoding将Project Encoding和Default encoding for properties files改为utf-8即可

4.多环境开发兼容问题(maven和boot兼容)

  1. Maven中设置多环境配置
 <profiles>
        <!--开发环境-->
        <profile>
            <id>dev</id>
            <properties>
                <profile.active>dev</profile.active>
            </properties>
        </profile>
        <!--生产环境-->
        <profile>
            <id>pro</id>
            <properties>
                <profile.active>pro</profile.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <!--测试环境-->
        <profile>
            <id>test</id>
            <properties>
                <profile.active>test</profile.active>
            </properties>
        </profile>
</profiles>
  1. SpringBoot中也设置了多环境,要使他们兼容,得让配置文件读取maven配置的多环境
#设置启用的环境
spring:
  profiles:
    active: ${profile.active}

---
#开发
spring:
  profiles: dev
server:
  port: 80
---
#生产
spring:
  profiles: pro
server:
  port: 81
---
#测试
spring:
  profiles: test
server:
  port: 82
---

  1. 设置完成后就会加载profile.active这个属性了
    在这里插入图片描述
  • 最终运行时配置文件中的${profile.active}会被修改为pom文件中指定的pro了
  1. 但这样直接打包仍会失败,因为Maven执行完毕后,其中类参与编译,但是配置文件没有编译,而是复制到包中
    在这里插入图片描述
  • 因此我们要对于源码中非java类的操作要求加载Maven对应的属性,解析${}占位符
  • pom文件中添加插件
 <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>

5.配置文件分类

SpringBoot 定义了配置文件不同的放置的位置;而放在不同位置的优先级时不同的。

SpringBoot 中4级配置文件放置位置:

  • 1级:classpath:application.yml
    • resources 下创建的 application.yml 配置文件
  • 2级:classpath:config/application.yml
    • resources 下创建一个名为 config 的目录,在该目录中创建 application.yml 配置文件
  • 3级:file :application.yml
    • jar 包所在位置创建 application.yml 配置文件
  • 4级:file :config/application.yml
    • jar 包所在位置创建 config 文件夹,在该文件夹下创建 application.yml 配置文件
  • 作用:
    • 1级与2级留做系统打包后设置通用属性
    • 3级和4级用于系统开发阶段设置通用属性

**说明:**级别越高优先级越高

SpringBoot_3_整合

1.SpringBoot整合junit

  • test/java 下创建 com.itheima 包,在该包下创建测试类,将 BookService 注入到该测试类中
@SpringBootTest//第一步,加上该注解
class Springboot07TestApplicationTests {

    @Autowired//第二步,将要测试的bean自动装配
    private BookService bookService;

    @Test//第三步,测试
    public void save() {
        bookService.save();
    }
}

2.SpringBoot整合mybatis

  1. 创建新模块,选择spring初始化,并配置模块相关基础信息
    在这里插入图片描述

  2. 选择当前模块需要的技术集
    在这里插入图片描述

  3. 在定义好实体类,dao接口后定义测试类

@SpringBootTest
class Springboot08MybatisApplicationTests {

	@Autowired
	private BookDao bookDao;

	@Test
	void testGetById() {
		Book book = bookDao.getById(1);
		System.out.println(book);
	}
}
  1. 设置数据源参数
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
    username: root
    password: root
  1. 给dao接口加上@Mapper注解
  • Mybatis 会扫描接口并创建接口的代码对象交给 Spring 管理,但是现在并没有告诉 Mybatis 哪个是 dao 接口。而我们要解决这个问题需要在Dao 接口上使用 @Mapper ,例如
@Mapper
public interface BookDao {
    @Select("select * from tbl_book where id = #{id}")
    public Book getById(Integer id);
}
  1. 使用Druid数据源
  • 导入 Druid 依赖
  • application.yml 配置文件配置
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值