最近發現了一個能夠節省代碼的小工具,用了一段時間感覺還不錯,特此推薦一下.
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 注解在類上;為類提供一個全參的構造方法