Intellij IDEA中lombok插件的使用

àì夳堔傛蜴生んèń 2022-01-27 15:13 506阅读 0赞
  • idea中开启对lombok插件的支持

下载lombok插件,在settings中
在这里插入图片描述
搜索lombok,然后再install,install好了之后再restart
在这里插入图片描述

  • lombok的使用

在pom.xml文件中添加lombok的依赖

  1. <dependency>
  2. <groupId>org.projectlombok</groupId>
  3. <artifactId>lombok</artifactId>
  4. <optional>true</optional>
  5. </dependency>

在实体类中常用注解的使用,只需要在实体类中添加下列响应注解,lombok插件就能自动生成相应的方法

@Setter:注解在类上,该类所有的字段都生成set方法,注解在字段上,只为该字段生成set方法
@Getter:注解在类上,该类所有的字段都生成get方法,注解在字段上,只为该字段生成get方法
@Data:注解在类上;提供类所有属性的get和set方法,此外还提供了equals、canEqual、hashCode、toString方法
@ToString:注解在类上,生成toString方法
@EqualAndHashCode:注解在类上,生成equals、canEqual、hashCode方法
@NoArgsConstructor:注解在类上;生成一个无参的构造方法
@AllArgsConstructor:注解在类上;生成一个全参的构造方法

  • 举例使用

在User实体类中添加@Data注解,相当于提供类所有属性的get和set方法,此外还提供了equals、canEqual、hashCode、toString方法

  1. import lombok.*;
  2. import javax.persistence.Entity;
  3. import javax.persistence.GeneratedValue;
  4. import javax.persistence.Id;
  5. @Entity
  6. @Data
  7. public class User {
  8. @Id
  9. @GeneratedValue
  10. private Long id;
  11. private String name;
  12. private Integer age;
  13. private String sex;
  14. private String address;
  15. }

在编译后的class文件中

  1. import javax.persistence.Entity;
  2. import javax.persistence.GeneratedValue;
  3. import javax.persistence.Id;
  4. @Entity
  5. public class User {
  6. @Id
  7. @GeneratedValue
  8. private Long id;
  9. private String name;
  10. private Integer age;
  11. private String sex;
  12. private String address;
  13. public User() { }
  14. public Long getId() { return this.id;}
  15. public String getName() { return this.name;}
  16. public Integer getAge() { return this.age;}
  17. public String getSex() { return this.sex;}
  18. public String getAddress() { return this.address;}
  19. public void setId(final Long id) { this.id = id;}
  20. public void setName(final String name) { this.name = name;}
  21. public void setAge(final Integer age) { this.age = age;}
  22. public void setSex(final String sex) { this.sex = sex;}
  23. public void setAddress(final String address) { this.address = address;}
  24. public boolean equals(final Object o) {
  25. if (o == this) {
  26. return true;
  27. } else if (!(o instanceof User)) {
  28. return false;
  29. } else {
  30. User other = (User)o;
  31. if (!other.canEqual(this)) {
  32. return false;
  33. } else {
  34. label71: {
  35. Object this$id = this.getId();
  36. Object other$id = other.getId();
  37. if (this$id == null) {
  38. if (other$id == null) {
  39. break label71;
  40. }
  41. } else if (this$id.equals(other$id)) {
  42. break label71;
  43. }
  44. return false;
  45. }
  46. Object this$name = this.getName();
  47. Object other$name = other.getName();
  48. if (this$name == null) {
  49. if (other$name != null) {
  50. return false;
  51. }
  52. } else if (!this$name.equals(other$name)) {
  53. return false;
  54. }
  55. label57: {
  56. Object this$age = this.getAge();
  57. Object other$age = other.getAge();
  58. if (this$age == null) {
  59. if (other$age == null) {
  60. break label57;
  61. }
  62. } else if (this$age.equals(other$age)) {
  63. break label57;
  64. }
  65. return false;
  66. }
  67. Object this$sex = this.getSex();
  68. Object other$sex = other.getSex();
  69. if (this$sex == null) {
  70. if (other$sex != null) {
  71. return false;
  72. }
  73. } else if (!this$sex.equals(other$sex)) {
  74. return false;
  75. }
  76. Object this$address = this.getAddress();
  77. Object other$address = other.getAddress();
  78. if (this$address == null) {
  79. if (other$address == null) {
  80. return true;
  81. }
  82. } else if (this$address.equals(other$address)) {
  83. return true;
  84. }
  85. return false;
  86. }
  87. }
  88. }
  89. protected boolean canEqual(final Object other) {
  90. return other instanceof User;
  91. }
  92. public int hashCode() {
  93. int PRIME = true;
  94. int result = 1;
  95. Object $id = this.getId();
  96. int result = result * 59 + ($id == null ? 43 : $id.hashCode());
  97. Object $name = this.getName();
  98. result = result * 59 + ($name == null ? 43 : $name.hashCode());
  99. Object $age = this.getAge();
  100. result = result * 59 + ($age == null ? 43 : $age.hashCode());
  101. Object $sex = this.getSex();
  102. result = result * 59 + ($sex == null ? 43 : $sex.hashCode());
  103. Object $address = this.getAddress();
  104. result = result * 59 + ($address == null ? 43 : $address.hashCode());
  105. return result;
  106. }
  107. public String toString() {
  108. return "User(id=" + this.getId() + ", name=" + this.getName() + ", age=" + this.getAge() + ", sex=" + this.getSex() + ", address=" + this.getAddress() + ")";
  109. }
  110. }

发表评论

表情:
评论列表 (有 0 条评论,506人围观)

还没有评论,来说两句吧...

相关阅读

    相关 IntelliJ IDEA lombok

    IntelliJ IDEA是一款非常优秀的集成开发工具,功能强大,而且插件众多。lombok是开源的代码生成库,是一款非常实用的小工具,我们在编辑实体类时可以通过lombok注