Lombok安装与使用

 最近发现了一个能够节省代码的小工具,用了一段时间感觉还不错,特此推荐一下.

lombok介绍:
      lombok是一个可以通过简单的注解的形式来帮助我们简化消除一些必须有但显得很臃肿的 Java 代码的工具.例如,我们在项目中可能不可避免的使用到一些POJO类,这时候我们需要填充应有的字段,并手动建立构造函数,Getter,Setter等方法.而lombok就可以通过@Data注解的方式生成其属性的构造器,Getter,Setter,equals,toString等方法.在编译时这些方法会自动生成,在.class文件中,这些方法和我们手写Getter,Setter等方法是一样的.使用lombok减少了重复的创建那些方法的时间,并且可以使得代码更加简洁.

IntelliJ IDEA安装Lombok(Mac版)  Windows也是相应的流程,只是可能地方不一样.  
    1. IntellJ IDEA  —> Preferences
 2.Plugins  —> Browse repositories
3.搜索Lombok,并点击Install进行下载安装.

这样Lombok插件已经安装完毕了.下面是如何使用

1.在Maven中导入依赖,现在就可以在项目中使用lombok了
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.16.10</version>
</dependency>



2.比较使用lombok和不使用lombok的代码量

普通的POJO类
package com.newtank.scorpio.web.controller.request;

import com.newtank.scorpio.web.query.DataGridFilter;
import com.newtank.scorpio.web.query.QueryType;
import com.newtank.scorpio.web.query.Restrictions;

/**
 * Created by looper on 2017/8/29.
 */
public class UserActionLogFilter extends DataGridFilter {
  @QueryType(value = Restrictions.EQ, column = "id")
  private String id;

  @QueryType(value = Restrictions.EQ, column = "mobile")
  private String  mobile;

  @QueryType(value = Restrictions.EQ, column = "openid")
  private String openid;

  @QueryType(value = Restrictions.EQ, column = "source")
  private String source;

  @QueryType(value = Restrictions.EQ, column = "model")
  private String model;

  @QueryType(value = Restrictions.EQ, column = "action")
  private String action;

  public UserActionLogFilter(){

  }

  public UserActionLogFilter(String id, String mobile, String openid, String source, String model, String action) {
    this.id = id;
    this.mobile = mobile;
    this.openid = openid;
    this.source = source;
    this.model = model;
    this.action = action;
  }

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getMobile() {
    return mobile;
  }

  public void setMobile(String mobile) {
    this.mobile = mobile;
  }

  public String getOpenid() {
    return openid;
  }

  public void setOpenid(String openid) {
    this.openid = openid;
  }

  public String getSource() {
    return source;
  }

  public void setSource(String source) {
    this.source = source;
  }

  public String getModel() {
    return model;
  }

  public void setModel(String model) {
    this.model = model;
  }

  public String getAction() {
    return action;
  }

  public void setAction(String action) {
    this.action = action;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    UserActionLogFilter that = (UserActionLogFilter) o;

    if (id != null ? ! id.equals(that.id) : that.id != null) return false;
    if (mobile != null ? ! mobile.equals(that.mobile) : that.mobile != null) return false;
    if (openid != null ? ! openid.equals(that.openid) : that.openid != null) return false;
    if (source != null ? ! source.equals(that.source) : that.source != null) return false;
    if (model != null ? ! model.equals(that.model) : that.model != null) return false;
    return action != null ? action.equals(that.action) : that.action == null;

  }

  @Override
  public int hashCode() {
    int result = id != null ? id.hashCode() : 0;
    result = 31 * result + (mobile != null ? mobile.hashCode() : 0);
    result = 31 * result + (openid != null ? openid.hashCode() : 0);
    result = 31 * result + (source != null ? source.hashCode() : 0);
    result = 31 * result + (model != null ? model.hashCode() : 0);
    result = 31 * result + (action != null ? action.hashCode() : 0);
    return result;
  }

  @Override
  public String toString() {
    return "UserActionLogFilter{" +
        "id='" + id + '\'' +
        ", mobile='" + mobile + '\'' +
        ", openid='" + openid + '\'' +
        ", source='" + source + '\'' +
        ", model='" + model + '\'' +
        ", action='" + action + '\'' +
        '}';
  }
}

根据下图的左方可以看到这里面有构造器,Getter,Setter,equals,HashCode,toString等方法


使用lombok的POJO类
package com.newtank.scorpio.web.controller.request;

import com.newtank.scorpio.web.query.DataGridFilter;
import com.newtank.scorpio.web.query.QueryType;
import com.newtank.scorpio.web.query.Restrictions;
import lombok.Data;

/**
 * Created by looper on 2017/8/29.
 */
@Data
public class UserActionLogFilter extends DataGridFilter {
  @QueryType(value = Restrictions.EQ, column = "id")
  private String id;

  @QueryType(value = Restrictions.EQ, column = "mobile")
  private String  mobile;

  @QueryType(value = Restrictions.EQ, column = "openid")
  private String openid;

  @QueryType(value = Restrictions.EQ, column = "source")
  private String source;

  @QueryType(value = Restrictions.EQ, column = "model")
  private String model;

  @QueryType(value = Restrictions.EQ, column = "action")
  private String action;
}





根据下图可以看出该有的方法都有.其中@data 注解在类上,提供Getter,Setter,equals,HashCode,toString等方法



当然如果不需要这么多的方法,那就不需要使用@Data注解 下面介绍其它几个比较常用的注解.
@Getter 为属性提供Getter方法, 注解在属性上为该属性提供Getter方法,注解在类上为所有的属性提供Getter方法
@Setter 为属性提供Setter方法, 注解在属性上为该属性提供Setter方法,注解在类上为所有的属性提供Setter方法
@ Log4j 注解在类上;为类提供一个 属性名为log 的 log4j 日志对象
@ NoArgsConstructor 注解在类上;为类提供一个无参的构造方法
@ AllArgsConstructor 注解在类上;为类提供一个全参的构造方法


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶孤心丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值