SpringBoot 2.x 整合Lombok
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
返回结果
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赋值,输出结果
ApiFiles file = ApiFiles.builder()
.id(id)
.name("file"+String.valueOf(r))
.size(r)
.status(1)
.uploadTime(currentTime)
.version("11")
.build();
结果
总结:
Lombok的优点:
能通过注解的形式自动生成代码,提高了一定的开发效率;
让代码变得简洁,简化了维护工作
不足:
eclipse或IntelliJ IDEA需要安装相应的插件;
不支持多种参数构造器的重载;
降低了源代码的可读性和完整性
SpringBoot 2.x 整合Lombok的更多相关文章
- 基于SpringBoot从零构建博客网站 - 整合lombok和mybatis-plus提高开发效率
在上一章节中<技术选型和整合开发环境>,确定了开发的技术,但是如果直接这样用的话,可能开发效率会不高,为了提高开发的效率,这里再整合lombok和mybatis-plus两个组件. 1.l ...
- springboot + mybatis + mycat整合
1.mycat服务 搭建mycat服务并启动,windows安装参照. 系列文章: [Mycat 简介] [Mycat 配置文件server.xml] [Mycat 配置文件schema.xml] [ ...
- SpringBoot+Mybatis+MybatisPlus整合实现基本的CRUD操作
SpringBoot+Mybatis+MybatisPlus整合实现基本的CRUD操作 1> 数据准备 -- 创建测试表 CREATE TABLE `tb_user` ( `id` ) NOT ...
- 从零开始的SpringBoot项目 ( 六 ) 整合 MybatisPlus 实现代码自动生成
1.添加依赖 <!-- MySQL数据库 --> <dependency> <groupId>mysql</groupId> <artifactI ...
- SpringBoot与Mybatis整合方式01(源码分析)
前言:入职新公司,SpringBoot和Mybatis都被封装了一次,光用而不知道原理实在受不了,于是开始恶补源码,由于刚开始比较浅,存属娱乐,大神勿喷. 就如网上的流传的SpringBoot与Myb ...
- Springboot security cas整合方案-实践篇
承接前文Springboot security cas整合方案-原理篇,请在理解原理的情况下再查看实践篇 maven环境 <dependency> <groupId>org.s ...
- Springboot security cas整合方案-原理篇
前言:网络中关于Spring security整合cas的方案有很多例,对于Springboot security整合cas方案则比较少,且有些仿制下来运行也有些错误,所以博主在此篇详细的分析cas原 ...
- 使用Springboot + Gradle快速整合Mybatis-Plus
使用Springboot + Gradle快速整合Mybatis-Plus 作者:Stanley 罗昊 [转载请注明出处和署名,谢谢!] MyBatis-Plus(简称 MP)是一个 MyBatis ...
- springboot 与 shiro 整合 (简洁版)
前言: 网上有很多springboot 与 shiro 整合的资料,有些确实写得很好, 对学习shiro和springboot 都有很大的帮助. 有些朋友比较省事, 直接转发或者复制粘贴.但是没有经过 ...
随机推荐
- Windows 任务调度程序定时执行Python脚本
Windows 任务调度程序(Task Scheduler)可以定时执行程序,本文分享使用Task Scheduler定时执行Python脚本的两种方法. 在控制面版->管理员工具中打开 Tas ...
- GitHub中文社区
今天在打开GitHub的时候,使用了bing.com搜索,输入GitHub进行搜索链接,排名第一的为GitHub中文社区,点击去发现这个社区还可以,我们看看GitHub中文社区有哪些好的地方 GitH ...
- 配置 ASP.NET Core 请求(Request)处理管道
配置 ASP.NET Core 请求(Request)处理管道 在本节中,我们将讨论使用中间件组件为 asp.net core 应用程序配置请求处理管道. 作为应用程序启动的一部分,我们要在Confi ...
- wordpress 数据查询-全局注入-模板数据消费输出简图
我一直比较好奇,类似于wordpress这样的CMS,它可以做的很灵活,同样的软件,为什么就能做出几乎完全不具有相似性的不同站点来呢?除了功能可以有大不同以外,即便是相同的简单blog站他们的外观也可 ...
- pytest-fixture参数化
fixture参数化 指定params属性,实现fixture的参数化,引用该fixture的测试方法将遍历全部参数 import pytest @pytest.fixture(params=[&qu ...
- centos 7 防火墙相关操作
centos 7 防火墙相关操作 CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewal ...
- Ansible varialbes
1.什么是变量? 以一个固定的字符串,表示一个不固定的值 version: 1.12 2.定义变量? 1.在playbook中定义变量? vars 关键字 [root@manager projec ...
- Vue笔记1
index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...
- c++的explicit理解
默认规定 只有一个参数的构造函数也定义了一个隐式转换,将该构造函数对应数据类型的数据转换为该类对象 explicit class A { explicit A(int n); A(char *p); ...
- 基于Docker实现MySQL主从复制
前言 MySQL的主从复制是实现应用的高性能,高可用的基础.对于数据库读操作较密集的应用,通过使数据库请求负载均衡分配到不同MySQL服务器,可有效减轻数据库压力.当遇到MySQL单点故障中,也能在短 ...