1.添加依赖和插件plu
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.4</version>
</dependency>
![](https://i-blog.csdnimg.cn/blog_migrate/21eaba6596dc0fb3abf6f5e4675fdc15.png)
2.不使用lombok代码展示
private Long id;
private String name;
private Integer age;
private String email;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", email='" + email + '\'' +
'}';
}
@RestController
@SpringBootApplication
public class LombokApplication {
@RequestMapping("/user")
public User index(){
User user = new User();
user.setId(1L);
user.setName("Lucky");
user.setAge(18);
user.setEmail("123@qq.com");
return user;
}
public static void main(String[] args) {
SpringApplication.run(LombokApplication.class, args);
}
}
3.使用lombok代码展示
/**
* @Data 自动为所有字段添加@ToString, @EqualsAndHashCode, @Getter方法
* 为非final字段添加@Setter,和@RequiredArgsConstructor!
*/
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
4.lombok注解
@Data (类注解)
@Setter (field注解)
@Getter (field注解)
@ToString (类注解)
@NoArgsConstructor (类注解)
@AllArgsConstructor(类注解)
5.如下的注解作用和@Data相同
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class User {
@Getter
@Setter
private Long id;
@Getter
@Setter
private String name;
@Getter
@Setter
private Integer age;
@Getter
@Setter
private String email;
}