简单使用lombok 链式编程

    技术2025-04-14  8

    使用条件

    一,导包

    <!--只是用来下载一个jar--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency>

    二,安装插件

    File -> Settings -> Plugins 搜索Lombok,并进行安装

     

    以下参考自

    首先来看一下不加Lombok如何实现链式

    package 方; class Student{ private int age; private String name; public int getAge() { return age; } public Student setAge(int age) { this.age = age; return this; } public String getName() { return name; } public Student setName(String name) { this.name = name; return this; } @Override public String toString() { return "Student{" + "age=" + age + ", name='" + name + '\'' + '}'; } } public class Test { public static void main(String[] args) { Student student = new Student().setAge(18).setName("TAT"); System.out.println( student.toString() ); } }

     

    使用Lombok

    package 方; import lombok.*; import lombok.experimental.Accessors; @Accessors(chain = true) @Data class Student{ private int age; private String name; } public class Test { public static void main(String[] args) { Student student = new Student().setAge(18).setName("TAT"); System.out.println( student.toString() ); } }

    Processed: 0.010, SQL: 9