Cómo utilizar la anotación @Data en primavera

  1. Cuando se usa Spring, para lograr la inyección de clase y la inyección de valor de atributo, los métodos como set y get generalmente se declaran en la clase definida, y cada valor de atributo en la clase debe declararse, lo que genera redundancia.
  2. Después de usar la anotación @Data, puede simplemente agregarla a la clase definida directamente y los métodos set, get y otros de todos los atributos de la clase se implementarán automáticamente, lo que simplifica enormemente el código.
  3. Esta clase proporciona los métodos get, set, equals, hashCode, canEqual y toString.
  • 1. Configuración del código al usar

  • Agregar paquete de dependencia
  • <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.12</version>
    </dependency>

    Agregar anotación @Data

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

    Expliquemos las otras dos anotaciones en la clase de entidad
    @AllArgsConstructor  : La anotación está en la clase y está construida con parámetros
    @NoArgsConstructor : La anotación está en la clase y está construida sin parámetros

  • resultados generados

  • 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. Ventajas y desventajas

ventaja                                                                                                                                           

  1. Genere automáticamente constructores, getters/setters, iguales, hashcode, toString y otros métodos, lo que mejora la eficiencia del desarrollo hasta cierto punto.
  2. Simplifique el código                                                                                                                

defecto

  1. No se admite la sobrecarga de constructores de múltiples parámetros
  2. Demasiados complementos pueden reducir fácilmente el nivel de comodidad al leer el código fuente.

Supongo que te gusta

Origin blog.csdn.net/weixin_46996561/article/details/125268876
Recomendado
Clasificación