0
点赞
收藏
分享

微信扫一扫

Lombok使用(二)

  • Data

@Data  // 相当于 @getter  @setter  @ToString  @EqualsAndHashCode,但需要所有属性的值相同才是同一个对象
@EqualsAndHashCode(of = {"id"})
public class UserInfoData {
private Long id;
private String name;
private String phone;
private Date birthDay;
private String address;
}

  • Accessors

@Accessors(
// chain = true, // 链式编程
fluent = true
)
public class UserInfoAccessors {
private Long id;
private String name;
private String phone;
private Date birthDay;
private String address;
}

  • 测试
  • Lombok使用(二)_Lombok

  • Builder

@Builder
public class User {
private Long id;
private String name;
private String phone;
private Date birthDay;
private String address;
}

  • 查看编译后的类

public class User {
private Long id;
private String name;
private String phone;
private Date birthDay;
private String address;

User(Long id, String name, String phone, Date birthDay, String address) {
this.id = id;
this.name = name;
this.phone = phone;
this.birthDay = birthDay;
this.address = address;
}

public static User.UserBuilder builder() {
return new User.UserBuilder();
}

public static class UserBuilder {
private Long id;
private String name;
private String phone;
private Date birthDay;
private String address;

UserBuilder() {
}

public User.UserBuilder id(Long id) {
this.id = id;
return this;
}

public User.UserBuilder name(String name) {
this.name = name;
return this;
}

public User.UserBuilder phone(String phone) {
this.phone = phone;
return this;
}

public User.UserBuilder birthDay(Date birthDay) {
this.birthDay = birthDay;
return this;
}

public User.UserBuilder address(String address) {
this.address = address;
return this;
}

public User build() {
return new User(this.id, this.name, this.phone, this.birthDay, this.address);
}

public String toString() {
return "User.UserBuilder(id=" + this.id + ", name=" + this.name + ", phone=" + this.phone + ", birthDay=" + this.birthDay + ", address=" + this.address + ")";
}
}
}

  • 测试

public class Test {
public static void main(String[] args) {
User.UserBuilder builder = User.builder();
User user = builder.id(1L).build();
System.out.print(user);
}
}

  • 日志

# 导入依赖
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>

# 写法1
public class UserInfoService {
private static final Logger LOGGER = LoggerFactory.getLogger(UserInfoService.class);
public List<UserInfo> getAll() {
LOGGER.info("进入getAll方法");
return new ArrayList<>();
}
}

# 写法2
@Slf4j // 专门针对项目里面使用的是slf4j
public class UserInfoService {
public List<UserInfo> getAll() {
log.info("进入getAll方法");
return new ArrayList<>();
}
}

  • 其他

@Log @Log4j @Log4j2

  • 原理

jsr

描述

相关实现

jsr107

缓存规范

spring基于此实现的缓存体系

jsr250

java平台Common Annotations 如@PostConstruct

spring

jsr269

Pluggable Annotation Processing API

lombok,mapstruct

jsr303,jsr349,jsr380

bean validation

hibernate-validitor



举报

相关推荐

0 条评论