springboot 3.x
首先在springboot项目的pom.xml中导入
<!-- lombok,可以简化实体类声明--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency>
加载完成后就可以在实体类中使用了
原代码
package com.example.pojo; public class User { private Integer id; private String name; private Short age; private Short gender; private String phone; public User() { } public User(Integer id, String name, Short age, Short gender, String phone) { this.id = id; this.name = name; this.age = age; this.gender = gender; this.phone = phone; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Short getAge() { return age; } public void setAge(Short age) { this.age = age; } public Short getGender() { return gender; } public void setGender(Short gender) { this.gender = gender; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", gender=" + gender + ", phone='" + phone + '\'' + '}'; } }
使用 lombok后
其中@data等价于以下四个注解
@Getter get方法
@Setter set方法
@ToString tostring方法
@EqualsAndHashCode equals和hashcode方法
package com.example.pojo; import lombok.*; //用于存放实体类 @Data @NoArgsConstructor //无参构造 @AllArgsConstructor //全参构造 public class User { private Integer id; private String name; private Short age; private Short gender; private String phone; }
可以看出使用 lombok 注解后代码简化了很多
标签:String,sprintboot,age,配置,id,phone,lombok,public,name From: https://www.cnblogs.com/yansans/p/18314440