Lombok 常用的注解:
private final Logger logger = LoggerFactory.getLogger(XXX.class);
这句代码的。使用的日志框架是LogBack@Log4j该注解也是用来解决不用每次都写日志对象声明语句的,从字面上也可以看出,使用的日志框架是log4j@Data该注解是 @ToString、@EqualsAndHashCode注解,和所有属性的@Getter注解, 以及所有non-final属性的@Setter注解的组合,通常情况下,我们使用这个注解就足够了。
以上只列出了部分常用注解,更多注解的使用方式,请参考 官网关于注解的文档
package .huawei.guanggao.pojo;import lombok.*;import java.io.Serializable;
import java.util.Date;@Getter
@Setter//全参构造函数
@AllArgsConstructor
//无参构造函数
@NoArgsConstructor//不包括某个字段
//@ToString(exclude = {"pwd","phone"})
//只输出某个字段
//@ToString(of = {"pwd","phone"})
@ToString//作用于类,覆盖默认的equals和hashCode, 作用于全部属性
@EqualsAndHashCode//不包括某个属性
@EqualsAndHashCode(exclude = {"age"})//只输出某个属性
@EqualsAndHashCode(of = {"name"})//构造者模式 (Goods goods = Goods.builder().name("硒鼓").id(3).build();)
@Builder
public class Goods implements Serializable {private static final long serialVersionUID = -1939317434177299884L;/*** final 只会生成get*/private final String biaoti="史蒂芬孙";/*** 下面两个静态成员变量不会生成set/get方法*/static Date createTime = new Date();private static final String address = "广东省广州市";private Integer id;private String name;/*** 不想生成 get ,set 方法*/@Getter(value = AessLevel.NONE)@Setter(value = AessLevel.NONE)private String pwd;/*** 控制访问权限*/@Getter(value =AessLevel.PROTECTED)private String phone;private Integer stock;}
@Data, 组合注解,相当于以下注解的集合 (注意:全参构造用不了,可以用@Builder的方式)
@ToString
@EqualsAndHashCode
@Getter
@Setter
@RequiredArgsConstructor
@Slf4j或@Log4j注解,日志使用
log.info("日志");
编译后的文件,有些代码省略了,大概就是这个意思
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower depiler)
//package .huawei.guanggao.pojo;import java.io.Serializable;
import java.util.Date;public class Goods implements Serializable {private static final long serialVersionUID = -1939317434177299884L;private final String biaoti = "史蒂芬孙";static Date createTime = new Date();private static final String address = "广东省广州市";private Integer id;private String name;private String pwd;private String phone;private Integer stock;public static Goods.GoodsBuilder builder() {return new Goods.GoodsBuilder();}public String getBiaoti() {this.getClass();return "史蒂芬孙";}public Integer getId() {return this.id;}public String getName() {return this.name;}public Integer getStock() {return this.stock;}public void setId(final Integer id) {this.id = id;}public void setName(final String name) {this.name = name;}public void setPhone(final String phone) {this.phone = phone;}public void setStock(final Integer stock) {this.stock = stock;}public Goods(final Integer id, final String name, final String pwd, final String phone, final Integer stock) {this.id = id;this.name = name;this.pwd = pwd;this.phone = phone;this.stock = stock;}public Goods() {}public String toString() {return "Goods(biaoti=" + this.getBiaoti() + ", id=" + this.getId() + ", name=" + this.getName() + ", pwd=" + this.pwd + ", phone=" + this.getPhone() + ", stock=" + this.getStock() + ")";}protected String getPhone() {return this.phone;}public static class GoodsBuilder {private Integer id;private String name;private String pwd;private String phone;private Integer stock;GoodsBuilder() {}public Goods.GoodsBuilder id(final Integer id) {this.id = id;return this;}public Goods.GoodsBuilder name(final String name) {this.name = name;return this;}public Goods.GoodsBuilder pwd(final String pwd) {this.pwd = pwd;return this;}public Goods.GoodsBuilder phone(final String phone) {this.phone = phone;return this;}public Goods.GoodsBuilder stock(final Integer stock) {this.stock = stock;return this;}public Goods build() {return new Goods(this.id, this.name, this.pwd, this.phone, this.stock);}public String toString() {return "Goods.GoodsBuilder(id=" + this.id + ", name=" + this.name + ", pwd=" + this.pwd + ", phone=" + this.phone + ", stock=" + this.stock + ")";}}
}
更多推荐
注解,示例,常见,简单,lombok
发布评论