lombok简介

lombok简介

lombok是java开发的神器,使用注解让实体类pojo还有日志slf4j操作特别方便。

lombok使用方式

(1)idea中使用lombok工具,需要安装lombok插件。大家plugins搜索lombok安装即可,不然,使用lombok会报错。 (2)在Java项目的pom文件中添加依赖,使用注解就可以了。

注解介绍

(1) @Getter/@Setter注解可以针对类的属性字段自动生成Get/Set方法。

public class Pojo{

   @Setter
   [@Getter](https://my.oschina.net/u/3288663)
   private String name;
   //其他代码……
}

(2) @ToString注解,为使用该注解的类生成一个toString方法

@ToString
public class Pojo {
    private String name;
}

(3)@EqualsAndHashCode注解,为使用该注解的类自动生成equals和hashCode方法

@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注解使用建造者模式,为制定参数赋值

@Builder 
public class Pojo { 
    private String name; 	
}	

使用起来非常的方便,满足日常的工作需要。

有问题,请留言!

个人博客地址 https://blog.ailijie.top/archives/lombok

转载于:https://my.oschina.net/wotrd/blog/3079737

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值