SpringBoot框架学习笔记(三):Lombok 和 Spring Initailizr

news2024/9/21 4:29:30

1 Lombok

1.1 Lombok 介绍

(1)Lombok 作用

  • 简化JavaBean开发,可以使用Lombok的注解让代码更加简洁
  • Java项目中,很多没有技术含量又必须存在的代码:POJO的getter/setter/toString;异常处理;I/O流的关闭操作等等,这些代码既没有技术含量,又影响着代码的美观,Lombok应运而生

(2)SpringBoot 和 IDEA 官方支持

  • IDEA 2020已经内置了Lombok插件
  • SpringBoot 2.1.x之后的版本也在Starter中内置了Lombok依赖

1.2 Lombok 常用注解

  • @Data:注解在类上;提供所有属性的 getter 和 setter 方法,此外还提供了equals、canEqual、hashCode、toString 方法
  • @Setter:注解在属性上;为属性提供 setter 方法
  • @Getter:注解在属性上;为属性提供 getter 方法
  • @ToString:注解在类上;提供toString 方法
  • @Log4j:注解在类上;为类提供一个属性名为 log 的 Log4j 对象
  • @NoArgsConstructor:注解在类上;为类提供一个无参的构造方法
  • @AllArgsConstructor:注解在类上;为类提供一个全参的构造方法
  • @CleanUp:可以关闭流
  • @Builder:给被注解的类加个构造者模式
  • @Synchronized:加个同步锁
  • @SneakyThrows:等同于try/catch 捕获异常
  • @NotNull:如果给参数加个这个注解,参数为 null 时会抛出空指针异常
  • @Value:该注解和@Data类似,区别在于它会把所有成员变量默认定于为private final修饰,并且不会生成setter方法

Lombok扩展注解(需要在idea上安装Lombok插件才能使用) 

  • @Slf4j:注解在类上;为类提供一个属性名为 log 的 Slf4j 对象进行日志输出

1.3 Lombok 应用实例

需求说明:使用Lombok 简化实体类 Furn.java 代码,让代码简洁高效

代码实现

(1)Furn 实体类如下 

package com.springboot.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "furn01")
public class Furn {
    private Integer id;
    private String name;
    private Double price;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }
}

(2)在pom.xml中引入lombok,虽然springboot内置了lombok,但是如果要使用的话,仍然需要显示得引入一下

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

不需要指定版本,版本在spring-boot-dependencies-2.5.3中指定了

但我这里出现了问题。这样设置后也无法使用Lombok的注解。解决方法:在pom文件设置高版本的Lombok

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.28</version>
</dependency>

(3)修改 Furn.java 使用Lombok注解简化代码,可以通过idea自带的反编译功能,看Furn.class的源码,就可以看到生成的完整代码

简化代码如下

package com.springboot.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@NoArgsConstructor
@AllArgsConstructor
@Data
@Component
@ConfigurationProperties(prefix = "furn01")
public class Furn {
    private Integer id;
    private String name;
    private Double price;
}

在目录 target/classes/com/springboot/bean/furn.class 即可看到反编译后的源码

完整源码如下 

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.springboot.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(
    prefix = "furn01"
)
public class Furn {
    private Integer id;
    private String name;
    private Double price;

    public Furn() {
    }

