当前位置: 首页 > news >正文

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

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.lryc.cn/news/402445.html

相关文章:

  • 【ASP.NET网站传值问题】“object”不包含“GetEnumerator”的公共定义,因此 foreach 语句不能作用于“object”类型的变量等
  • Stateflow中的状态转换表
  • 结合Redis解决接口幂等性问题
  • 2024算力基础设施安全架构设计与思考(免费下载)
  • ExoPlayer架构详解与源码分析(15)——Renderer
  • 网络安全-等级保护制度介绍
  • 【介绍下大数据组件之Storm】
  • React Hook 总结(React 萌新升级打怪中...)
  • Typora 1.5.8 版本安装下载教程 (轻量级 Markdown 编辑器),图文步骤详解,免费领取
  • mac docker no space left on device
  • 单片机主控的基本电路
  • 【19】读感 - 架构整洁之道(一)
  • 多层全连接神经网络(三)---分类问题
  • 签名优化:请求数据类型不是`application/json`,将只对随机数进行签名计算,例如文件上传接口。
  • PostgreSQL的Json数据类型如何使用
  • SpringData JPA Mongodb 查询部分字段
  • NC65 设置下拉列表框值
  • 小阿轩yx-高性能内存对象缓存
  • 华中师范大学学报人文社会科学版
  • CI/CD的node.js编译报错npm ERR! network request to https://registry.npmjs.org/
  • 用ssh tunnel的方式设置 AWS DocumentDB 公网访问
  • 基于电鸿(电力鸿蒙)的边缘计算网关,支持定制
  • WPF之URI的使用
  • Web开发:ASP.NET CORE前后端交互之AJAX(含基础Demo)
  • 经典神经网络(14)T5模型原理详解及其微调(文本摘要)
  • C语言结构体字节对齐技术详解
  • Linux编辑器——vim的使用
  • Java案例斗地主游戏
  • sqlite|轻量数据库|pgadmin4的sqlite数据库操作--重置密码和账号解锁
  • 【ARMv8/v9 异常模型入门及渐进 9.1 - FIQ 和 IRQ 打开和关闭】