How to use @Data annotation in spring

  1. When using spring, in order to achieve class injection and attribute value injection, methods such as set and get are generally declared in the defined class. Each attribute value in the class needs to be declared, which causes redundancy.
  2. After using the @Data annotation, you can just add it to the directly defined class, and the set, get and other methods of all attributes in the class will be automatically implemented, which greatly simplifies the code.
  3. This class provides get, set, equals, hashCode, canEqual, toString methods
  • 1. Code configuration when using

  • Add dependency package
  • <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.12</version>
    </dependency>

    Add @Data annotation

  • @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Books {
        private int bookID;
        private String bookName;
        private int bookCounts;
        private String detail;
    }
    

    Let’s explain the other two annotations in the entity class
    @AllArgsConstructor  : Annotation is on the class, and it is constructed with parameters
    @NoArgsConstructor : Annotation is on the class, and it is constructed without parameters

  • generated results

  • 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. Advantages and Disadvantages

advantage                                                                                                                                           

  1. Automatically generate constructors, getters/setters, equals, hashcode, toString and other methods, which improves development efficiency to a certain extent.
  2. Make code simpler                                                                                                                

shortcoming

  1. Overloading of multiple parameter constructors is not supported
  2. Too many plug-ins can easily reduce the comfort level of reading source code.

Guess you like

Origin blog.csdn.net/weixin_46996561/article/details/125268876