1.在idea上安装lombok插件
![idea 安装lombok插件](https://i-blog.csdnimg.cn/blog_migrate/f0ae0d52a84effa3c97d77c03b87b5a1.png)
2.在pom中引入依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
3.创建测试类
import lombok.*;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
/**
* @Data Generates getters for all fields, a useful toString method, and hashCode and equals implementations that check
* all non-transient fields. Will also generate setters for all non-final fields, as well as a constructor.
* <p>
* @Data Equivalent to {@code @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode}.
* <p>
*
* {@Getter,@Setter) getter and setter methods
* {@Slf4j,@Log4j} 如果需要用到日志,直接加上注解,使用log就可以了,需要注意的是你项目使用的是什么日志框架,根据日志框架选择一个注解。
*/
@Data
@Getter(AccessLevel.PUBLIC)//AccessLevel.PUBLIC指定方法的权限(PUBLIC, MODULE, PROTECTED, PACKAGE, PRIVATE)
@Setter(AccessLevel.PUBLIC)//AccessLevel.PUBLIC指定方法的权限(PUBLIC, MODULE, PROTECTED, PACKAGE, PRIVATE)
@NoArgsConstructor//参数构造器
@AllArgsConstructor//全参构造器
@Slf4j
@Log4j
//@ToString(exclude = {"userName","password"})//重写toString,exclude表示排除掉哪些字段
@ToString(of = {"userName","password"})//重写toString,of表示包括哪些字段
@EqualsAndHashCode(exclude = {"userName","password"})//重写equals和hashcode方法
public class LombokTest {
private String userName;
private String password;
public void log() {
log.info("hello");
}
}