SpringBoot自学笔记《黑马程序员》

SpringBoot

SpringBoot简介

SpringBoot是用来简化 Spring 应用的初始搭建以及开发过程

Spring框架开发,我们首先需要创建工程,然后在pom.xml中导入需要的依赖坐标(servlet、mybatis、spring-webmvc等相关依赖),然后创建配置类,编写SpringConfig、ServletConfig、JdbcConfig、MybatisConfig以及SpringmvcConfig等相关文件代码,然后创建接口,编写dao层、service层代码实现接口,到这里整个工程的大体框架就已经搭建完毕,要想被外界访问,还需要编写Controller类,在控制类中提供方法。

SpringBoot是对控制类之前的步骤(搭建环境)进行整合简化。

SpringBoot相比于Spring优点:

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

前后端协同开发的时候,后端可以将SpringBoot项目打成jar包,前端人员可以不需要开发工具,只需要连接相同的数据库即可使用。关于maven打包问题在SSM框架专栏有详细讲解。

在这里插入图片描述

打包之后,寻找target目录下打包好的jar包,用cmd命令行的方法运行:

java -jar springboot_01.jar

SpringBoot开发步骤

  • 创建新模块,选择Spring初始化,并配置模块相关基础信息
  • 选择当前模块需要使用的技术集
  • 开发控制器类(创建Controller包)
  • 运行自动生成的Application类

具体操作:

点击 + 选择 New Module 创建新模块

在这里插入图片描述

选择 Spring Initializr ,用来创建 SpringBoot 工程

在这里插入图片描述

SpringBoot 工程进行相关的设置

在这里插入图片描述

注意:打包方式这里需要设置为 Jar

选中 Web,然后勾选 Spring Web

在这里插入图片描述

至此,一个SpringBoot工程就创建完成了。

SpringBoot配置文件

属性配置

SpringBoot提供了多种属性配置方式:

  • application.properties
server.port=80
  • application.yml
server:
	port: 81
  • application.yaml
server:
	port: 82

注意:SpringBoot 程序的配置文件名必须是 application,只是后缀名不同而已。

冒号后面必须有空格!!!

三种配置文件的优先级是:

application.properties > application.yml > application.yaml

YAML格式

YAML 文件扩展名:

  • .yml (主流)
  • .yaml

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

优点:

  • 容易阅读

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

  • 容易与脚本语言交互

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

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

语法规则

  • 大小写敏感

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

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

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

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

  • # 表示注释

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

数组数据在数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分隔,例如:

enterprise:
  name: itcast
  age: 16
  tel: 4006184000
  subject:
    - Java
    - 前端
    - 大数据

YAML读取

在controller中读取配置数据有三种方式:

1、使用 @Value注解

使用 @Value(“表达式”) 注解可以从配合文件中读取数据,注解中用于读取属性名引用方式是:${一级属性名.二级属性名……}

2、Environment对象

使用 @Value注解读取到的数据特别零散,SpringBoot 还可以使用 @Autowired 注解注入 Environment 对象的方式读取数据。这种方式 SpringBoot 会将配置文件中所有的数据封装到 Environment 对象中,如果需要使用哪个数据只需要通过调用 Environment 对象的 getProperty(String name) 方法获取。

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;
    //Setter and Getter
    //toString方法
}
@RestController
@RequestMapping("/books")
public class BookController {
    //yaml读取数据第一种方法:value
    @Value("${lesson}")
    private String lesson;
    @Value("${server.port}")
    private Integer port;
    @Value("${enterprise.subject[0]}")
    private String subject_00;

    //yaml读取数据第二种方法:environment对象
    @Autowired
    private Environment environment;

    //yaml读取数据第三种方法:自定义对象
    @Autowired
    private Enterprise enterprise;

    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
//        System.out.println("id ==> "+id);
        System.out.println(lesson);
        System.out.println(port);
        System.out.println(subject_00);
        System.out.println("---------------------");
        System.out.println(environment.getProperty("lesson"));
        System.out.println(environment.getProperty("server.port"));
        System.out.println(environment.getProperty("enterprise.tel"));
        System.out.println(environment.getProperty("enterprise.subject[1]"));
        System.out.println("---------------------");
        System.out.println(enterprise);

