zl程序教程

您现在的位置是:首页 >  后端

当前栏目

【springboot】5、lombok

SpringBoot Lombok
2023-09-14 09:14:17 时间

基本介绍

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.
Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.

上面是官方对lombok的解释,大概意思就是Lombok是一个java库,可以自动生成代码,一个注解就能完成getter,setter等操作。

使用方法

插件安装

我们要在idea中使用lombok需要安装lombok插件

在这里插入图片描述

这个插件我记得是默认安装的,如果没有那么自己搜索安装一下就行了

启用lombok功能

我们需要在setting->Build,Execution,Deployment->Compiler->Annotation Processor中启用处理注解功能

在这里插入图片描述

引入相关jar包

我是使用的springboot项目,直接引入即可,使用springboot指定的版本

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

在这里插入图片描述

常见注解及其功能

https://projectlombok.org/features/这个是官方文档,就是对注解的解释
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

个人使用的最频繁的注解就是@Data,@ToString,@NoArgsConstructor, @AllArgsConstructor

实例演示

在这里插入图片描述
我们在Dog类上加上了@Getter,@Setter,@ToString这3个注解,编译一下,我们查看源代码,源代码如下
在这里插入图片描述
可以发现已经生成了getter,setter,toString等方法。其它的注解使用方法也是类似的,参考官方文档和源码注释即可