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

 

转载于:https://www.cnblogs.com/mr-yang-localhost/p/9330349.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值