    public Furn(final Integer id, final String name, final Double price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    public Integer getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public Double getPrice() {
        return this.price;
    }

    public void setId(final Integer id) {
        this.id = id;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public void setPrice(final Double price) {
        this.price = price;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Furn)) {
            return false;
        } else {
            Furn other = (Furn)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label47: {
                    Object this$id = this.getId();
                    Object other$id = other.getId();
                    if (this$id == null) {
                        if (other$id == null) {
                            break label47;
                        }
                    } else if (this$id.equals(other$id)) {
                        break label47;
                    }

                    return false;
                }

                Object this$price = this.getPrice();
                Object other$price = other.getPrice();
                if (this$price == null) {
                    if (other$price != null) {
                        return false;
                    }
                } else if (!this$price.equals(other$price)) {
                    return false;
                }

                Object this$name = this.getName();
                Object other$name = other.getName();
                if (this$name == null) {
                    if (other$name != null) {
                        return false;
                    }
                } else if (!this$name.equals(other$name)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof Furn;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $id = this.getId();
        result = result * 59 + ($id == null ? 43 : $id.hashCode());
        Object $price = this.getPrice();
        result = result * 59 + ($price == null ? 43 : $price.hashCode());
        Object $name = this.getName();
        result = result * 59 + ($name == null ? 43 : $name.hashCode());
        return result;
    }

    public String toString() {
        return "Furn(id=" + this.getId() + ", name=" + this.getName() + ", price=" + this.getPrice() + ")";
    }
}

2 Spring Initailizr

2.1 Spring Initailizr 作用

通过Spring官方提供的Spring Initailizr 来构建Maven项目,能完美支持IDEA和Eclipse,让程序员来选择需要的开发场景(starter),还能自动生成启动类和单元测试代码

需要注意得是 Spring Initailizr 对Idea版本有要求,同时还要走网络

2.2  Spring Initailizr 使用演示

需求说明:使用 Spring Initailizr 创建SpringBoot项目,并支持web应用场景,支持MyBatis

2.2.1 方式1:IDEA 创建

(1)文件->新建->项目

(2)选择 Spring Initailizr (如果看不到这个项,需要安装 Spring Initailizr 插件) 

(3)效果如下

 

2.2.2 方式2:start.spring.io 创建

(1)打开网站  start.spring.io

 

(2)将该压缩包解压后使用IDEA打开,效果如下

 

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C语言学习笔记[25]:循环语句for

for循环 for循环的基本语法 for(表达式1;表达式2;表达式3)循环语句; 表达式1为初始化部分&#xff0c;用于初始化循环变量的。 表达式2为条件判断部分&#xff0c;用于判断循环何时终止。 表达式3为调整部分&#xff0c;用于循环条件的调整。 例如用for循环实现打印1~10的数字…

HarmonyOS根据官网写案列~ArkTs从简单地页面开始

Entry Component struct Index {State message: string 快速入门;build() {Column() {Text(this.message).fontSize(24).fontWeight(700).width(100%).textAlign(TextAlign.Start).padding({ left: 16 }).fontFamily(HarmonyHeiTi-Bold).lineHeight(33)Scroll() {Column() {Ba…

object-C 解答算法:移动零(leetCode-283)

移动零(leetCode-283) 题目如下图:(也可以到leetCode上看完整题目,题号283) 解题思路: 本质就是把非0的元素往前移动,接下来要考虑的是怎么移动,每次移动多少? 这里需要用到双指针,i 记录每次遍历的元素值, j 记录“非0元素值”需要移动到的位置; 当所有“非0元素值”都移…

解决TypeError: __init__() takes 1 positional argument but 2 were given

问题描述&#xff1a; 如下图&#xff0c;在使用torch.nn.Sigmoid非线性激活时报错 源代码&#xff1a; class testrelu(nn.Module):def __init__(self):super().__init__()self.sigmoid Sigmoid()def forward(self, input):output self.sigmoid(input)return outputwriter…

可视化剪辑,账号矩阵,视频分发,聚合私信一体化营销工具 源----代码开发部署方案

可视化剪辑&#xff1a; 为了实现可视化剪辑功能&#xff0c;可以使用流行的视频编辑软件或者开发自己的视频编辑工具。其中&#xff0c;通过设计用户友好的界面&#xff0c;用户可以简单地拖拽和放大缩小视频片段&#xff0c;剪辑出满足需求的视频。在开发过程中&#xff0c;可…

接口测试JMeter-1.接口测试初识

第一章 接口测试初识 1. 接口测试理论基础 “接口测试”一个让人觉得非常高大上的名词&#xff0c;特别是对于刚入门的测试同学而言。随着测试技术不断的深化&#xff0c;“接口测试”出现在我们视野中的频次越来越高。那么接口测试到底是如何做的&#xff1f;接口测试的优势又…

Uniapp 组件 props 属性为 undefined

问题 props 里的属性值都是 undefined 代码 可能的原因 组件的名字要这样写&#xff0c;这个官方文档有说明

Linux-CentOS7忘记密码找回步骤

虚拟机版本 一、进入开机页面&#xff0c;先按上下&#xff08;↑↓&#xff09;键&#xff0c;以免系统自动启动。 二、按“e”键进入编辑页面,找到如下图位置&#xff0c;输入&#xff1a;init/bin/sh 按CTRLX 进入单用户模式。 三、 输入 mount -o remount,rw / 然后按 ent…

verilog bug记录——正点原子spi_drive存在的问题

verilog bug记录——正点原子spi_drive存在的问题 问题概述代码修改—spi_drive.v遗留问题 问题概述 因为项目需求&#xff0c;需要利用spi对flash进行擦除和写入操作&#xff0c;所使用的开发板是正电原子的达芬奇开发板&#xff0c;我事先往Flash里面存了两个bit&#xff0c…

笔记 7 :linux 011 注释,函 bread () , get_hash_table () , find_buffer ()

&#xff08;57&#xff09;接着介绍另一个读盘块的函数 bread&#xff08;&#xff09;&#xff1a; &#xff08;58&#xff09;因为 函数 get_blk&#xff08;&#xff09;大量调用了其它函数&#xff0c;一版面列举不完&#xff0c;故对其调用的函数先行注释&#xff1a;ge…

分布式搜索引擎ES-Elasticsearch进阶

1.head与postman基于索引的操作 引入概念&#xff1a; 集群健康&#xff1a; green 所有的主分片和副本分片都正常运行。你的集群是100%可用 yellow 所有的主分片都正常运行&#xff0c;但不是所有的副本分片都正常运行。 red 有主分片没能正常运行。 查询es集群健康状态&…

PyQt5中pyqtgraph鼠标获取坐标

PyQt5中pyqtgraph鼠标获取坐标 1、效果 2、流程 安装库: pip install numpy==1.19.5 pip install PyQt5==5.15.9 pip install pyqtgraph==0.11.11、创建一个ui 2、在ui中添加一个Vertical Layout控件,命名为my_view 3、把ui转成py 4、绑定鼠标移动事件 5、x,y值向下取整 6…

Linux下vim编辑器的使用方法

Vim编辑器 vim kk 使用vim来创建或编辑 kk文件 一般模式下的操作 x 为向后删除一个字符 nx 连续向后删除n个字符 dd 删除光标所在行 ndd 删除光标所在的向下n行 yy 复制光标所在的那一行 nyy 复制光标所在的向下n列 p 将已复制的数据在光标下一行粘贴上 P 则为贴在光标的上一…

vscode 中python 支持自动跳转

随笔记录 目录 1. 背景介绍 2. 解决方案 1. 背景介绍 vscode 远程ssh 打开python 脚本无法自动跳转 2. 解决方案 安装python 插件即可。 至此&#xff0c;已完成vscode 上py 文件支持自动跳转功能

如何获得Cesium的TileSet并设置本地服务器的Url

一.总体思路 首先使用管理者获得TileSet&#xff0c;通过JSON文件读写&#xff0c;调用对应的Cesium内部提供的函数。 UE5中Json文件的读取与解析 - 知乎 (zhihu.com) 不太了解JSON的可以学习这个。 二.具体实现 1.创建Actor,并且 如何获得Cesium的TileSet,设置本地Url 一…

鸿蒙Navigation路由能力汇总

基本使用步骤&#xff1a; 1、新增配置文件router_map&#xff1a; 2、在moudle.json5中添加刚才新增的router_map配置&#xff1a; 3、使用方法&#xff1a; 属性汇总&#xff1a; https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-basic-compone…

Docker安装mysql详细教程, mysqld: Can‘t read dir of ‘/etc/mysql/conf.d/‘(已解决)

文章目录 一、下载MySQL的docker镜像二、启动MySQL容器2.1 命令2.2 报错mysqld: Cant read dir of /etc/mysql/conf.d/ (Errcode: 2 - No such file or directory) 三、进入mysql容器四、修改mysql默认配置4.1 查看mysql挂载的文件夹4.2 mysql配置 五、补充 如果还没在虚拟机/服…

41 QOS技术(服务质量)

1 QOS 产生背景 对于网络业务&#xff0c;影响服务质量的因素包括传输的带宽、传送的时延、数据的丢包率等。网络资源总是有限的&#xff0c;只要存在抢夺网络资源的情况&#xff0c;就会出现服务质量的要求网络总带宽固定的情况下&#xff0c;如果某类业务占用的带宽越多&am…

redis server response timeout(3000ms) occurred after 3 retry attempts异常分析

读取redis数据报超时错误&#xff1a; Redis server response timeout (3000 ms) occured after 3 retry attempts2024-07-18 17:07:57.124 ERROR [e8f07b0a671c08311dff589827897232] [http-nio-9528-exec-6] c.z.i.u.m.c.e.BaspUserExceptionHandler.exceptionHandler:83 - R…

【贪心算法】力扣1481.不同整数的最少数目

给你一个整数数组 arr 和一个整数 k 。现需要从数组中恰好移除 k 个元素&#xff0c;请找出移除后数组中不同整数的最少数目。 示例 1&#xff1a; 输入&#xff1a;arr [5,5,4], k 1 输出&#xff1a;1 解释&#xff1a;移除 1 个 4 &#xff0c;数组中只剩下 5 一种整数。…