        return "hello , spring boot!";
    }
}

多环境配置

以后在工作中,对于开发环境、测试环境、生产环境的配置肯定都不相同,比如我们开发阶段会在自己的电脑上安装 mysql ,连接自己电脑上的 mysql 即可,但是项目开发完毕后要上线就需要该配置,将环境的配置改为线上环境的。

在这里插入图片描述

来回的修改配置会很麻烦,而 SpringBoot 给开发者提供了多环境的快捷配置,需要切换环境时只需要改一个配置即可。不同类型的配置文件多环境开发的配置都不相同,接下来对不同类型的配置文件进行说明

application.yml 配置文件内容如下(使用 --- 来分割不同的配置):

#设置启用的环境
spring:
  profiles:
    active: dev

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

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

application-dev.properties 是开发环境的配置文件。我们在该文件中配置端口号为 80

application-test.properties 是测试环境的配置文件。我们在该文件中配置端口号为 81

application-pro.properties 是生产环境的配置文件。我们在该文件中配置端口号为 82

SpringBoot 只会默认加载名为 application.properties 的配置文件,所以需要在 application.properties 配置文件中设置启用哪个配置文件,配置如下:

spring.profiles.active=pro

配置文件级别

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

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

  • 1级:classpath(类路径):application.yml
  • 2级:classpath:config/application.yml
  • 3级:file (jar包所在位置创建文件夹):application.yml
  • 4级:file :config/application.yml (最高)

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

SpringBoot整合junit

创建BookService接口及其实现类,编写测试类,将 BookService 注入到该测试类中:

@SpringBootTest
class Springboot01TestApplicationTests {

    @Autowired
    private BookService bookService;

    @Test
    public void save() {
        bookService.save();
    }
}

注意:这里的引导类所在包必须是测试类所在包及其子包。

例如:

  • 引导类所在包是 com.itheima
  • 测试类所在包是 com.itheima

如果不满足这个要求的话,就需要在使用 @SpringBootTest 注解时,使用 classes 属性指定引导类的字节码对象。如 @SpringBootTest(classes = Springboot01TestApplication.class)

SpringBoot整合mybatis

创建模块

1、创建新模块,选择 Spring Initializr,并配置模块相关基础信息

在这里插入图片描述

在这里插入图片描述

定义实体类、dao接口及测试类

com.itheima.domain 包下定义实体类 Book,内容如下:

public class Book {
    private Integer id;
    private String name;
    private String type;
    private String description;
    //setter and  getter
    //toString
}

com.itheima.dao 包下定义 BookDao 接口,内容如下:

public interface BookDao {
    @Select("select * from tbl_book where id = #{id}")
    public Book getById(Integer id);
}

test/java 下定义包 com.itheima ,在该包下测试类,内容如下:

@SpringBootTest
class Springboot08MybatisApplicationTests {
	@Autowired
	private BookDao bookDao;
	@Test
	void testGetById() {
		Book book = bookDao.getById(1);
		System.out.println(book);
	}
}

编写配置

application.yml 配置文件中配置如下内容,连接数据库:

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm_db
    username: root
    password: root

测试

运行测试方法,我们会看到如下错误信息

在这里插入图片描述

错误信息原因是 Mybatis 会扫描接口并创建接口的代码对象交给 Spring 管理,但是现在并没有告诉 Mybatis 哪个是 dao 接口。而我们要解决这个问题需要在BookDao 接口上使用 @MapperBookDao 接口改进为

@Mapper
public interface BookDao {
    @Select("select * from tbl_book where id = #{id}")
    public Book getById(Integer id);
}

使用Druid数据源

上述操作并没有指定数据源,SpringBoot 有默认的数据源,我们也可以指定使用 Druid 数据源,按照以下步骤实现:

1、导入 Druid 依赖

2、在 application.yml 配置文件配置

可以通过 spring.datasource.type 来配置使用什么数据源。配置文件内容可以改进为:

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
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

科研达人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值