• 【JAVA】SpringBoot


    目录

    【SpringBoot】

    【使用】

    【快速启动】

    【辅助功能——切换服务器】

    【基础配置】

    【yaml】—— YAML Ain't Markup Language

    【多环境开发】

    【yaml文件多环境启动】

    【properties文件多环境启动】

    【多环境启动命令格式】

    【多环境开发控制】

    【Maven与SpringBoot多环境兼容】

    【配置文件分类】

    【SpringBoot整合JUnit】

    【SpringBoot实现SSM整合】

    【SpringBoot整合MyBatis】


    【SpringBoot】

    【概述】

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

    【优点】

    • 自动配置
    • 起步依赖(简化依赖配置)
    • 辅助功能(内置服务器)

    【使用】

    1、创建新模块,选择Spring初始化,并配置模块相关基础信息(根据需求改)

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

    3、开发控制器类

    1. @RestController
    2. @RequestMapping("/books")
    3. public class BookController {
    4. @GetMapping("/{id}")
    5. public String getById(@PathVariable Integer id) {
    6. System.out.println("id==>" + id);
    7. return "hello SpringBoot!";
    8. }
    9. }

    4、运行自动生成的Application类

    【注意】

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

    创建项目是基于SpringBoot官网

    【快速启动】

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

    2、执行启动指令:java -jar spring.jar(jar包名)

    【注意】

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

    1. <build>
    2.     <plugins>
    3.         <plugin>
    4.             <groupId>org.springframework.bootgroupId>
    5.             <artifactId>spring-boot-maven-pluginartifactId>
    6.         plugin>
    7.     plugins>
    8. build>

    2、打jar包前注意:

    1. package之前进行一次clean,防止之前的缓存影响之后的结果
    2. 更改语言环境为UTF-8,防止配置文件中的中文出现乱码

    【辅助功能——切换服务器】

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

    1. <dependencies>
    2.     <dependency>
    3.         <groupId>org.springframework.bootgroupId>
    4.         <artifactId>spring-boot-starter-webartifactId>
    5.        
    6.         <exclusions>
    7.             <exclusion>
    8.                 <groupId>org.springframework.bootgroupId>
    9.                 <artifactId>spring-boot-starter-tomcatartifactId>
    10.             exclusion>
    11.         exclusions>
    12.     dependency>
    13.    
    14.     <dependency>
    15.         <groupId>org.springframework.bootgroupId>
    16.         <artifactId>spring-boot-starter-jettyartifactId>
    17.     dependency>
    18. dependencies>

    【注意】

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

    【基础配置】

    【配置格式】

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

    【注意】

    1、自动提示功能消失解决方案

    2、SpringBoot配置文件加载顺序

    application.properties  >  application.yml  >  application.yaml

    3、主要使用application.yml

    4、SpringBoot核心配置文件名为application

    5、SpringBoot内置属性过多,且所有属性集中在一起修改,在使用时,通过提示键+关键字修改属性

    【yaml】—— YAML Ain't Markup Language

    【概述】

    一种数据序列化格式

    【优点】

    • 容易阅读
    • 容易与脚本语言交互
    • 以数据为核心,重数据轻格式

    【YAML文件扩展名】

    • .yml(主流)
    • .yaml

    【语法规则】

    • 核心:数据前面要加空格与冒号隔开
    • 大小写敏感
    • 属性层级关系使用多行描述,每行结尾使用冒号结束
    • 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)
    • 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
    • # 表示注释

    【yaml数组数据】

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

    例:

    1. enterprise:
    2. name: itcast
    3. age: 16
    4. tel: 4006184000
    5. subject:
    6. - Java
    7. - 前端
    8. - 大数据

    【yaml数据读取】

    【数据】

    1. lesson: SpringBoot
    2. server:
    3. port: 8080
    4. enterprise:
    5. name: zhangsan
    6. age: 15
    7. tel: 12345678910
    8. subject:
    9. - java
    10. - web
    11. - 大数据
    • 使用@Value读取单个数据,属性名引用方式${一级属性名.二级属性名……}
    1. @RestController
    2. @RequestMapping("/books")
    3. public class BookController {
    4.     @Value("${lesson}")
    5.     private String lessonName;
    6.     @Value("${server.port}")
    7.     private int port;
    8.     @Value("${enterprise.subject[1]}")
    9.     private String[] subject_01;
    10. }
    • 读取多个数据,封装全部数据到Environment对象
    1. @RestController
    2. @RequestMapping("/books")
    3. public class BookController {
    4.     @Autowired
    5.     private Environment env;
    6.     @GetMapping("/{id}")
    7.     public String getById(@PathVariable Integer id){
    8.         System.out.println(env.getProperty("lesson"));
    9.         System.out.println(env.getProperty("enterprise.name"));
    10.         System.out.println(env.getProperty("enterprise.subject[0]"));
    11.         return "hello , spring boot!";
    12.     }
    13. }
    • 读取多个数据,自定义对象封装指定数据

    1、定义一个对象(属性与数据名称一致)

    1. @Component
    2. @ConfigurationProperties(prefix = "enterprise")
    3. public class Enterprise {
    4. private String name;
    5. private Integer age;
    6. private String tel;
    7. private String[] subject;
    8. }

    2、引用对象

    1. @RestController
    2. @RequestMapping("/books")
    3. public class BookController {
    4.     @Autowired
    5.     private Enterprise enterprise;
    6. }

    【注意】

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

    1. <dependency>
    2.     <groupId>org.springframework.bootgroupId>
    3.     <artifactId>spring-boot-configuration-processorartifactId>
    4.     <optional>trueoptional>
    5. dependency>

    【多环境开发】

    【yaml文件多环境启动】

    1. #设置启用环境
    2. spring:
    3. profiles:
    4. active: dev
    5. ---
    6. #开发
    7. server:
    8. port: 8080 #开发环境具体参数指定
    9. spring:
    10. config:
    11. activate:
    12. on-profile: dev #设置开发环境
    13. ---
    14. #生产
    15. server:
    16. port: 8081 #生产环境具体参数指定
    17. spring:
    18. config:
    19. activate:
    20. on-profile: pro #设置生产环境
    21. ---
    22. #测试
    23. server:
    24. port: 8082 #测试环境具体参数指定
    25. spring:
    26. config:
    27. activate:
    28. on-profile: test #设置测试环境

    【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

    【多环境启动命令格式】

    • 带参数启动Spring

    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多环境兼容】

    SpringBoot听从Maven的设定

    1、对资源文件开启对默认占位符的解析

    1. <build>
    2. <plugins>
    3. <plugin>
    4. <groupId>org.apache.maven.pluginsgroupId>
    5. <artifactId>maven-resources-pluginartifactId>
    6. <version>3.2.0version>
    7. <configuration>
    8. <encoding>utf-8encoding>
    9. <useDefaultDelimiters>trueuseDefaultDelimiters>
    10. configuration>
    11. plugin>
    12. plugins>
    13. build>

    2、Maven中设置多环境属性

    1. <profiles>   
    2.     <profile>       
    3.         <id>dev_envid>       
    4.         <properties>           
    5.             <profile.active>devprofile.active>       
    6.         properties>       
    7.         <activation>           
    8.             <activeByDefault>trueactiveByDefault>       
    9.         activation>   
    10.     profile>   
    11.     <profile>       
    12.         <id>pro_envid>       
    13.         <properties>           
    14.             <profile.active>proprofile.active>       
    15.         properties>   
    16.     profile>   
    17.     <profile>       
    18.         <id>test_envid>       
    19.         <properties>           
    20.             <profile.active>testprofile.active>       
    21.         properties>   
    22.     profile>
    23. profiles>

     3、SpringBoot中引用Maven属性

    1. #设置启用环境
    2. spring:
    3. profiles:
    4. active: ${profile.active}
    5. ---
    6. #开发
    7. server:
    8. port: 8080
    9. spring:
    10. config:
    11. activate:
    12. on-profile: dev
    13. ---
    14. #生产
    15. server:
    16. port: 8081
    17. spring:
    18. config:
    19. activate:
    20. on-profile: pro
    21. ---
    22. #测试
    23. server:
    24. port: 8082
    25. spring:
    26. config:
    27. activate:
    28. on-profile: test

    4、执行Maven打包指令

    【配置文件分类】

    SpringBoot中4级配置文件

    • 1级: file :config/application.yml  【最高】
    • 2级: file :application.yml
    • 3级:classpath:config/application.yml
    • 4级:classpath:application.yml  【最低】

    【作用】

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

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

    【SpringBoot整合JUnit】

    1. @SpringBootTest
    2. class SpringBoot04TestApplicationTests {
    3. @Autowired
    4. private BookService bookService;
    5. @Test
    6. void contextLoads() {
    7. bookService.save();
    8. }
    9. }

    【@SpringBootTest】

    • 名称:@SpringBootTest
    • 类型:测试类注解
    • 作用:设置JUnit加载的SpringBoot启动类
    • 属性:classes:设置SpringBoot启动类

    例:

    1. @SpringBootTest(classes = Springboot07JunitApplication.class)
    2. class Springboot07JunitApplicationTests {}

    【注意】

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

    【SpringBoot实现SSM整合】

    SpringBoot整合MyBatis

    【流程】

    1. pom.xml:配置起步依赖,必要的资源坐标(druid)
    2. application.yml:设置数据源、端口等
    3. 配置类:全部删除
    4. dao:设置@Mapper
    5. 测试类
    6. 页面:放置在resources目录下的static目录中

    【步骤】

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

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

     3、设置数据源参数

    1. spring:
    2. datasource:
    3. driver-class-name: com.mysql.cj.jdbc.Driver
    4. url: jdbc:mysql://localhost:3306/db1
    5. username: root
    6. passwords: root
    7. type: com.alibaba.druid.pool.DruidDataSource

    【注意】

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

    jdbc:mysql://localhost:3306/db1?serverTimezone=UTC

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

    1. @Mapper
    2. public interface BookDao {
    3. @Select("select * from tb_book where id = #{id}")
    4. public Book getById(Integer Id);
    5. }

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

    1. @SpringBootTest
    2. class SpringBoot05MybatisApplicationTests {
    3. @Autowired
    4. private BookDao bookDao;
    5. @Test
    6. void testById() {
    7. Book book=bookDao.getById(1);
    8. System.out.println(book);
    9. }
    10. }

    【注意】

    在SpringBoot的服务器中,访问HTML需要增加一个坐标

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-thymeleafartifactId>
    4. dependency>
  • 相关阅读:
    win11,无法修改文件的只读属性,解决办法
    阿里云国际版两个ECS云服务器之间的迁移教程
    【毕业设计】Java 基于微信小程序的药店管理系统
    辛弃疾,笔墨剑影的一生
    django中的跨域问题以及解决策略
    UWB安全数据通讯STS-加密、身份认证
    Mock使用场景
    【故障公告】数据库服务器 CPU 近 100% 造成全站故障,雪上加霜难上加难的三月
    计算机毕业设计ssm基于疫情防控下社区管理平台my3tu系统+程序+源码+lw+远程部署
    React 中 react-i18next 切换语言( 项目国际化 )
  • 原文地址:https://blog.csdn.net/huihu__/article/details/126821973