SpringBoot(Lombok + Spring Initailizr + yaml)

1.Lombok

1.基本介绍

image-20240313161718829

2.应用实例
1.pom.xml 引入Lombok,使用版本仲裁
    <!--导入springboot父工程-->
    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.5.3</version>
    </parent>
    <dependencies>
        <!--配置maven项目场景启动器,自动导入和web相关的包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--引入Lombok,使用版本仲裁-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
2.@Data注解说明
  • 相当于Getter, Setter, RequiredArgsConstructor, ToString, EqualsAndHashCode,Value这些注解的组合
  • 主要记住Getter, Setter,ToString

package lombok;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Generates getters for all fields, a useful toString method, and hashCode and equals implementations that check
* all non-transient fields. Will also generate setters for all non-final fields, as well as a constructor.
*


* Equivalent to {@code @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode}.
*


* Complete documentation is found at the project lombok features page for @Data.
*
* @see Getter
* @see Setter
* @see RequiredArgsConstructor
* @see ToString
* @see EqualsAndHashCode
* @see lombok.Value
/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface Data {
/
*
* If you specify a static constructor name, then the generated constructor will be private, and
* instead a static factory method is created that other classes can use to create instances.
* We suggest the name: “of”, like so:
*
*


* public @Data(staticConstructor = “of”) class Point { final int x, y; }
*

*
* Default: No static constructor, instead the normal constructor is public.
*
* @return Name of static ‘constructor’ method to generate (blank = generate a normal constructor).
*/
String staticConstructor() default “”;
}

3.@RequiredArgsConstructor注解说明(不常用)

image-20240313163329615

4.@NoArgsConstructor无参构造器
5.@AllArgsConstructor全参构造器
注意事项:
  • 当使用全参构造器时,默认的无参构造器会消失
  • 如果还想要无参构造器就需要使用无参构造器的注解
6.两种使用Lombok的方式
1.需要Getter, Setter,ToString,无参构造器
  • @Data
2.需要使用Getter, Setter,ToString,无参构造器,全参构造器
  • @Data
  • @AllArgsConstructor
  • @NoArgsConstructor
3.在IDEA中安装Lombok插件解锁扩展注解
1.安装插件

image-20240313170237189

2.扩展注解:日志输出
1.代码实例

image-20240313171249514

2.会在日志中输出

image-20240313171312777

2.Spring Initailizr(不推荐)

1.基本介绍

image-20240313171622129

2.通过IDEA方式创建
1.新创建一个项目

image-20240313172411941

2.进行配置

image-20240313172738168

3.创建成功

image-20240313173041517

3.通过官网创建
1.进入官网

image-20240313173352413

2.配置完之后选择

image-20240313173437210

3.最后会生成一个.zip文件,解压之后在IDEA中打开即可
4.第一次使用自动配置爆红

image-20240313173715463

3.yaml

1.基本说明

image-20240313174407089

2.yaml基本语法

image-20240313174915598

3.yaml数据类型
1.字面量

image-20240313175754697

2.对象

image-20240313175837930

3.数组

image-20240313175918900

4.yaml应用实例
1.创建一个新的maven项目

image-20240313190536147

2.pom.xml引入依赖并刷新maven
    <!--导入springboot父工程-->
    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.5.3</version>
    </parent>
    <dependencies>
        <!--配置maven项目场景启动器,自动导入和web相关的包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--引入Lombok,使用版本仲裁-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
3.编写两个bean
1.Car.java
package com.sun.springboot.bean;


import lombok.Data;
import org.springframework.stereotype.Component;

/**
 * @author 孙显圣
 * @version 1.0
 */
@Data //getter,setter,tostring,无参构造
@Component
public class Car {
    private String name;
    private Double price;

}

2.Monster.java
package com.sun.springboot.bean;

import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author 孙显圣
 * @version 1.0
 */
@Data
@Component
public class Monster {
    private Integer id;
    private String name;
    private Integer age;
    private Boolean isMarried;
    private Date birth;
    private Car car;
    private String[] skill;
    private List<String> hobby;
    private Map<String, Object> wife;
    private Set<Double> salaries;
    private Map<String, List<Car>> cars;

}

4.HiController.java 接受请求
package com.sun.springboot.controller;

import com.sun.springboot.bean.Monster;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author 孙显圣
 * @version 1.0
 */
@RestController
public class HiController {
    @Resource
    private Monster monster;

    @RequestMapping("/monster")
    public Monster monster() {
        return monster;
    }
}

5.主程序Application.java
package com.sun.springboot;

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

