SpringBoot

概述

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

2、Spring程序缺点

  • 配置繁琐
  • 依赖设置繁琐
    3、SpringBoot程序优点
  • 自动配置
  • 起步依赖(简化依赖配置)
  • 辅助功能(内置服务器,……)

使用

1、新建一个spring项目,导入sprinboot 依赖

 <!--版本控制-->
    <parent>
        <artifactId>spring-boot-dependencies</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.4.5</version>
    </parent>
    <dependencies>
        <!--起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

2、编写启动类·

package soLo.iu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

整合第三方

整合JUnit

  1. 创建spring项目,导入springboot起步依赖
<!--版本控制-->
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
       <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

2 导JUnit依赖

  <!--junit起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

2、编写测试类


SSM整合

  • SpringBoot整合Spring(不存在)
  • SpringBoot整合SpringMVC(不存在)
  • SpringBoot整合MyBatis(主要)

基于springboot整合ssm主要整合的是MyBatis

  1. 导入mybtis依赖
 <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
  1. 导入mysql依赖
 <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--druid连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.4</version>
        </dependency>
  1. 设置数据源参数
spring:
 datasource:
   type: com.alibaba.druid.pool.DruidDataSource
   driver-class-name: com.mysql.cj.jdbc.Driver
   url: jdbc:mysql://localhost:3306/demo?serverTimezone=UTC
   username: root
   password: 1234
  1. 定义数据层接口与映射配置
@Mapper
public interface BrandMapper {

    Brand selectOneById(int id);
}

springboot配置文件

SpringBoot提供了多种属性配置方式,通常有:

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

加载顺序

  • application.properties > application.yml > application.yaml

注意事项:

  1. SpringBoot核心配置文件名为application
  2. SpringBoot内置属性过多,且所有属性集中在一起修改,在使用时,通过提示键+关键字修改属性

多环境开发配置

多环境启动配置
  • yaml文件多环境启动
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
  • properties文件多环境启动
#主启动配置文件 application.properties
spring.profiles.active=pro
#环境分类配置文件 application-pro.properties
server.port=80
#环境分类配置文件 application-dev.properties
server.port=81
#环境分类配置文件application-test.properties
server.port=82
多环境开发控制

Maven与SpringBoot多环境兼容(步骤)

  1. Maven中设置多环境属性
<profiles>
    <profile>
        <id>dev_env</id>
        <properties>
            <profile.active>dev</profile.active>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>pro_env</id>
        <properties>
            <profile.active>pro</profile.active>
        </properties>
    </profile>
    <profile>
        <id>test_env</id>
        <properties>
            <profile.active>test</profile.active>
        </properties>
    </profile>
</profiles>
  1. SpringBoot中引用Maven属性
    在这里插入图片描述

  2. 执行Maven打包指令

  • Maven指令执行完毕后,生成了对应的包,其中类参与编译,但是配置文件并没有编译,而是复制到包中
    在这里插入图片描述
  • 解决思路:对于源码中非java类的操作要求加载Maven对应的属性,解析${}占位符
  1. 对资源文件开启对默认占位符的解析
<build>
    <plugins>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <configuration>
                <encoding>utf-8</encoding>
                <useDefaultDelimiters>true</useDefaultDelimiters>
            </configuration>
        </plugin>
    </plugins>
</build>
  • Maven打包加载到属性,打包顺利通过
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值