最近在公司项目中,总是会遇到如下写法
例如现有一个实体类People
@Data
@TableName("people")
public class People implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(type= IdType.AUTO)
private Long id;
private String name;
private Integer sex;
}
当我利用set方法向实现类中注入属性值得时候我需要像下面这种繁琐的写法来完成
People people = new People();
people.setSex(1);
people.setName("张三");
可见这种代码的编写方式会随着属性的增多而增加方法的行数,而阿里规范中要避免一个方法内行数超过80行,所以为了有效的减少此类代码的行数,我们可以使用lombok中的@Accessors注解
@Data
@TableName("people")
@Accessors(chain = true)
public class People implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(type= IdType.AUTO)
private Long id;
private String name;
private Integer sex;
}
当使用了这个注解之后,我们在使用set方法就可以按照如下方式进行编写,减少了代码的行数
people.setSex(1).setName("李四");