三、注意:如果注解@Slf4j注入后找不到变量log,那就给IDE安装lombok插件,、 下面以idea为例
1、File → settings → Plugins, 然后点击“Browse repositories” 如图
2、输入 lombok 搜索插件, 点install安装,安装完重启idea 这时候输入 log就可以有提示了
package com.yanjun.mybatis.bean;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
@Data
@NoArgsConstructor
public class User {
public Integer age;
@NonNull
public String name;
}
相当于
package com.yanjun.mybatis.bean;
import lombok.NonNull;
public class User {
public Integer age;
@NonNull
public String name;
public Integer getAge() {
return this.age;
}
@NonNull
public String getName() {
return this.name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setName(@NonNull String name) {
if (name == null) {
throw new NullPointerException("name is marked @NonNull but is null");
} else {
this.name = name;
}
}
}
测试
User user = new User();
user.setName(null);
效果
Exception in thread "main" java.lang.NullPointerException: name is marked @NonNull but is null
at com.yanjun.mybatis.bean.User.setName(User.java:7)
at com.yanjun.mybatis.LombokApplication.main(LombokApplication.java:15)
package com.yanjun.mybatis.bean;
import lombok.Data;
@Data //自动生成 get、set 方法
public class User {
Integer id;
String name;
Integer age;
public static void main(String[] args) {
//测试方法
User user = new User();
user.setName("zhaoyanjun");
user.setAge(20);
user.setId(1);
}
}
package com.yanjun.mybatis.bean;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@Data //自动生成 get、set 方法
@Slf4j //日志打印
public class User {
Integer id;
String name;
Integer age;
public static void main(String[] args) {
User user = new User();
user.setName("zhaoyanjun");
user.setAge(20);
user.setId(1);
log.info("日志" + user.toString());
}
}
package com.yanjun.mybatis.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@Data //自动生成 get、set 方法
@AllArgsConstructor //自动生成全参数构造函数
@Slf4j //日志打印
public class User {
Integer id;
String name;
Integer age;
public static void main(String[] args) {
User user = new User(1, "zhaoyanjun", 20);
log.info("日志" + user.toString());
}
}
package com.yanjun.mybatis.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
@Data //自动生成 get、set 方法
@AllArgsConstructor //自动生成全参数构造函数
@Slf4j //日志打印
@ToString(of = {"id","age"}) //tostring() 方法里面只打印 id ,name 字段
public class User {
Integer id;
String name;
Integer age;
public static void main(String[] args) {
User user = new User(1, "zhaoyanjun", 20);
log.info("日志" + user.toString());
}
}
toString() 方法排除字段
//排除字段
@ToString(exclude = {"name"}) //tostring() 方法中,name 字段不参与打印
@Value
注解和@Data
类似,区别在于它会把所有成员变量默认定义为 private final
修饰,并且不会生成set
方法。
package com.yanjun.mybatis.bean;
import lombok.Value;
@Value
public class User {
Integer id = 1;
String name = "zhaoyanjun";
Integer age = 3 ;
}
编译后的代码
public final class User {
private final Integer id = 1;
private final String name = "zhaoyanjun";
private final Integer age = 3;
public User() {
}
public Integer getId() {
return this.id;
}
public String getName() {
this.getClass();
return "zhaoyanjun";
}
public Integer getAge() {
return this.age;
}
//省略部分代码
......
}
import lombok.Builder;
@Builder
public class User {
public Integer id;
public String name;
public Integer age;
}
相当于以下代码
package com.yanjun.mybatis.bean;
public class User {
public Integer id;
public String name;
public Integer age;
User(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public static User.UserBuilder builder() {
return new User.UserBuilder();
}
public static class UserBuilder {
private Integer id;
private String name;
private Integer age;
UserBuilder() {
}
public User.UserBuilder id(Integer id) {
this.id = id;
return this;
}
public User.UserBuilder name(String name) {
this.name = name;
return this;
}
public User.UserBuilder age(Integer age) {
this.age = age;
return this;
}
public User build() {
return new User(this.id, this.name, this.age);
}
public String toString() {
return "User.UserBuilder(id=" + this.id + ", name=" + this.name + ", age=" + this.age + ")";
}
}
}
使用
UserBuilder userBuilder = User.builder();
User user = userBuilder
.age(10)
.id(1)
.name("yanjun")
.build();
System.out.println(": " + userBuilder.toString());
package com.yanjun.mybatis.bean;
import lombok.Synchronized;
public class User {
public Integer age;
public String name;
//普通方法,相当于对象锁
@Synchronized
int run1() {
return 1;
}
//静态方法,相当于类锁
@Synchronized
static int run2() {
return 2;
}
}
代码的效果相当于
public class User {
private final Object $lock = new Object[0];
private static final Object $LOCK = new Object[0];
public Integer age;
public String name;
public User() {
}
int run1() {
Object var1 = this.$lock;
synchronized(this.$lock) {
return 1;
}
}
static int run2() {
Object var0 = $LOCK;
synchronized($LOCK) {
return 2;
}
}
}