这篇文章主要讲解了“lombok的介绍和使用方式”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“lombok的介绍和使用方式”吧!
lombok是java开发的神器,使用注解让实体类pojo还有日志slf4j操作特别方便。
(1)idea中使用lombok工具,需要安装lombok插件。大家plugins搜索lombok安装即可,不然,使用lombok会报错。 (2)在Java项目的pom文件中添加依赖,使用注解就可以了。
(1) @Getter/@Setter注解可以针对类的属性字段自动生成Get/Set方法。
public class Pojo{
@Setter
[@Getter](https://my.oschina.net/u/3288663)
private String name;
//其他代码……
}
(2) @ToString注解,为使用该注解的类生成一个toString方法
@ToString
public class Pojo {
private String name;
}
(3)@EqualsAndHashCode注解,为使用该注解的类自动生成equals和hashCode方法
@EqualsAndHashCode
public class Pojo {
private String name;
}
(4) @NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor,这几个注解分别为类自动生成了无参构造器、指定参数的构造器和包含所有参数的构造器。
@NoArgsConstructor
@AllArgsConstructor
public class Pojo {
private String name;
}
(5)@Data注解作用比较全,其包含注解的集合@ToString,@EqualsAndHashCode,所有字段的@Getter和所有非final字段的@Setter, @RequiredArgsConstructor。其示例代码可以参考上面几个注解的组合。
* @see Getter
* @see Setter
* @see RequiredArgsConstructor
* @see ToString
* @see EqualsAndHashCode
* @see lombok.Value
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface Data {
/**
* If you specify a static constructor name, then the generated constructor will be private, and
* instead a static factory method is created that other classes can use to create instances.
* We suggest the name: "of", like so:
*
* <pre>
* public @Data(staticConstructor = "of") class Point { final int x, y; }
* </pre>
*
* Default: No static constructor, instead the normal constructor is public.
*
* @return Name of static 'constructor' method to generate (blank = generate a normal constructor).
*/
String staticConstructor() default "";
}
(6)@Builder注解使用建造者模式,为制定参数赋值
@Builder
public class Pojo {
private String name;
}
使用起来非常的方便,满足日常的工作需要。
感谢各位的阅读,以上就是“lombok的介绍和使用方式”的内容了,经过本文的学习后,相信大家对lombok的介绍和使用方式这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/wotrd/blog/3079737