lombok是java開發的神器,使用註解讓實體類pojo還有日誌slf4j操做特別方便。java
(1)idea中使用lombok工具,須要安裝lombok插件。你們plugins搜索lombok安裝便可,否則,使用lombok會報錯。
(2)在Java項目的pom文件中添加依賴,使用註解就能夠了。ide
(1) @Getter/@Setter註解能夠針對類的屬性字段自動生成Get/Set方法。工具
public class Pojo{ @Setter @Getter private String name; //其餘代碼…… }
(2) @ToString註解,爲使用該註解的類生成一個toString方法ui
@ToString public class Pojo { private String name; }
(3)@EqualsAndHashCode註解,爲使用該註解的類自動生成equals和hashCode方法idea
@EqualsAndHashCode public class Pojo { private String name; }
(4) @NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor,這幾個註解分別爲類自動生成了無參構造器、指定參數的構造器和包含全部參數的構造器。插件
@NoArgsConstructor @AllArgsConstructor public class Pojo { private String name; }
(5)@Data註解做用比較全,其包含註解的集合@ToString,@EqualsAndHashCode,全部字段的@Getter和全部非final字段的@Setter, @RequiredArgsConstructor。其示例代碼能夠參考上面幾個註解的組合。日誌
* @see Getter * @see Setter * @see RequiredArgsConstructor * @see ToString * @see EqualsAndHashCode * @see lombok.Value */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.SOURCE) public @interface Data { /** * If you specify a static constructor name, then the generated constructor will be private, and * instead a static factory method is created that other classes can use to create instances. * We suggest the name: "of", like so: * * <pre> * public @Data(staticConstructor = "of") class Point { final int x, y; } * </pre> * * Default: No static constructor, instead the normal constructor is public. * * @return Name of static 'constructor' method to generate (blank = generate a normal constructor). */ String staticConstructor() default ""; }
(6)@Builder註解使用建造者模式,爲制定參數賦值code
@Builder public class Pojo { private String name; }
使用起來很是的方便,知足平常的工做須要。orm
有問題,請留言!
我的博客地址 https://blog.ailijie.top/arch...blog