- 使用spring中,为了实现类的注入和属性值注入,一般会在定义的类里声明set,get等方法,需要对类里的每个属性值都需声明造成比较冗余,
- 使用了@Data注解后,就可以在直接定义的类添加即可,就会自动实现类里所有属性的set,get等方法,极大的简化了代码
- 该类提供了get、set、equals、hashCode、canEqual、toString方法
-
1.使用时代码配置
- 添加依赖包
-
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency>
添加@Data注解
-
@Data @AllArgsConstructor @NoArgsConstructor public class Books { private int bookID; private String bookName; private int bookCounts; private String detail; }
讲解一下实体类中另外两个注解
@AllArgsConstructor
: 注解在类上,有参构造@NoArgsConstructor
: 注解在类上,无参构造 -
生成的结果
-
package com.kk.pojo; public class Books { private int bookID; private String bookName; private int bookCounts; private String detail; public int getBookID() { return this.bookID; } public String getBookName() { return this.bookName; } public int getBookCounts() { return this.bookCounts; } public String getDetail() { return this.detail; } public void setBookID(int bookID) { this.bookID = bookID; } public void setBookName(String bookName) { this.bookName = bookName; } public void setBookCounts(int bookCounts) { this.bookCounts = bookCounts; } public void setDetail(String detail) { this.detail = detail; } public boolean equals(Object o) { if (o == this) { return true; } else if (!(o instanceof Books)) { return false; } else { Books other = (Books)o; if (!other.canEqual(this)) { return false; } else if (this.getBookID() != other.getBookID()) { return false; } else { label41: { Object this$bookName = this.getBookName(); Object other$bookName = other.getBookName(); if (this$bookName == null) { if (other$bookName == null) { break label41; } } else if (this$bookName.equals(other$bookName)) { break label41; } return false; } if (this.getBookCounts() != other.getBookCounts()) { return false; } else { Object this$detail = this.getDetail(); Object other$detail = other.getDetail(); if (this$detail == null) { if (other$detail != null) { return false; } } else if (!this$detail.equals(other$detail)) { return false; } return true; } } } } protected boolean canEqual(Object other) { return other instanceof Books; } public int hashCode() { int PRIME = true; int result = 1; int result = result * 59 + this.getBookID(); Object $bookName = this.getBookName(); result = result * 59 + ($bookName == null ? 43 : $bookName.hashCode()); result = result * 59 + this.getBookCounts(); Object $detail = this.getDetail(); result = result * 59 + ($detail == null ? 43 : $detail.hashCode()); return result; } public String toString() { return "Books(bookID=" + this.getBookID() + ", bookName=" + this.getBookName() + ", bookCounts=" + this.getBookCounts() + ", detail=" + this.getDetail() + ")"; } public Books(int bookID, String bookName, int bookCounts, String detail) { this.bookID = bookID; this.bookName = bookName; this.bookCounts = bookCounts; this.detail = detail; } public Books() { } }
-
3. 优缺点
优点
- 自动生成构造器、getter/setter、equals、hashcode、toString等方法,提高了一定的开发效率
- 让代码变得简洁
缺点
- 不支持多种参数构造器的重载
- 过多的插件容易降低阅读源代码的舒适度