lombok学习


前言

在开发中创建对象需要给对象创建get set方法,虽然有快捷键但是当项目创建对象非常多时,就很麻烦,影响开发效率


一、lombok是什么?

百度百科:Lombok项目是一个Java库,它会自动插入编辑器和构建工具中,Lombok提供了一组有用的注释,用来消除Java类中的大量样板代码。仅五个字符(@Data)就可以替换数百行代码从而产生干净,简洁且易于维护的Java类。
官方定义:Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.
Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more
通俗解释:lombok快速开发工具,提供了一组java相关注解,通过注解来更快的生成java对象中的set get方法 toString 等

二、使用lombok

1.引入依赖

  <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.16.18</version>
      <type>pom</type>
    </dependency>

2.使用提供的注解

import lombok.Data;
@Data
public class Users {
    private Integer id;
    private String name;
    private Integer age;
    private Date bir;
}

public class TestUser {
    public static void main(String[] args) {
        Users users = new Users();
        users.setId(1);
        users.setAge(12);
        users.setName("aa");
        users.setBir(new Date());
        System.out.println(users);
    }
}
// 打印结果 :users(id=1, name=aa, age=12, bir=Sun Apr 03 22:11:59 CST 2022)
@Data 注解:自动给对象提供get set toString hashCode equals 方法

对应的class文件

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.baizhi.entity;

import java.util.Date;

public class Users {
    private Integer id;
    private String name;
    private Integer age;
    private Date bir;

    public Users() {
    }

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

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

    public Integer getAge() {
        return this.age;
    }

    public Date getBir() {
        return this.bir;
    }

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

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

    public void setAge(Integer age) {
        this.age = age;
    }

    public void setBir(Date bir) {
        this.bir = bir;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Users)) {
            return false;
        } else {
            Users other = (Users)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label59: {
                    Object this$id = this.getId();
                    Object other$id = other.getId();
                    if (this$id == null) {
                        if (other$id == null) {
                            break label59;
                        }
                    } else if (this$id.equals(other$id)) {
                        break label59;
                    }

                    return false;
                }

                Object this$age = this.getAge();
                Object other$age = other.getAge();
                if (this$age == null) {
                    if (other$age != null) {
                        return false;
                    }
                } else if (!this$age.equals(other$age)) {
                    return false;
                }

                Object this$name = this.getName();
                Object other$name = other.getName();
                if (this$name == null) {
                    if (other$name != null) {
                        return false;
                    }
                } else if (!this$name.equals(other$name)) {
                    return false;
                }

                Object this$bir = this.getBir();
                Object other$bir = other.getBir();
                if (this$bir == null) {
                    if (other$bir != null) {
                        return false;
                    }
                } else if (!this$bir.equals(other$bir)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof Users;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $id = this.getId();
        int result = result * 59 + ($id == null ? 43 : $id.hashCode());
        Object $age = this.getAge();
        result = result * 59 + ($age == null ? 43 : $age.hashCode());
        Object $name = this.getName();
        result = result * 59 + ($name == null ? 43 : $name.hashCode());
        Object $bir = this.getBir();
        result = result * 59 + ($bir == null ? 43 : $bir.hashCode());
        return result;
    }

    public String toString() {
        return "Users(id=" + this.getId() + ", name=" + this.getName() + ", age=" + this.getAge() + ", bir=" + this.getBir() + ")";
    }
}


3. lombok原理

  • javac对.java源文件进行分析,生成java语法树

  • lombok在编译过程中通过注解修改语法树

  • 生成.class字节码文件

4. lombok一组注解

  1. @Data用在类上 生成get set toString 方法
  2. @Getter @Setter 用在类上 用来只生成对象的get set 方法
  3. @ToString 用在类上 用来只生成对象的toString 方法
  4. @AllArgsConstructor 和 @NoArgsConstructor 有参构造和无参构造同时使用
  5. 不能生成单独参数的构造方法,需要自己单独写
  6. @Accessors 用在类上,用来给类中set方法开启链式调用chain属性,用来指定是否开启set方法的链式调用,true是开启,false 不开启
  7. @slf4j 用在类上,用来快速给类中定义一个日志变量:原理:相当于在类上加入了日志对象
    private Logger log = LoggerFactory.getLogger(TestUser.class);
@Accessors(chain = true)//
public class Users {
    private Integer id;
    private String name;
    private Integer age;
    private Date bir;
}
public class TestUser {
    public static void main(String[] args) {
        Users users = new Users();
        users.setBir(new Date()).setAge(12).setName("222").setId(12);
        System.out.println(users);
    }
}
// Users(id=12, name=222, age=12, bir=Sun Apr 03 22:39:41 CST 2022)
@Controller
@RequestMapping("user")
@Slf4j
public class UserController {
    @RequestMapping("findAll")
    public String findAll(){
        log.info("111");
        return "index";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值