Lombok的官方介绍
Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.
Lombok以简单的注解形式来简化java代码,提高开发人员的开发效率
lombok
是一个编译级别的插件,它可以在项目编译的时候生成一些代码
1.为IntelliJ IDEA安装插件
file——>settings——>Plugins
安装完后需要重启IntelliJ IDEA
2.添加依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.61</version> </dependency> </dependencies>
3.使用
常用注解
@Data 注解在类上;提供类所有属性的 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法 @Setter :注解在属性上;为属性提供 setting 方法 @Setter :注解在属性上;为属性提供 getting 方法 @Log4j :注解在类上;为类提供一个 属性名为log 的 log4j 日志对象 @NoArgsConstructor :注解在类上;为类提供一个无参的构造方法 @AllArgsConstructor :注解在类上;为类提供一个全参的构造方法 @Cleanup : 可以关闭流 @Builder : 被注解的类加个构造者模式 @Synchronized : 加个同步锁 @SneakyThrows : 等同于try/catch 捕获异常 @NonNull : 如果给参数加个这个注解 参数为null会抛出空指针异常 @Value : 注解和@Data类似,区别在于它会把所有成员变量默认定义为private final修饰,并且不会生成set方法
(1)@Data
@Data 自动生成set/get方法,toString方法,equals方法,hashCode方法,不带参数的构造方法
实体类
package com.abc.plus.entity; import lombok.Data; import java.util.Date; @Data public class ApiFiles { private Long id; private String name; private Integer size; private Integer status; private Date uploadTime; private String version; }
测试
package com.abc.plus.controller; import com.abc.plus.core.Result; import com.abc.plus.core.SnowFlake; import com.abc.plus.entity.ApiFiles; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; @RestController public class demoController { private SnowFlake sn; @RequestMapping("/test") public Result test(){ sn = new SnowFlake(1,1); ApiFiles file = new ApiFiles(); file.setId(sn.nextId()); int r= (int) (Math.random() * 50 + 500); file.setName("file"+String.valueOf(r)); file.setSize(r); file.setStatus(1); Date currentTime = new Date(); file.setUploadTime(currentTime); return Result.success(200,file); } }
SnowFlake
package com.abc.plus.core; import com.alibaba.fastjson.JSON; /** * Created by Beibei on 19/02/22 * API响应结果 */ public class Result<T> { private int code; private String message; private T data; public Result setCode(Integer code) { this.code = code; return this; } public int getCode() { return code; } public String getMessage() { return message; } public Result setMessage(String message) { this.message = message; return this; } public T getData() { return data; } public Result setData(T data) { this.data = data; return this; } @Override public String toString() { return JSON.toJSONString(this); } public static <T> Result<T> fail(Integer code,T data) { Result<T> ret = new Result<T>(); ret.setCode(code); ret.setData(data); return ret; } public static <T> Result<T> failMessage(Integer code,String msg) { Result<T> ret = new Result<T>(); ret.setCode(code); ret.setMessage(msg); return ret; } public static <T> Result<T> successMessage(Integer code,String msg) { Result<T> ret = new Result<T>(); ret.setCode(code); ret.setMessage(msg); return ret; } public static <T> Result<T> success(Integer code,T data) { Result<T> ret = new Result<T>(); ret.setCode(code); ret.setData(data); return ret; } public static <T> Result<T> success(Integer code,T data,String msg) { Result<T> ret = new Result<T>(); ret.setCode(code); ret.setData(data); ret.setMessage(msg); return ret; } }
Result
package com.abc.plus.core; public class SnowFlake { /** * 起始的时间戳 */ private final static long START_STMP = 1480166465631L; /** * 每一部分占用的位数 */ private final static long SEQUENCE_BIT = 12; //序列号占用的位数 private final static long MACHINE_BIT = 5; //机器标识占用的位数 private final static long DATACENTER_BIT = 5;//数据中心占用的位数 /** * 每一部分的最大值 */ private final static long MAX_DATACENTER_NUM = -1L ^ (-1L << DATACENTER_BIT); private final static long MAX_MACHINE_NUM = -1L ^ (-1L << MACHINE_BIT); private final static long MAX_SEQUENCE = -1L ^ (-1L << SEQUENCE_BIT); /** * 每一部分向左的位移 */ private final static long MACHINE_LEFT = SEQUENCE_BIT; private final static long DATACENTER_LEFT = SEQUENCE_BIT + MACHINE_BIT; private final static long TIMESTMP_LEFT = DATACENTER_LEFT + DATACENTER_BIT; private long datacenterId; //数据中心 private long machineId; //机器标识 private long sequence = 0L; //序列号 private long lastStmp = -1L;//上一次时间戳 public SnowFlake(long datacenterId, long machineId) { if (datacenterId > MAX_DATACENTER_NUM || datacenterId < 0) { throw new IllegalArgumentException("datacenterId can't be greater than MAX_DATACENTER_NUM or less than 0"); } if (machineId > MAX_MACHINE_NUM || machineId < 0) { throw new IllegalArgumentException("machineId can't be greater than MAX_MACHINE_NUM or less than 0"); } this.datacenterId = datacenterId; this.machineId = machineId; } /** * 产生下一个ID * * @return */ public long nextId() { long currStmp = getNewstmp(); if (currStmp < lastStmp) { throw new RuntimeException("Clock moved backwards. Refusing to generate id"); } if (currStmp == lastStmp) { //相同毫秒内,序列号自增 sequence = (sequence + 1) & MAX_SEQUENCE; //同一毫秒的序列数已经达到最大 if (sequence == 0L) { currStmp = getNextMill(); } } else { //不同毫秒内,序列号置为0 sequence = 0L; } lastStmp = currStmp; return (currStmp - START_STMP) << TIMESTMP_LEFT //时间戳部分 | datacenterId << DATACENTER_LEFT //数据中心部分 | machineId << MACHINE_LEFT //机器标识部分 | sequence; //序列号部分 } private long getNextMill() { long mill = getNewstmp(); while (mill <= lastStmp) { mill = getNewstmp(); } return mill; } private long getNewstmp() { return System.currentTimeMillis(); } }
启动项目
http://localhost:8080/test
返回结果
{
"code": 200,
"message": null,
"data": {
"id": 400317896322060288,
"name": "file507",
"size": 507,
"status": 1,
"uploadTime": "2019-12-06T05:21:29.661+0000",
"version": null
}
}
(2)@Builder
实体类
package com.abc.plus.entity; import lombok.Builder; import lombok.Data; import java.util.Date; @Builder @Data public class ApiFiles { private Long id; private String name; private Integer size; private Integer status; private Date uploadTime; private String version; }
测试
package com.abc.plus.controller; import com.abc.plus.core.Result; import com.abc.plus.core.SnowFlake; import com.abc.plus.entity.ApiFiles; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; @RestController public class demoController { private SnowFlake sn; @RequestMapping("/test") public Result test(){ sn = new SnowFlake(1,1); Long id= sn.nextId(); int r= (int) (Math.random() * 50 + 500); Date currentTime = new Date(); ApiFiles file = ApiFiles.builder() .id(id) .name("file"+String.valueOf(r)) .size(r) .status(1) .uploadTime(currentTime) .build(); return Result.success(200,file); } }
(3)@NonNull
实体类
package com.abc.plus.entity; import lombok.Builder; import lombok.Data; import lombok.NonNull; import java.util.Date; @Builder @Data public class ApiFiles { private Long id; private String name; private Integer size; private Integer status; private Date uploadTime; @NonNull private String version; }
用上边的测试方法测试,没给version赋值,输出结果
{
"timestamp": "2019-12-06T05:40:09.861+0000",
"status": 500,
"error": "Internal Server Error",
"message": "version is marked non-null but is null",
"path": "/test"
}
修改为
ApiFiles file = ApiFiles.builder() .id(id) .name("file"+String.valueOf(r)) .size(r) .status(1) .uploadTime(currentTime) .version("11") .build();
结果
{
"code": 200,
"message": null,
"data": {
"id": 400323058746920960,
"name": "file547",
"size": 547,
"status": 1,
"uploadTime": "2019-12-06T05:42:00.479+0000",
"version": "11"
}
}
还有很多,先写这些了
总结:
Lombok的优点:
能通过注解的形式自动生成代码,提高了一定的开发效率;
让代码变得简洁,简化了维护工作
不足:
eclipse或IntelliJ IDEA需要安装相应的插件;
不支持多种参数构造器的重载;
降低了源代码的可读性和完整性