lombok简介及在eclipse和idea中的配置lombok

目录

1. lombok简介

官网简介:

官网地址:Project Lombok

2.lombok在idea中的使用

3. lombok在eclipse中的使用

4.总结


 

1. lombok简介

官网简介:

        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.

         Project Lombok 是一个 Java 库,可自动插入您的编辑器并构建工具,为您的 Java 增添趣味。
        永远不要再编写另一个 getter 或 equals 方法,通过一个注释,您的类就有一个功能齐全的构建器,自动化您的日志变量等等。

官网地址:Project Lombok

2.lombok在idea中的使用

1.在idea的插件市场里搜索lombok,安装图片中的这个。

2.新建一个maven项目,选择quickstar即可,在pom文件中加入lombok的依赖

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

3.新建一个实体类,这里我以Student为例

public class Student {
    private int id;
    private String name;
    private String address;
    private int age;
    private String email;
}

 4.加入lombok的注解

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private int id;
    private String name;
    private String address;
    private int age;
    private String email;
}

说明:

@Data:作用相当于写了set,get,toString方法

/**
 * Generates getters for all fields, a useful toString method, and hashCode and equals implementations that check
 * all non-transient fields. Will also generate setters for all non-final fields, as well as a constructor.
 * <p>
 * Equivalent to {@code @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode}.
 * <p>
 * Complete documentation is found at <a href="https://projectlombok.org/features/Data">the project lombok features page for &#64;Data</a>.
 * 
 * @see Getter
 * @see Setter
 * @see RequiredArgsConstructor
 * @see ToString
 * @see EqualsAndHashCode
 * @see lombok.Value
 */

@AllArgsConstructor:全参构造方法

/**
 * Generates an all-args constructor.
 * An all-args constructor requires one argument for every field in the class.
 * <p>
 * Complete documentation is found at <a href="https://projectlombok.org/features/Constructor">the project lombok features page for &#64;Constructor</a>.
 * <p>
 * Even though it is not listed, this annotation also has the {@code onConstructor} parameter. See the full documentation for more details.
 * 
 * @see NoArgsConstructor
 * @see RequiredArgsConstructor
 */

@NoArgsConstructor:无参构造方法

/**
 * Generates a no-args constructor.
 * Will generate an error message if such a constructor cannot be written due to the existence of final fields.
 * <p>
 * Complete documentation is found at <a href="https://projectlombok.org/features/Constructor">the project lombok features page for &#64;Constructor</a>.
 * <p>
 * Even though it is not listed, this annotation also has the {@code onConstructor} parameter. See the full documentation for more details.
 * <p>
 * NB: Fields with constraints such as {@code @NonNull} will <em>NOT</em> be checked in a {@code @NoArgsConstructor} constructor, of course!
 * 
 * @see RequiredArgsConstructor
 * @see AllArgsConstructor
 */

@ToString:相当于toString方法,一般来讲,写了@Data就不用写这个了

/**
 * Generates an implementation for the {@code toString} method inherited by all objects, consisting of printing the values of relevant fields.
 * <p>
 * Complete documentation is found at <a href="https://projectlombok.org/features/ToString">the project lombok features page for &#64;ToString</a>.
 */

其他的注解可以自己去看

5.创建一个测试方法

import org.example.domain.Student;
import org.junit.Test;

public class MyTest {
   
    @Test
    public void test01(){
        // 测试无参构造
        Student student = new Student();
        System.out.println(student);

        // 测试全参构造
        Student student1 = new Student(1001, "zhangsan", "china", 18, "123@qq.com");
        System.out.println(student1);

        // 测试set, get方法
        Student student2 = new Student();
        System.out.println(student2);
        student2.setId(1002);
        student2.setName("lisi");
        System.out.println(student2);
    }
}

输出结果

Student(id=0, name=null, address=null, age=0, email=null)
Student(id=1001, name=zhangsan, address=china, age=18, email=123@qq.com)
Student(id=0, name=null, address=null, age=0, email=null)
Student(id=1002, name=lisi, address=null, age=0, email=null)

3. lombok在eclipse中的使用

1.下载lombok的插件,网上有教程

Eclipse安装lombok插件 - boonya - 博客园

2.进行测试,大致步骤和idea类似,还是先创建一个maven的java项目,然后添加lombok的依赖进行测试。

4.总结

Lombok项目是一个Java库,它会自动插入编辑器和构建工具中,Lombok提供了一组有用的注释,用来消除Java类中的大量样板代码。仅五个字符(@Data)就可以替换数百行代码从而产生干净,简洁且易于维护的Java类。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

别来BUG求求了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值