Lombok开启链式编程@Accessors注解

@Accessors(chain = true) 是 Lombok 注解,用于启用链式编程。它的作用是为带有 @Data 注解的类自动生成链式调用方法。

在你的实体类中,如果要启用链式编程,只需在类级别上添加 @Accessors(chain = true) 注解即可。修改后的实体类代码如下所示:

@ExcelProperty注解是easyexecl使用到了 这里无视即可

import com.alibaba.excel.annotation.ExcelProperty;
import com.woniu.common.utils.StringConverter;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

@AllArgsConstructor
@NoArgsConstructor
@Data
@Accessors(chain = true)//链式编程
public class OperationLog {
    //    @JsonFormat(shape = JsonFormat.Shape.STRING)
    @ExcelProperty(value = "id", converter = StringConverter.class,index = 0)
    private String id; //日志id
    @ExcelProperty(value = "操作人",index = 1)
    private String userCode; //操作人
    @ExcelProperty(value = "操作ip",index = 2)
    private String ip; //操作ip
    @ExcelProperty(value = "操作类型",index = 3)
    private String type; //操作类型
    @ExcelProperty(value = "操作名称",index = 4)
    private String description; //操作名称
    @ExcelProperty(value = "操作模块",index = 5)
    private String model; //操作模块
    @ExcelProperty(value = "操作时间",index = 6)
    private String operationTime; //操作时间
    @ExcelProperty(value = "操作结果",index = 7)
    private String result; //操作结果
}

添加了 @Accessors(chain = true) 注解后,你可以使用链式编程来设置实体类的属性值,例如:

OperationLog log = new OperationLog()
    .setId("1")
    .setUserCode("admin")
    .setIp("127.0.0.1")
    .setType("操作类型")
    .setDescription("操作名称")
    .setModel("操作模块")
    .setOperationTime("2023-06-16 20:44:00")
    .setResult("操作结果");

这样可以提高代码的可读性和简洁性。

 通过target文件下可以看出是将set方法改写了~

猜你喜欢

转载自blog.csdn.net/lps12345666/article/details/131254800