lombok常见注解

Posted mr-yang-localhost

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lombok常见注解相关的知识,希望对你有一定的参考价值。

一、使用lombok简化代码

lombok提供了很多注解,在编译时候生成java代码,代替了手工编写一些简单的代码,使程序员可以关注更重要的实现。

二、常用注解

以model为例

public class DataDemo {
    private Integer id;
    private String name;
    private Date time;
}

一下是添加不同lombok注解的编译结果示例,编译结果很简单,不需要做什么说明,直接上代码:

@Getter / @Setter

技术分享图片
public class GetterSetterDemo {
    private Integer id;
    private String name;
    private Date time;

    public GetterSetterDemo() {  }

    public Integer getId() { return this.id; }

    public String getName() { return this.name; }

    public Date getTime() { return this.time; }

    public void setId(Integer id) { this.id = id; }

    public void setName(String name) { this.name = name; }

    public void setTime(Date time) { this.time = time; }
}
View Code

@ToString

model上添加注解:@ToString(exclude = {"id"}, includeFieldNames = false)

技术分享图片
public class ToStringDemo {
    private Integer id;
    private String name;
    private Date time;

    public ToStringDemo() {  }

    public String toString() {
        return "ToStringDemo(" + this.name + ", " + this.time + ")";
    }
}
View Code

@Data

@Data 注解相当于 Getter + Setter + ToString + @RequiredArgsConstrutor,可以用在pojo上

技术分享图片
public class DataDemo {
    private Integer id;
    private String name;
    private Date time;

    public DataDemo() {  }

    public Integer getId() { return this.id;}

    public String getName() { return this.name; }

    public Date getTime() { return this.time; }

    public void setId(Integer id) { this.id = id; }

    public void setName(String name) { this.name = name; }

    public void setTime(Date time) { this.time = time; }

    public boolean equals(Object o) { ... }

    protected boolean canEqual(Object other) { ... }

    public int hashCode() { ... }

    public String toString() {
        return "DataDemo(id=" + this.getId() + ", name=" + this.getName() + ", time=" + this.getTime() + ")";
    }
}
View Code

@Builder

技术分享图片
public class BuilderDemo {
    private Integer id;
    private String name;
    private Date time;

    @ConstructorProperties({"id", "name", "time"})
    BuilderDemo(Integer id, String name, Date time) {
        this.id = id;
        this.name = name;
        this.time = time;
    }

    public static BuilderDemo.BuilderDemoBuilder builder() {
        return new BuilderDemo.BuilderDemoBuilder();
    }

    public static class BuilderDemoBuilder {
        private Integer id;
        private String name;
        private Date time;

        BuilderDemoBuilder() {
        }

        public BuilderDemo.BuilderDemoBuilder id(Integer id) {
            this.id = id;
            return this;
        }

        public BuilderDemo.BuilderDemoBuilder name(String name) {
            this.name = name;
            return this;
        }

        public BuilderDemo.BuilderDemoBuilder time(Date time) {
            this.time = time;
            return this;
        }

        public BuilderDemo build() {
            return new BuilderDemo(this.id, this.name, this.time);
        }

        public String toString() {
            return "BuilderDemo.BuilderDemoBuilder(id=" + this.id + ", name=" + this.name + ", time=" + this.time + ")";
        }
    }
}
View Code

构造函数

@AllArgsConstructor    全部参数构造函数

@NoArgsConstructor   无参数构造函数

@RequiredArgsConstructor   NoNull参数和常量构造函数

技术分享图片
/**@AllArgsConstructor*/
public class AllArgsConstructorDemo {
    private Integer id;
    private String name;
    private Date time;

    @ConstructorProperties({"id", "name", "time"})
    public AllArgsConstructorDemo(Integer id, String name, Date time) {
        this.id = id;
        this.name = name;
        this.time = time;
    }
}


/**@NoArgsConstructor*/
public class NoArgsConstructorDemo {
    private Integer id;
    private String name;
    private Date time;

    public NoArgsConstructorDemo() {  }
}


/**@RequiredArgsConstructor(staticName = "of")*/
public class RequiredArgsConstructorDemo {
    private Integer id;
    private String name;
    private Date time;

    private RequiredArgsConstructorDemo() {  }

    public static RequiredArgsConstructorDemo of() {
        return new RequiredArgsConstructorDemo();
    }
}

/**@RequiredArgsConstructor(staticName = "of")*/
public class RequiredArgsConstructorDemo {
    private Integer id;
    @NonNull
    private String name;
    @NonNull
    private Date time;

    @ConstructorProperties({"name", "time"})
    private RequiredArgsConstructorDemo(@NonNull String name, @NonNull Date time) {
        if(name == null) {
            throw new NullPointerException("name");
        } else if(time == null) {
            throw new NullPointerException("time");
        } else {
            this.name = name;
            this.time = time;
        }
    }

    public static RequiredArgsConstructorDemo of(@NonNull String name, @NonNull Date time) {
        return new RequiredArgsConstructorDemo(name, time);
    }
}
View Code

@Value

技术分享图片
public final class ValueDemo {
    private final Integer id;
    private final String name;
    private final Date time;

    @ConstructorProperties({"id", "name", "time"})
    public ValueDemo(Integer id, String name, Date time) {
        this.id = id;
        this.name = name;
        this.time = time;
    }

    public Integer getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public Date getTime() {
        return this.time;
    }

    public boolean equals(Object o) { ... }

    public int hashCode() { ... }

    public String toString() {
        return "ValueDemo(id=" + this.getId() + ", name=" + this.getName() + ", time=" + this.getTime() + ")";
    }
}
View Code

 

以上是关于lombok常见注解的主要内容,如果未能解决你的问题,请参考以下文章

java 编译时注解框架 lombok-ex

Lombok之使用详解

idea怎么看lombok注解自动生成的代码?

lombok-ex 编译时注解框架,性能完爆 AOP

lombok-ex 编译时注解框架,性能完爆 AOP

lombok-ex 编译时注解框架,性能完爆 AOP