/**
 * @author 孙显圣
 * @version 1.0
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}

6.运行主程序(目前返回的值是空的)

image-20240313192941733

7.创建yaml文件(后缀也可以是yml) resources/application.yml
monster: #前缀
  id: 100
  name: 牛魔王
  age: 500
  isMarried: false
  birth: 2000/11/11 
8.绑定数据到Monster类

image-20240313194026901

9.解决报错
1.因为使用@Configuration注解导致的问题

image-20240313194047615

2.在pom.xml中添加依赖即可
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <!--防止将该依赖传递到其他模块-->
      <optional>true</optional>
    </dependency>
3.运行主程序

image-20240313195153451

10.完整yml文件
monster: #前缀
  id: 100
  name: 牛魔王
  age: 500
  isMarried: false
  birth: 2000/11/11
  #对象类型
#  car: {name: 宝马, price: 1000} #行内格式
  car:
    name: 奔驰
    price: 3000

  #数组类型
#  skill: [芭蕉扇, 牛魔拳] #行内格式
  skill:
    - 牛魔王
    - 芭蕉扇

  #list类型
#  hobby: [白骨精, 美人鱼]
  hobby:
    - 白骨精
    - 牛魔王

  #map类型
#  wife: {no1: 牛魔王, no2: 猪八戒}
  wife:
    no1: 白骨精
    no2: 铁扇公主

  #set类型
#  salaries: [1, 2, 3]
  salaries:
    - 4
    - 5
    - 6
  #map<String, List<Car>>类型
  cars:
    car1: [
      {name: 奔驰, price: 400},
      {name: 奔驰, price: 400}
    ]
    car2: [
      {name: 奔驰, price: 400},
      {name: 奔驰, price: 400}
    ]

#  cars: {car1: [{name: 奔驰, price: 200}, {name: 奔驰, price: 200}],
#          car2: [{name: 奔驰, price: 200}, {name: 奔驰, price: 200}]}



11.结果展示

image-20240313204905498

12.yaml注意事项和细节说明
1.注意事项
  • application.properties和application.yml如果有相同前缀值的绑定,则application.properties优先级高
  • 字符串无需加引号,但是加引号也没有问题
  • yaml配置文件如果不提示字段信息,则导入依赖即可
  • 如果添加依赖还不显示字段信息则安装YAML插件
2.细节说明
  • 其实不需要记住什么yaml的类型,只要能跟java对应上即可
  • 如果是对象或者map,则表示方式是
    • 换行key: value
    • {key1: value1, key2: value2}
  • 如果是数组或list,则表示方式是
    • 换行- value
    • [value1, value2]

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/454928.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

[论文笔记] pai-megatron qwen1.5报错

Qwen1.5-0.5b-chat 使用example中fintune.py 报错 Issue #77 QwenLM/Qwen1.5 GitHub 解决方案&#xff1a; transformers升级到4.37.0 pip install setuptools65.5.1 pip install transformers4.37.0

Matlab|【分布鲁棒】数据驱动的多离散场景电热综合能源系统分布鲁棒优化算法

目录 主要内容 1.1 主要难点-分布鲁棒优化 1.2 程序求解步骤-主子问题迭代 部分结果 下载链接 主要内容 本程序主要对《基于场景聚类的主动配电网分布鲁棒综合优化》-高海淑的方法复现&#xff0c;应用到综合能源电热微网方向&#xff0c;采用拉丁超立方抽样对不同…

鸿蒙API9+axios封装一个通用工具类

使用方式&#xff1a; 打开Harmony第三方工具仓&#xff0c;找到axios&#xff0c;如图&#xff1a; 第三方工具仓网址&#xff1a;https://ohpm.openharmony.cn/#/cn/home 在你的项目执行命令&#xff1a;ohpm install ohos/axios 前提是你已经装好了ohpm &#xff0c;如果没…

【Flutter 面试题】怎么理解Flutter的Isolate?并发编程

【Flutter 面试题】怎么理解Flutter的Isolate&#xff1f;并发编程 文章目录 写在前面解答补充说明完整代码示例说明 写在前面 &#x1f64b; 关于我 &#xff0c;小雨青年 &#x1f449; CSDN博客专家&#xff0c;GitChat专栏作者&#xff0c;阿里云社区专家博主&#xff0c;…

Qt-QPainter drawText方法不同重载之间的区别

QPainter类的drawText方法有如下重载&#xff1a; void drawText(const QPointF &position, const QString &text) void drawText(const QPoint &position, const QString &text) void drawText(int x, int y, const QString &text) void drawText(co…

解决尚品甄选验证码图片无法显示bug

按照他的视频要求去做发现图片无法正常显示&#xff0c;通过查看浏览器网络错误&#xff0c;发现请求验证码的网址是重叠的http://localhost:3001/admin/system/index/login/admin/system/index/generateValidateCode是这样的&#xff0c;说明baseUrl是/admin/system/index/log…

【Python如何与电脑玩石头剪刀布游戏】

1、石头剪刀布Python代码如下&#xff1a; import random while True:a random.randint(0, 2)b int(input("请输入一个数字&#xff08;0石头, 1剪刀, 2布&#xff09;: "))c [石头, 剪刀, 布]if b ! 0 and b ! 1 and b ! 2:print("傻子&#xff0c;你出错了…

Cisco Packet Tracer模拟器实现路由器的路由配置及网络的安全配置

1. 内容 1. 配置路由器实现多个不同网络间的通信&#xff0c;路由器提供的路由协议包括静态路由协议、RIP动态路由、OSPF动态路由协议等等&#xff0c;训练内容包括路由器的静态路由配置、路由器的RIP动态路由配置、路由器的OSPF动态路由配置以及路由器的路由重分布配置。 2.…

测试环境搭建整套大数据系统(十一:docker部署superset,无密码登录嵌入html)

一&#xff1a;安装docker 参考文档 https://blog.csdn.net/weixin_43446246/article/details/136554243 二&#xff1a;安装superset 下载镜像。 拉取镜像&#xff08;docker pull amancevice/superset&#xff09; 查看镜像是否下载完成&#xff08;docker images&#xf…

Tomcat目录结构

文章目录 binconfliblogswebapp bin 存放tomcat的可执行程序 从上图可以看出bin中的文件主要是两种文件&#xff0c;一种是.bat一种是.sh .bat:主要用于windows .sh:主要用于linux .bat文件是Windows操作系统中的批处理文件。它是一种简单的文本文件&#xff0c;其中包含了一…

java内部类的作用与优缺点

一、前言 很久没看到java内部类了&#xff0c;今天在审查代码时候&#xff0c;发现了java内部类&#xff0c;主要是内部类还嵌套了内部类。于是记录一下 二、java内部类的作用与优缺点 Java内部类&#xff0c;也称为嵌套类&#xff0c;是定义在另一个类&#xff08;外部类&am…

pycharm 历史版本下载地址

pycharm 历史版本下载地址 老版本能用就行&#xff0c;不需要搞最新的&#xff0c;当然了&#xff0c;有些小伙伴就是喜欢新的&#xff08;最先吃螃蟹&#xff09; 博主就不搞最新了&#xff0c;哈哈 上菜&#xff1a; https://www.jetbrains.com/pycharm/download/other.html…

Python (用户登录、身份归属地查询添加异常处理、绘制多角星、电影信息提取)

任务一&#xff1a;用户登录 登录系统通常分为普通用户与管理员权限&#xff0c;在用户登录系统时&#xff0c;可以根据自身权限进行选择登录。本任务要求实现一个用户登录的程序&#xff0c;该程序分为管理员用户与普通用户&#xff0c;其中管理员账号密码在程序中设定&#…

rt-thread之sal+lwip的tcp客户端示例记录(接收非阻塞)

示例记录 #include "lwip_test.h" #include "lwip/sockets.h" #include "netdev.h"#define DBG_ENABLE #define DBG_TAG "lwip.tst" #define DBG_LVL DBG_LOG#include <rtdbg.h>#define SERVER_PORT 8080 #define SERVER_HOST …

JAVA的编译过程

1.通过使用 javac.exe 对 xxx.java文件进行编译&#xff0c;生成相应的 xxx.class&#xff08;字节码文件&#xff09; 2.使用 java.exe 对 xxx.class 进行相应解码&#xff0c;并将结果送给JVM&#xff08;java虚拟机&#xff09;中的类装载器 3. 字节码验证器会判断代码类…

php双端交易所

php双端交易所&#xff0c;如需联系 完美修复版&#xff0c;带所有 PHP双端交易所完美版: PHP双端交易所完美版,带前端源码https://gitee.com/ycsw/ex.git

TikTok直播畅通无阻,海外直播专线打造稳定流畅的网络环境

随着tiktok的爆火&#xff0c;越来越多的商家开始尝试在tiktok进行直播。然而&#xff0c;由于距离长、横跨大陆海洋等原因&#xff0c;在海外直播时网络问题十分突出&#xff0c;例如冻结和传输故障&#xff0c;给观众带来不良体验。为了解决这一问题&#xff0c;tiktok海外直…

R语言系列4——R语言统计分析基础

目录 写在开头1. 描述性统计分析1.1 描述性统计分析的定义与重要性1.2 R语言中的描述性统计分析功能1.3 常用的描述性统计量及其在R中的计算方法1.4 使用R语言进行描述性统计分析的实际示例1.5 描述性统计分析的局限性和应用注意事项 2. 假设检验基础2.1. 假设检验的基本原理和…

【UE5】非持枪站姿移动混合空间

项目资源文末百度网盘自取 创建角色在非持枪状态且站立移动的动画混合空间 在Character文件夹中创建文件夹&#xff0c;命名为BlendSpace 所有混合空间文件都放到这个文件夹中 在BlendSpace文件夹中单击右键&#xff0c;选择动画(Animation)中的混合空间(BlendSpace) 选择SK…

学习网络编程No.13【网络层IP协议理解】

引言&#xff1a; 北京时间&#xff1a;2024/3/5/8:38&#xff0c;早六加早八又是生不如死的一天&#xff0c;不过好在喝两口热水提口气手指还能跳动。当然起关键性作用的还是思维跟上了课程脑袋较为清晰&#xff0c;假如是听学校老师在哪里磨过来磨过去&#xff0c;那我倒头就…
最新文章