Lombok安装与使用

来源:互联网 发布:宋茜演技 知乎 编辑:程序博客网 时间:2024/06/06 16:36
 最近发现了一个能够节省代码的小工具,用了一段时间感觉还不错,特此推荐一下.

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. */@Datapublic 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 注解在类上;为类提供一个全参的构造方法


原创粉丝点击