lombok使用
- 添加lombok依赖
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
-
idea安装lombok插件
社区版本自带
-
编写实体类
package com.hua;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class Persion {
private String gender;
private String name;
private int age;
}
使用
package com.hua;
public class LombokTest {
public static void main(String[] args) {
Persion persion = new Persion();
persion.setAge(23);
persion.setName("sss");
persion.setGender("男");
System.out.println(persion);
}
}
package com.hua;
public class LombokTest {
public static void main(String[] args) {
Persion persion = new Persion.PersionBuilder().age(23)
.name("ssss")
.gender("男").build();
System.out.println(persion);
}
}
就可以看到使用注解的效果了
- 常用注解
@Data
@ToString
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder