SpringBoot知识点

入门案例

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

        2、选择当前模块需要使用的技术集

        3、开发控制器类

@RestController
@RequestMapping("/books")
public class BookController {

    @RequestMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println("id ==>" + id);
        return "hello, spring boot";
    }
}

        4、运行自动生成的Application类

        最简SpringBoot程序所包含的基础文件

                pom.xml文件

                Application类

        Spring程序与SpringBoot程序对比

类/配置文件                                Spring                        SpringBoot

pom文件中的坐标                      手工添加                    勾选添加

web3.0配置类                             手工添加                    无

Spring/SpringMVC配置类           手工添加                    无

控制器                                         手工添加                    手工添加

                基于idea开发SpringBoot程序需要确保联网且能够加载到程序框架结构

                        可直接在Spring官网创建SpringBoot项目

        SpringBoot项目快速启动

                1、对SpringBoot项目打包(执行Maven构建指令p3、ackage)

                2、执行启动指令

                        java -jar springboot.jar

注意事项:

        jar支持命令行启动需要依赖maven插件支持,请确认打包时是否具有SpringBoot对应的maven插件

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

SpringBoot简介:       

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

        Spring程序缺点:

                配置繁琐

                依赖设置繁琐

        SpringBoot程序优点:

                自动配置

                起步依赖(简化依赖配置)

                辅助功能(内置服务器,...) 

        SpringBoot起步依赖

                starter

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

                parent

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

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

                实际开发:

                        使用任意坐标时,仅书写GAV中的GA,V由SpringBoot提供

                        如发生坐标错误,在指定version(要小心版本冲突)

                辅助功能

                启动方式

@SpringBootApplication
public class Springboot01QuickstartApplication {

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

}

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

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

                SpringBoot程序启动

                        使用maven依赖管理变更起步依赖项

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>

                        Jetty比Tomcat更轻量级,可扩展性更强(相较于Tomcat),谷歌应用引擎(GAE)已经全面切换为Jetty

基础配置

        配置格式

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

                        application.properties

                                server.prot=80

                        application.yml

                                server:

                                        port: 81

                        application.yaml

                                server:

                                        port: 82

                启动提示功能消失解决方案:

                        1、点击File中Project Structure

                        2、选择Facets

                        3、找到对应的Spring

                SpringBoot配置文件加载顺序(了解)

                        application.properties > application.yml > application.yaml 

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

        yaml

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

                优点:

                        容易阅读

                        容易与脚本语言交互

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

                YAML文件扩展名

                        .yml(主流)

                        .yaml

                yaml语法规则:

                        大小写敏感

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

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

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

                        # 表示注释

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

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

enterprise:
  name: zsb
  age: 16
  tel: 22222
  subject:
    - Java
    - 前端
    - 大数据

                yaml数据读取:

                        使用@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名...}

@Value("${lesson}")
private String lesson;

@Value("${server.port}")
private Integer port;

@Value("${enterprise.subject[0]}")
private String subject_00;

                        封装全部数据到Environment对象

@Autowired
private Environment environment;
System.out.println(environment.getProperty("lesson"));
System.out.println(environment.getProperty("server.port"));
System.out.println(environment.getProperty("enterprise.subject[0]"));

                        自定义对象封装指定数据

@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {

    private String name;
    private Integer age;
    private String tel;
    private String[] subject;
@Autowired
private Enterprise enterprise;
System.out.println(enterprise);

                        自定义对象封装数据警告解决方案

<dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-configuration-processor</artifactId>

        <optional>true</optional>

</dependency>

                

                多环境开发:

spring:
  profiles:
    active: dev

---
#开发环境
spring:
  config:
    activate:
      on-profile: dev
server:
  port: 80

---
#生产环境
spring:
  config:
    activate:
      on-profile: pro
server:
  port: 81
---
#测试环境
spring:
  config:
    activate:
      on-profile: test
server:
  port: 82

                properties文件多环境启动

                        主启动配置文件application.properties

#设置启用的环境
spring.profiles.active=test

                        环境分类配置文件application-pro.properties

server.port=8080

                        环境分类配置文件application-dev.properties

server.port=8081

                        环境分类配置文件application-test.properties

server.port=8082

                多环境启动命令格式

                        带参数启动SpringBoot

                java -jar springboot.jar --spring.profiles.active=test

                java -jar springboot.jar --server.port=88

                java -jar springboot.jar --server.port=88 --spring.profiles.active=test

                       

                参数加载优先顺序

查看        https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config

                Maven与SpringBoot多环境兼容

                        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>

                        2、SpringBoot中引用Maven属性

#设置启用的环境
spring:
  profiles:
    active: ${profile.active}

---
#开发
spring:
  config:
    activate:
      on-profile: dev
server:
  port: 80

---
#生产
spring:
  config:
    activate:
      on-profile: pro
server:
  port: 81

---
#测试
spring:
  config:
    activate:
      on-profile: test
server:
  port: 82

                        3、执行Maven打包指令

                        Maven指令执行完毕后,生成了对应的包,其中类参与编译,但是配置文件并没有编译,而是复制到包中

                        解决思路:对于源码中非java类的操作要求加载Maven对应的属性,解析${}占位符

<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>

                配置文件分类

                        SpringBot中4级配置文件

                                1级:file:config/application.yml        【最高】

                                2级:file:application.yml             

                                3级:classpath:config/application.yml        

                                4级:classpath:application.yml        【最低】

                        作用:

                                1级与2级留作系统打包后设置通用属性

                                3级与4级用于系统开发前设置通用属性      

整合第三方技术

        整合Junit

                SpringBoot整合JUnit

@SpringBootTest(classes = Springboot07TestApplication.class)
class Springboot07TestApplicationTests {

    @Autowired
    private BookService bookService;

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

}

@SpringBootTest        测试类注解        测试类定义上方        设置JUnit加载的SpringBoot启动类

相关属性:classes   设置SpringBoot启动类

        注意事项:如果测试类在SpringBoot启动类的包或子包中,可以省略启动类的设置,也就是省略classes的设定

        整合第三方技术

                基于SpringBoot实现SSM整合

                        SpringBoot整合Spring(不存在)

                        SpringBoot整合SpringMVC(不存在)

                        SpringBoot整合MyBatis(主要)

                SpringBoot整合MyBatis

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

                        2、选择当前模块需要使用的技术集(MyBatis、MySQL)

                        3、设置数据源参数

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

        注意事项:SpringBoot版本低于2.4.3(不含),Mysql驱动版本大于8.0时,需要在url连接串中配置时区

url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
   或在MySQL数据库端配置时区解决此问题

                        4、定义数据层接口与映射配置

@Mapper
public interface BookDao {

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

                        5、测试类中注入dao接口,测试功能

@SpringBootTest
class Springboot08MybatisApplicationTests {

    @Autowired
    private BookDao bookDao;

    @Test
    void testGetById() {
        Book book = bookDao.getById(1);
        System.out.println(book);
    }

}

                                

        

                

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值