Lombok的使用
程序员文章站
2022-05-09 10:42:19
...
版权声明:本文为博主原创文章,欢迎转载,转载请注明作者、原文超链接
Lombok可简化实体类的大部分代码。
一.以IDEA为例,安装插件
安装,后重启IDEA。
二.可用的注解
只介绍常用注解:
@Getter和@Setter 提供getter setter方法
@Getter
@Setter
public class User implements Serializable {
private String userName;
private String address;
private int age;
private Date birthday;
}
等同于:
public class User implements Serializable {
private String userName;
private String address;
private int age;
private Date birthday;
public String getUserName() {
return userName;
}
public String getAddress() {
return address;
}
public int getAge() {
return age;
}
public Date getBirthday() {
return birthday;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setAddress(String address) {
this.address = address;
}
public void setAge(int age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
@Data ,以**解的集合
/**
* @see Getter
* @see Setter
* @see RequiredArgsConstructor
* @see ToString
* @see EqualsAndHashCode
* @see lombok.Value
*/
@NotNull 使用在属性上,校验不允许为空
三。测试
user实体类
@Data
public class User implements Serializable {
private String userName;
private String address;
private int age;
private Date birthday;
//如果想在某个属性的get 或set 属性中作操作,可以这么写
public String getUserName() {
return userName+"1111111111";
}
public void setUserName(String userName) {
this.userName = userName;
}
}
测试:
下一篇: 推特给国家相关媒体和*官员账号贴上标签