SpringBoot

目录

SpringBoot 简介

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

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

    • 配置繁琐
    • 依赖设置繁琐
  • SpringBoot 程序优点恰巧就是针对 Spring 的缺点

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

    • SpringBoot 中常见项目名称,定义了当前项目使用的所有项目坐标,以达到减少依赖配置的目的
  • parent

    • 所有 SpringBoot 项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的

    • spring-boot-starter-parent(2.5.0)与 spring-boot-starter-parent(2.4.6)共计57处坐标版本不同

  • 引导类

java 复制代码
@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` 启动咱们的工程。

配置文件

配置文件格式

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

application.properties

yaml 复制代码
server.port=80

application.yml

yaml 复制代码
server:
	port: 81

application.yaml

yaml 复制代码
server:
	port: 82
  • SpringBoot 程序的配置文件名必须是 application ,只是后缀名不同而已。

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

    application.properties > application.yml > application.yaml

yaml格式

  • YAML(YAML Ain't Markup Language),一种数据序列化格式

优点:

  • 容易阅读

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

  • 容易与脚本语言交互

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

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

YAML 文件扩展名:

  • .yml (主流)
  • .yaml

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

命令行启动参数设置

yaml 复制代码
java --jar xxx.jar ---spring.profiles.active=test
  • 配置优先级

配置文件分类

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

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

  • 1级:classpath:application.yml
  • 2级:classpath:config/application.yml
  • 3级:file :application.yml
  • 4级:file :config/application.yml

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

SpringBoot整合junit

回顾 Spring 整合 junit

java 复制代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class UserServiceTest {
    
    @Autowired
    private BookService bookService;
    
    @Test
    public void testSave(){
        bookService.save();
    }
}

使用 @RunWith 注解指定运行器,使用 @ContextConfiguration 注解来指定配置类或者配置文件。而 SpringBoot 整合 junit 特别简单,分为以下三步完成

  • 在测试类上添加 SpringBootTest 注解
  • 使用 @Autowired 注入要测试的资源
  • 定义测试方法进行测试

SpringBoot 整合 Mybatis

  • Spring Boot推崇 " 约定大于配置 "
  1. 定义实体类
  2. 定义dao接口
    Mybatis 会扫描接口并创建接口的代码对象交给 Spring 管理,但是现在并没有告诉 Mybatis 哪个是 dao 接口。而我们要解决这个问题需要在BookDao 接口上使用 @Mapper
java 复制代码
@Mapper
public interface BookDao {
    @Select("select * from tbl_book where id = #{id}")
    public Book getById(Integer id);
}
  1. 定义测试类
  2. 编写配置
java 复制代码
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm_db
    username: root
    password: root
  1. 使用Druid数据源
  • 导入依赖
java 复制代码
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.16</version>
</dependency>
  • application.yml 配置文件配置

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

java 复制代码
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

SpringBoot 整合 SSM

  1. pom.xml

    配置起步依赖,必要的资源坐标(druid)

  2. application.yml

    设置数据源、端口等

  3. 配置类

    全部删除

  4. dao

    设置@Mapper

  5. 测试类

  6. 页面

    放置在resources目录下的static目录中

相关推荐
六毛的毛11 分钟前
Springboot开发常见注解一览
java·spring boot·后端
AntBlack18 分钟前
拖了五个月 ,不当韭菜体验版算是正式发布了
前端·后端·python
315356691321 分钟前
一个简单的脚本,让pdf开启夜间模式
前端·后端
程序漫游人24 分钟前
centos8.5安装jdk21详细安装教程
java·linux
uzong1 小时前
curl案例讲解
后端
超级码.里奥.农1 小时前
零基础 “入坑” Java--- 七、数组(二)
java·开发语言
hqxstudying1 小时前
Java创建型模式---单例模式
java·数据结构·设计模式·代码规范
挺菜的1 小时前
【算法刷题记录(简单题)002】字符串字符匹配(java代码实现)
java·开发语言·算法
A__tao1 小时前
一键将 SQL 转为 Java 实体类,全面支持 MySQL / PostgreSQL / Oracle!
java·sql·mysql
开开心心就好1 小时前
免费PDF处理软件,支持多种操作
运维·服务器·前端·spring boot·智能手机·pdf·电脑