• Lombok的使用


    1. Lombok项目是一个Java库

       Lombok是一种Java实用工具,可用来帮助开发人员消除Java中的冗长代码,尤其是对于简单的Java对象(POJO),它通过注解实现这一目的。JSR269,其在编译阶段生效
    

    2. Lombok的安装

    1. JDK环境

    拷贝jar到类路径
    javac-cp lombok.jar.....

    2. IDEA 下找到 plugin


    导入依赖

     <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>
    

    3.常用注解

    1. @Getter/@Setter

    1. 插入形式

    适用位置: 类(全局) 字段

    @Setter  //全局  
    public class User {
        private  Integer uid;
        private  String username;
    }
    

    2. 可以指定生成set/get方法的权限

    如 指定为set方法为protect权限

    3. @Getter/@Setter只对类成员产生效果,对静态变量没有任何影响,常量只有get方法生成

    @Getter
    @Setter(AccessLevel.PROTECTED)
    public class User {
        private  Integer uid;
        private  String username;
        static String motherland="中国";
        final Double P=3.14;
    }
    

    生成代码

    public class User {
        private Integer uid;
        private String username;
        static String motherland = "中国";
        final Double P = 3.14D;
    
        public User() {
        }
    
        public Integer getUid() {
            return this.uid;
        }
    
        public String getUsername() {
            return this.username;
        }
    
        public Double getP() {
            return this.P;
        }
    
        protected void setUid(Integer uid) {
            this.uid = uid;
        }
    
        protected void setUsername(String username) {
            this.username = username;
        }
    }
    
    

    4. 排除某个字段 不让其生成get方法,这个单独在某个字段设置权限,局部优先原则

    @Getter
    @Setter(AccessLevel.PROTECTED)
    public class User {
        private  Integer uid;
        @Getter(AccessLevel.NONE)
        private  String username;
        static String motherland="中国";
        final Double P=3.14;
    }
    

    2. @ToString

    适用场景: 类

    1. 现象:static修改变量 不会出现在toString

    2. 排除某个字段,不想让其出现在toString里面---- exclude

    3. 指定出现的字段---- of

    3. @EqualsAndHashCode

    • 生成的方法:equals、canEqual、hashCode常常用做对象的比较
    • exclude 和of 同样适用
    @EqualsAndHashCode(of={"uid","username"})
    public class User {
        private  Integer uid;
        private  String username;
        static String motherland="中国";
        final Double P=3.14D;
    }
    

    生成代码:

    public class User {
        private Integer uid;
        private String username;
        static String motherland = "中国";
        final Double P = 3.14D;
    
        public User() {
        }
    
        public boolean equals(Object o) {
            if (o == this) {
                return true;
            } else if (!(o instanceof User)) {
                return false;
            } else {
                User other = (User)o;
                if (!other.canEqual(this)) {
                    return false;
                } else {
                    Object this$uid = this.uid;
                    Object other$uid = other.uid;
                    if (this$uid == null) {
                        if (other$uid != null) {
                            return false;
                        }
                    } else if (!this$uid.equals(other$uid)) {
                        return false;
                    }
    
                    Object this$username = this.username;
                    Object other$username = other.username;
                    if (this$username == null) {
                        if (other$username != null) {
                            return false;
                        }
                    } else if (!this$username.equals(other$username)) {
                        return false;
                    }
    
                    return true;
                }
            }
        }
    
        protected boolean canEqual(Object other) {
            return other instanceof User;
        }
    
        public int hashCode() {
            int PRIME = true;
            int result = 1;
            Object $uid = this.uid;
            int result = result * 59 + ($uid == null ? 43 : $uid.hashCode());
            Object $username = this.username;
            result = result * 59 + ($username == null ? 43 : $username.hashCode());
            return result;
        }
    }
    
    

    4. @NonNull

    出现位置: 构造参数、方法参数,属性字段上

    1. 构造参数上

    2. 方法参数

    3. 属性字段上

    5. @NoArgsConstructor,@AllArgsConstructor,@RequiredArgsConstructor

    1. @NoArgsConstructor 无参构造 和@AllArgsConstructor 所有成员变量构造

    2. @RequiredArgsConstructor 指定参数构造

    条件:
    1.指定为 @NonNull
    2.表示为final.没有赋初始值的

    6. @Data

    囊括:
    @Getter/@Setter、@RequiredArgsConstructor、@ToString、@EqualsAndHashCode

    7. @Builder

    流式编程、函数式编程、链式编程

    生成的方法

    public class User {
        private Integer uid;
        private String username;
        static String motherland = "中国";
        final Double P;
    
        User(Integer uid, String username, Double P) {
            this.uid = uid;
            this.username = username;
            this.P = P;
        }
    
        public static User.UserBuilder builder() {
            return new User.UserBuilder();
        }
    
        public static class UserBuilder {
            private Integer uid;
            private String username;
            private Double P;
    
            UserBuilder() {
            }
    
            public User.UserBuilder uid(Integer uid) {
                this.uid = uid;
                return this;
            }
    
            public User.UserBuilder username(String username) {
                this.username = username;
                return this;
            }
    
            public User.UserBuilder P(Double P) {
                this.P = P;
                return this;
            }
    
            public User build() {
                return new User(this.uid, this.username, this.P);
            }
    
            public String toString() {
                return "User.UserBuilder(uid=" + this.uid + ", username=" + this.username + ", P=" + this.P + ")";
            }
        }
    }
    
    

    测试:

    public class Test {
        public static void main(String[] args) {
            System.out.println(User.builder().uid(1).username("王振强"));
        }
    }
    

    8. @Log

    9. 关键字类型 val 类型推断

    10. @Cleanup

    • 使用完之后帮我进行处理
    • 关闭连接资源
    • 关闭文件流
    public static void main(String[] args) throws IOException {
            @Cleanup FileInputStream fis=new FileInputStream("1.txt");
            @Cleanup FileOutputStream fos=new FileOutputStream("2.txt");
            byte[] b=new byte[1024];
            while(true){
                int r=0;
                if((r=fis.read(b))!=-1){
                    fos.write(b,0,r);
                }
            }
        }
    

    生成代码

     public static void main(String[] args) throws IOException {
            FileInputStream fis = new FileInputStream("1.txt");
    
            try {
                FileOutputStream fos = new FileOutputStream("2.txt");
    
                try {
                    byte[] b = new byte[1024];
    
                    while(true) {
                        int r;
                        do {
                            int r = false;
                        } while((r = fis.read(b)) == -1);
    
                        fos.write(b, 0, r);
                    }
                } finally {
                    if (Collections.singletonList(fos).get(0) != null) {
                        fos.close();
                    }
    
                }
            } finally {
                if (Collections.singletonList(fis).get(0) != null) {
                    fis.close();
                }
    
            }
        }
    
    不停的思考,就会不停的进步
  • 相关阅读:
    微信小程序动态更改样式
    ionic toggle点击返回true/false支持自定义
    ionic 页面传递参数
    ionic 搜索双向数据绑定失效
    关于select的默认样式问题
    nn
    MVC api json 格式
    iis 500 解决方法
    关于qquu8 的主页修改
    CentOS6.5下MAC
  • 原文地址:https://www.cnblogs.com/zhenqk/p/13581880.html
Copyright © 2020-2023  润新知