Spring Boot 依赖之 lombok的@Data注解
一、创建 Spring Boot 项目
- 启动 IntelliJ IDEA,点击
File
->New
->Project...
- 在新项目对话框中,选择
Spring Initializr
,点击Next
- 配置 Spring Initializr 项目元数据,如 Group=com.dependencies 和 Artifact=lombok
- 选择 Spring Boot 版本,点击
Next
- 在依赖选项中选择以下依赖:
lombok
- 点击
Next
->Finish
编译之后的代码
1、代码编译之后,字节码文件decompile反编译之后的内容。其实这也就是不加注解需要手动写的,很多setter getter太多太繁琐。
package com.javastudy.maven.pojo;
public class User {
private Integer id;
private String username;
private String password;
private String email;
public void setId(Integer id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setEmail(String email) {
this.email = email;
}
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof User))
return false;
User other = (User)o;
if (!other.canEqual(this))
return false;
Object this$id = getId(), other$id = other.getId();
if ((this$id == null) ? (other$id != null) : !this$id.equals(other$id))
return false;
Object this$username = getUsername(), other$username = other.getUsername();
if ((this$username == null) ? (other$username != null) : !this$username.equals(other$username))
return false;
Object this$password = getPassword(), other$password = other.getPassword();
if ((this$password == null) ? (other$password != null) : !this$password.equals(other$password))
return false;
Object this$email = getEmail(), other$email = other.getEmail();
return !((this$email == null) ? (other$email != null) : !this$email.equals(other$email));
}
protected boolean canEqual(Object other) {
return other instanceof User;
}
public int hashCode() {
int PRIME = 59;
result = 1;
Object $id = getId();
result = result * 59 + (($id == null) ? 43 : $id.hashCode());
Object $username = getUsername();
result = result * 59 + (($username == null) ? 43 : $username.hashCode());
Object $password = getPassword();
result = result * 59 + (($password == null) ? 43 : $password.hashCode());
Object $email = getEmail();
return result * 59 + (($email == null) ? 43 : $email.hashCode());
}
public String toString() {
return "User(id=" + getId() + ", username=" + getUsername() + ", password=" + getPassword() + ", email=" + getEmail() + ")";
}
public User(Integer id, String username, String password, String email) {
this.id = id;
this.username = username;
this.password = password;
this.email = email;
}
public User() {}
public Integer getId() {
return this.id;
}
public String getUsername() {
return this.username;
}
public String getPassword() {
return this.password;
}
public String getEmail() {
return this.email;
}
}
二、Java源代码
java源文件,使用@Data注解之后的代码:
package com.javastudy.maven.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author zhizhou 2024/6/27 17:37
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String username;
private String password;
private String email;
}
三、引入lombok
引入lombok
要使用 @Data 注解要先引入lombok,lombok 是什么,它是一个工具类库,可以用简单的注解形式来简化代码,提高开发效率。
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
在编译器中添加插件
这里以IDEA为例,在setting的plugin里搜索 lombok plugin,安装插件,安装完之后就可以在IDEA的Structure结构中看到@Data注解的效果。
安装完之后就可以在IDEA的Structure结构中看到@Data注解的效果。
总结:
lombok 注解 @Data 一个组合注解,包含 @Getter、@Setter、@ToString、@EqualsAndHashCode 和 @RequiredArgsConstructor等。通过创建各种实体类并使用 Lombok 注解来减少样板代码,提高开发效率。降本增效这确实是个好方法,特别是一些相对比较老的项目,重构优化的空间会很大。
标签:username,String,Spring,Boot,id,other,lombok,password,email From: https://blog.csdn.net/ahauedu/article/details/140968142