• 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
  • 相关阅读:
    linux下mysql的安装
    linux下mysql设置主从
    linux下安装jdk8并且配置环境变量
    C#实现rabbitmq 延迟队列功能
    对angular.js的一点理解
    angular.js的路由和模板在asp.net mvc 中的使用
    通过Web Api 和 Angular.js 构建单页面的web 程序
    Orchard运用
    Orchard运用
    Orchard运用
  • 原文地址:https://www.cnblogs.com/mr-yang-localhost/p/9330349.html
Copyright © 2020-2023  润新知