简介
Lombok项目是一个Java库,Lombok提供了一组有用的注释,用来消除Java类中的大量样板代码,简洁且易于维护的Java类。
使用工具
IDEA2018.2 MySQL5.6 JDK1.8
使用jar包
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>
环境搭建
请参照
https://blog.csdn.net/qq_44989881/article/details/102702027
进行项目配置
代码示例
优化Student.java代码
Student.java
package com.vvcat.gitee.bean;
import lombok.*;
import javax.persistence.*;
/**
* @ToString(callSuper=true,exclude="someExcludedField")
* 类使用@ToString注解,Lombok会生成一个toString()方法,默认情况下,会输出类名、所有属性(会按照属性定义顺序),用逗号来分割。
* 通过将includeFieldNames参数设为true,就能明确的输出toString()属性。
*
* @RequiredArgsConstructor(staticName = "Student")
* 注解在类上,会生成构造方法(可能带参数也可能不带参数)。
* 注意:如果带参数,这参数只能是以 final 修饰的未经初始化的字段或者是以 @NonNull 注解的未经初始化的字段。
* 该注解还可以用 @RequiredArgsConstructor(staticName="methodName") 的形式生成一个指定名称的静态方法,返回一个调用相应的构造方法产生的对象
*
* @NonNull
* 该注解用在属性或构造器上,Lombok会生成一个非空的声明,可用于校验参数,能帮助避免空指针。
*/
@Entity
@Table(name = "student")//数据库表明
@ToString(callSuper=true,exclude="age",includeFieldNames = true) //使用lombok 重写toSting方法 默认的toString格式
@NoArgsConstructor //无参构造器
@RequiredArgsConstructor(staticName = "Student") //部分参数构造器
@AllArgsConstructor(access = AccessLevel.PROTECTED) //全参构造器
public class Student {
@Id//声明id为主键
@Setter //为代码添加Setter方法
@Getter //为代码添加Getter方法
@GeneratedValue(strategy = GenerationType.IDENTITY)//声明自动增长
@Column(name = "id")//声明数据库对应的字段
private Integer id;
@NonNull //使用@NonNull注解,指定 stuName 为构造方法中的参数
@Setter //为代码添加Setter方法
@Getter //为代码添加Getter方法
@Column(name = "stuName")//声明数据库对应的字段
//定义字段也是有讲究的,比如首字母小写,后边的驼峰,对应的数据库字段,遇到驼峰用下划线断开
//比如实体类定义的userName,则数据库字段为user_name,
//比如实体类定义的username,则数据库字段也为username
private String stu_name;
@NonNull //使用@NonNull注解,指定 age 为构造方法中的参数
@Setter //为代码添加Setter方法
@Getter //为代码添加Getter方法
@Column(name = "age")//声明数据库对应的字段
private Integer age;
@NonNull //使用@NonNull注解,指定 sex 为构造方法中的参数
@Setter //为代码添加Setter方法
@Getter //为代码添加Getter方法
@Column(name = "sex")//声明数据库对应的字段
private String sex;
}
使用说明
1. @ToString注解
- @ToString(callSuper=true,exclude=“age”)
- 类使用@ToString注解,Lombok会生成一个toString()方法,默认情况下,会输出类名、所有属性(会按照属性定义顺序),用逗号来分割。
-
- 通过将includeFieldNames参数设为true,就能明确的输出toString()属性。
@ToString注解的使用可以代替快捷键Alt + Insert中ToString方法的生成
快捷键Alt + Insert生成的ToString方法:
@Override
public String toString() {
return "Student{" +
"id=" + id +
", stu_name='" + stu_name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
:
使用@ToString注解生成的ToString方法:
public String toString() {
return "Student(super=" + super.toString() + ", id=" + this.getId() + ", stu_name=" + this.getStu_name() + ", age=" + this.getAge() + ", sex=" + this.getSex() + ")";
}
2. @Setter和@Getter注解
@Setter和@Getter注解 的使用可以代替快捷键Alt + Insert中Setter和Getter方法的生成
快捷键Alt + Insert生成的Setter和Gette方法:
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
使用@Setter和@Getter注解生成的Setter和Getter方法:
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
public String getStu_name() {
return this.stu_name;
}
在 private String stu_name; 添加@Setter和@Getter注解后再添加@NonNull注解
@NonNull //使用@NonNull注解,指定 stuName 为构造方法中的参数
@Setter //为代码添加Setter方法
@Getter //为代码添加Getter方法
@Column(name = "stuName")//声明数据库对应的字段
private String stu_name;
生成的Setter和Getter方法:
public void setStu_name(@NonNull String stu_name) {
if (stu_name == null) {
throw new NullPointerException("stu_name");
} else {
this.stu_name = stu_name;
}
}
@NonNull
public String getStu_name() {
return this.stu_name;
}
@NonNull 该注解用在属性或构造器上,Lombok会生成一个非空的声明,可用于校验参数,能帮助避免空指针。
3. @NoArgsConstructor注解
@NoArgsConstructor注解 的使用可以代替快捷键Alt + Insert中Constructor方法的生成
快捷键Alt + Insert生成的Constructor方法:
public Student() {
}
使用@NoArgsConstructor注解生成的Constructor方法:
public Student() {
}
4. @RequiredArgsConstructor注解
- @RequiredArgsConstructor(staticName = “Student”)
- 注解在类上,会生成构造方法(可能带参数也可能不带参数)。
- 注意:如果带参数,这参数只能是以 final 修饰的未经初始化的字段或者是以 @NonNull 注解的未经初始化的字段。
使用@RequiredArgsConstructor 需要在封装的属性上添加@NonNull注解
例如(下面例子中没有为id 添加 @NonNull )
@Id//声明id为主键
@Setter //为代码添加Setter方法
@Getter //为代码添加Getter方法
@GeneratedValue(strategy = GenerationType.IDENTITY)//声明自动增长
@Column(name = "id")//声明数据库对应的字段
private Integer id;
@NonNull //使用@NonNull注解,指定 stuName 为构造方法中的参数
@Setter //为代码添加Setter方法
@Getter //为代码添加Getter方法
@Column(name = "stuName")//声明数据库对应的字段
//定义字段也是有讲究的,比如首字母小写,后边的驼峰,对应的数据库字段,遇到驼峰用下划线断开
//比如实体类定义的userName,则数据库字段为user_name,
//比如实体类定义的username,则数据库字段也为username
private String stu_name;
@NonNull //使用@NonNull注解,指定 age 为构造方法中的参数
@Setter //为代码添加Setter方法
@Getter //为代码添加Getter方法
@Column(name = "age")//声明数据库对应的字段
private Integer age;
@NonNull //使用@NonNull注解,指定 sex 为构造方法中的参数
@Setter //为代码添加Setter方法
@Getter //为代码添加Getter方法
@Column(name = "sex")//声明数据库对应的字段
private String sex;
@RequiredArgsConstructor注解 的使用可以代替快捷键Alt + Insert中Constructor方法的生成
快捷键Alt + Insert生成的Constructor方法:
public Student(String stu_name, Integer age, String sex) {
this.stu_name = stu_name;
this.age = age;
this.sex = sex;
}
使用@RequiredArgsConstructor注解生成的Constructor方法:
private Student(@NonNull String stu_name, @NonNull Integer age, @NonNull String sex) {
if (stu_name == null) {
throw new NullPointerException("stu_name");
} else if (age == null) {
throw new NullPointerException("age");
} else if (sex == null) {
throw new NullPointerException("sex");
} else {
this.stu_name = stu_name;
this.age = age;
this.sex = sex;
}
}
- 该注解还可以用 @RequiredArgsConstructor(staticName=“methodName”) 的形式生成一个指定名称的静态方法,返回一个调用相应的构造方法产生的对象
例如:
@RequiredArgsConstructor(staticName = "Test") //部分参数构造器
使用@RequiredArgsConstructor注解生成的Constructor方法:
public static Student Test(@NonNull String stu_name, @NonNull Integer age, @NonNull String sex) {
return new Student(stu_name, age, sex);
}
5. @AllArgsConstructor注解
@AllArgsConstructor注解 的使用可以代替快捷键Alt + Insert中Constructor方法的生成
快捷键Alt + Insert生成的Constructor方法:
public Student(Integer id,String stu_name, Integer age, String sex) {
this.id = id;
this.stu_name = stu_name;
this.age = age;
this.sex = sex;
}
使用@AllArgsConstructor注解生成的Constructor方法:
@ConstructorProperties({"id", "stu_name", "age", "sex"})
protected Student(Integer id, String stu_name, Integer age, String sex) {
this.id = id;
this.stu_name = stu_name;
this.age = age;
this.sex = sex;
}
在属性上添加上@NonNull注解后,使用@AllArgsConstructor注解生成的Constructor方法:
@ConstructorProperties({"id", "stu_name", "age", "sex"})
protected Student(Integer id, @NonNull String stu_name, @NonNull Integer age, @NonNull String sex) {
if (stu_name == null) {
throw new NullPointerException("stu_name");
} else if (age == null) {
throw new NullPointerException("age");
} else if (sex == null) {
throw new NullPointerException("sex");
} else {
this.id = id;
this.stu_name = stu_name;
this.age = age;
this.sex = sex;
}
}
6.@NonNull注解
该注解用在属性或构造器上,Lombok会生成一个非空的声明,可用于校验参数,能帮助避免空指针。
7.@Data注解
@Data注解在 类 上;提供类所有属性的 get 和 set 方法,此外还提供了equals、canEqual、hashCode、toString 方法。
在使用注解时的代码:
import lombok.*;
import javax.persistence.*;
@Entity
@Table(name = "student")//数据库表明
@Data //使用Data注解
public class Student {
@Id//声明id为主键
@GeneratedValue(strategy = GenerationType.IDENTITY)//声明自动增长
@Column(name = "id")//声明数据库对应的字段
private Integer id;
@Column(name = "stuName")//声明数据库对应的字段
private String stu_name;
@Column(name = "age")//声明数据库对应的字段
private Integer age;
@Column(name = "sex")//声明数据库对应的字段
private String sex;
}
使用@Data注解编译后自动生成的代码:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(
name = "student"
)
public class Student {
@Id
@GeneratedValue(
strategy = GenerationType.IDENTITY
)
@Column(
name = "id"
)
private Integer id;
@Column(
name = "stuName"
)
private String stu_name;
@Column(
name = "age"
)
private Integer age;
@Column(
name = "sex"
)
private String sex;
public Student() {
}
public Integer getId() {
return this.id;
}
public String getStu_name() {
return this.stu_name;
}
public Integer getAge() {
return this.age;
}
public String getSex() {
return this.sex;
}
public void setId(Integer id) {
this.id = id;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setSex(String sex) {
this.sex = sex;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Student)) {
return false;
} else {
Student other = (Student)o;
if (!other.canEqual(this)) {
return false;
} else {
label59: {
Object this$id = this.getId();
Object other$id = other.getId();
if (this$id == null) {
if (other$id == null) {
break label59;
}
} else if (this$id.equals(other$id)) {
break label59;
}
return false;
}
Object this$stu_name = this.getStu_name();
Object other$stu_name = other.getStu_name();
if (this$stu_name == null) {
if (other$stu_name != null) {
return false;
}
} else if (!this$stu_name.equals(other$stu_name)) {
return false;
}
Object this$age = this.getAge();
Object other$age = other.getAge();
if (this$age == null) {
if (other$age != null) {
return false;
}
} else if (!this$age.equals(other$age)) {
return false;
}
Object this$sex = this.getSex();
Object other$sex = other.getSex();
if (this$sex == null) {
if (other$sex != null) {
return false;
}
} else if (!this$sex.equals(other$sex)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof Student;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $id = this.getId();
int result = result * 59 + ($id == null ? 43 : $id.hashCode());
Object $stu_name = this.getStu_name();
result = result * 59 + ($stu_name == null ? 43 : $stu_name.hashCode());
Object $age = this.getAge();
result = result * 59 + ($age == null ? 43 : $age.hashCode());
Object $sex = this.getSex();
result = result * 59 + ($sex == null ? 43 : $sex.hashCode());
return result;
}
public String toString() {
return "Student(id=" + this.getId() + ", stu_name=" + this.getStu_name() + ", age=" + this.getAge() + ", sex=" + this.getSex() + ")";
}
}
其它注解
@Log4j
@EqualsAndHashCode
@Cleanup
@Value
@SneakyThrows
@Synchronized
目前刚开始接触,还未使用到,日后用到之后,再进行补充说明