spring中@Data注解详细解析

目录

  • 前言
  • 1. 源码
    • 1.1 lombok
  • 2. 代码
  • 3. 优缺点

前言

这个注解一般主要的功能是在实体类上面
主要功能是为了
提供类的get、set、equals、hashCode、canEqual、toString方法

1. 源码

通过学习注解上的源码以及官方文档
可以了解更加透彻

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
public @interface Data {
    String staticConstructor() default "";
}

@Data : 注解的目标是 类上
提供类的get、set、equals、hashCode、canEqual、toString方法
如果方法值默认不写是系统默认值

引用这个注解的时候还要加入依赖包才可导入

依赖包如下

1.1 lombok

工具类库,可以用简单的注解形式来简化代码,提高开发效率

<dependency>
    <groupId>org.projectlombokgroupId>
    <artifactId>lombokartifactId>
    <version>1.18.12version>
dependency>

具体可以通过maven库选择版本号

官网

只有结合这个依赖包工具库和注解@Data才可以进入自动注入

2. 代码

记得添加上依赖包
结合@Data注解进行自动注入

主要功能是:丰富java自动资源管理,自动生成getter、setter、equals、hashCode和toString等等

@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等方法,提高了一定的开发效率
  • 让代码变得简洁

缺点:

  • 不支持多种参数构造器的重载
  • 过多的插件容易降低阅读源代码的舒适度

你可能感兴趣的:(java,java,spring,后端)