- Springを利用する場合、クラスインジェクションや属性値インジェクションを実現するために、定義したクラス内でsetやgetなどのメソッドを宣言することが一般的であり、クラス内の各属性値を宣言する必要があり、冗長性が生じます。
- @Data アノテーションを使用した後は、直接定義されたクラスに @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; }
エンティティ クラスの他の 2 つのアノテーションについて説明します
@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. メリットとデメリット
アドバンテージ
- コンストラクター、ゲッター/セッター、equals、hashcode、toString などのメソッドを自動生成するため、開発効率がある程度向上します。
- コードをよりシンプルにする
欠点がある
- 複数のパラメーター コンストラクターのオーバーロードはサポートされていません
- プラグインが多すぎると、ソース コードを読む快適さのレベルが簡単に低下します。