1 什么是 Lombok
官网介绍:
Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.
Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.
翻译过来就是:
Project Lombok是一个java库,可以自动插入到编辑器和构建工具中,使java更加丰富多彩。
不要再编写另一个getter或equals方法,只要有一个注释,类就有功能齐全的构建器,自动记录日志变量,等等。
就是我使用注释就可以代替那些 set和get方法 、构造方法等,消除程序的冗长,简化代码
没使用Lombok之前:
public class User implements Serializable {
private int age; //年龄
private String username; //用户姓名
private String email;//用户邮箱
public User(String username, int age) {
this.username=username;
this.age=age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
使用lombok之后:
import lombok.Data;
import java.io.Serializable;
@Data
public class User implements Serializable {
private int age;
private String username;
private String email;
}
2 如何准备Lombok
首先在pom.xml文件中引入lombok的依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
然后 配置idea( setting ->plugin->lombok->install->重启idea)
3 lombok的方法
1 @Data 相当于使用@Setter
、@Getter
、@RequiredArgsConstructor
、@ToString
、@EqualsAndHashCode 为这个实体类自动添加set方法 get方法 有参构造函数 toString方法 重写Hashcode函数
2 @Setter 给实体类自动创建Setter方法,如果标记在类上就是给这个类的所有属性创建Setter方法,如果标记在属性上就是给这个属性创建Setter方法
import java.io.Serializable;
public class User implements Serializable {
@Setter
private int age;
private String username;
private String email;
}
效果:
public class testController {
@RequestMapping("/hello")
public User newuser()
{
User user=new User();
user.setAge(1);
user.setUsername("222");
return user;
}
}
标记在类上:
@Setter
public class User implements Serializable {
private int age;
private String username;
private String email;
}
效果:
@Controller
public class testController {
@RequestMapping("/hello")
public User newuser()
{
User user=new User();
user.setAge(1);
user.setUsername("222");
user.setEmail("3333");
return user;
}
}
3 @Getter注解 同样的是标记在类上是给这个类自动创建Getter方法,标记在属性上就是给这个属性创建Getter方法
@Getter
public class User implements Serializable {
private int age;
private String username;
private String email;
}
@Controller
public class testController {
@RequestMapping("/hello")
public User newuser()
{
User user=new User();
user.getAge();
user.getEmail();
user.getUsername();
return user;
}
}
标记在 属性上
public class User implements Serializable {
@Getter
private int age;
private String username;
private String email;
}
4 @NonNull 用于标注非空的
public class User implements Serializable {
private int age;
private String username;
private String email;
public String fuzhi(@NonNull Pet pet)
{
this.username=pet.getName();
return this.username;
}
}
@Controller
public class testController {
@RequestMapping("/hello")
public User newuser()
{
User user=new User();
Pet pet=null;
user.fuzhi(pet);
return user;
}
}
传入一个空对象就会提示 is marked non-null but is null
如果不使用注解的话就需要if判断参数是否为空,为空就throw抛出异常
5 @Cleanup 自动关闭相关的流对象,调用其close方法
@Cleanup Reader fileReader =
new FileReader(".\\src\\main\\java\\com\\cauchy6317\\common\\Cleanup\\ForCleanupExample1.txt");
待续