注解
- @Builder
// 示例代码
package test;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class BuilderTest {
private Integer id ;
private String name ;
public static void main(String[] args) {
BuilderTest test = BuilderTest.builder().id(1).name("1").build();
System.out.println(test);
BuilderTestBuilder builder1 = BuilderTest.builder();
BuilderTestBuilder builder2 = BuilderTest.builder();
System.out.println(builder1 == builder2);
BuilderTest test1 = BuilderTest.builder().build();
BuilderTest test2 = BuilderTest.builder().build();
System.out.println(test1 == test2);
}
}
// 编译后的代码
package test;
import java.beans.ConstructorProperties;
import java.io.PrintStream;
public class BuilderTest
{
private Integer id;
private String name;
public void setId(Integer id)
{
this.id = id;
}
public void setName(String name)
{
this.name = name;
}
public boolean equals(Object o)
{
if (o == this) {
return true;
}
if (!(o instanceof BuilderTest)) {
return false;
}
BuilderTest other = (BuilderTest)o;
if (!other.canEqual(this)) {
return false;
}
Object this$id = getId();Object other$id = other.getId();
if (this$id == null ? other$id != null : !this$id.equals(other$id)) {
return false;
}
Object this$name = getName();Object other$name = other.getName();return this$name == null ? other$name == null : this$name.equals(other$name);
}
protected boolean canEqual(Object other)
{
return other instanceof BuilderTest;
}
public int hashCode()
{
int PRIME = 59;int result = 1;Object $id = getId();result = result * 59 + ($id == null ? 43 : $id.hashCode());Object $name = getName();result = result * 59 + ($name == null ? 43 : $name.hashCode());return result;
}
public String toString()
{
return "BuilderTest(id=" + getId() + ", name=" + getName() + ")";
}
public static class BuilderTestBuilder
{
private Integer id;
private String name;
public String toString()
{
return "BuilderTest.BuilderTestBuilder(id=" + this.id + ", name=" + this.name + ")";
}
public BuilderTest build()
{
return new BuilderTest(this.id, this.name);
}
public BuilderTestBuilder name(String name)
{
this.name = name;return this;
}
public BuilderTestBuilder id(Integer id)
{
this.id = id;return this;
}
}
public static BuilderTestBuilder builder()
{
return new BuilderTestBuilder();
}
@ConstructorProperties({"id", "name"})
public BuilderTest(Integer id, String name)
{
this.id = id;this.name = name;
}
public Integer getId()
{
return this.id;
}
public String getName()
{
return this.name;
}
public static void main(String[] args)
{
BuilderTest test = builder().id(Integer.valueOf(1)).name("1").build();
System.out.println(test);
BuilderTestBuilder builder1 = builder();
BuilderTestBuilder builder2 = builder();
System.out.println(builder1 == builder2);
BuilderTest test1 = builder().build();
BuilderTest test2 = builder().build();
System.out.println(test1 == test2);
}
public BuilderTest() {}
}
-
说明:使用@Builder注解可以省去对象声明赋值的流程,但是同时创建的对象变为了两个,首先需要调用静态内部类创建 builder对象,然后再生成PO对象;道理很简单,此消彼长,省事了一定会多消耗空间
-
常用注解
注解 | 作用 |
---|---|
@Data | 属性set get toString 方法 |
@Builder | 构造一个实例,属性不需要单独set |
@AllArgsConstructor | 全参数构造方法 |
@NoArgsConstructor | 无参数构造方案 |