如何在SpringBoot中使用Lombok依赖?

huangapple go评论113阅读模式
英文:

How to use Lambok dependency in SpringBoot?

问题

我刚刚在我的SpringBoot项目中添加了Lombok依赖,这样我就不必重复编写获取器、设置器和构造函数的代码了。

以下是我的代码示例:

Book.java

  1. @NoArgsConstructor
  2. @AllArgsConstructor
  3. @Getter
  4. @Setter
  5. @ToString
  6. @Entity
  7. @Table(name = "Books")
  8. public class Book {
  9. @Id
  10. @GeneratedValue(strategy = GenerationType.IDENTITY)
  11. private Integer id;
  12. private String title;
  13. private String ISBN;
  14. private String author;
  15. private String issuer;
  16. private Integer dateOfIssue;
  17. private Boolean IsRented;
  18. }

但是现在在我的BookService.java中,所有的获取器和设置器都变成了红色,错误显示为:

  1. Cannot resolve method 'setTitle' in 'Book'

这是我尝试使用获取器和设置器的方式:

  1. public Book updateBook(Book book){
  2. Book existingBook = bookRepo.findById(book.getId()).orElse(null);
  3. existingBook.setTitle(book.getTitle());
  4. existingBook.setISBN(book.getISBN());
  5. existingBook.setAuthor(book.getAuthor());
  6. existingBook.setIssuer(book.getIssuer());
  7. existingBook.setDateOfIssue(book.getDateOfIssue());
  8. existingBook.setDateOfIssue(book.getDateOfIssue());
  9. existingBook.setRented(book.getRented());
  10. return bookRepo.save(existingBook);
  11. }

为什么会发生这种情况?当我像下面这样编写我的获取器和设置器时:

  1. public String getTitle() {
  2. return title;
  3. }
  4. public void setTitle(String title) {
  5. this.title = title;
  6. }

一切都还好,但是当我删除它并添加了Lombok后,我似乎无法访问我的获取器和设置器。

英文:

I have just added Lombok dependency to my SpringBoot project so I don't have to repeat lines of getters, setters and constructors...

Here is my code example:

Book.java

  1. @NoArgsConstructor
  2. @AllArgsConstructor
  3. @Getter
  4. @Setter
  5. @ToString
  6. @Entity
  7. @Table(name = "Books")
  8. public class Book {
  9. @Id
  10. @GeneratedValue(strategy = GenerationType.IDENTITY)
  11. private Integer id;
  12. private String title;
  13. private String ISBN;
  14. private String author;
  15. private String issuer;
  16. private Integer dateOfIssue;
  17. private Boolean IsRented;
  18. }

But now in my BookService.java I have all the getters and setters turned red with error saying

  1. Cannot resolve method 'setTitle' in 'Book'

This is how I am trying to use getters and setters:

  1. public Book updateBook(Book book){
  2. Book existingBook = bookRepo.findById(book.getId()).orElse(null);
  3. existingBook.setTitle(book.getTitle());
  4. existingBook.setISBN(book.getISBN());
  5. existingBook.setAuthor(book.getAuthor());
  6. existingBook.setIssuer(book.getIssuer());
  7. existingBook.setDateOfIssue(book.getDateOfIssue());
  8. existingBook.setDateOfIssue(book.getDateOfIssue());
  9. existingBook.setRented(book.getRented());
  10. return bookRepo.save(existingBook);
  11. }

Why is that happening? When I had my getters and setters written like:

  1. public String getTitle() {
  2. return title;
  3. }
  4. public void setTitle(String title) {
  5. this.title = title;
  6. }

everything was okay but as I removed that and added Lombok it seems like I can't reach to my getters and setters.

答案1

得分: 3

似乎您正在使用集成开发环境(IDE)。为了使您的IDE能够识别由Lombok自动生成的代码,您需要将其安装到您的IDE中,并将其作为项目的依赖项。Lombok的网站提供了关于如何进行此操作的说明。例如,如果您正在使用Eclipse,可以在这里找到说明

英文:

It seems you are using an IDE. For your IDE to recognize the code autogenerated by Lombok you need to install it on your IDE as well as having it as a dependency of your project. The web site for Lombok has instructions on how to do this. For example, if you are using Eclipse, the instructions are here.

答案2

得分: 1

你还需要在你的集成开发环境中安装它。以下是步骤的链接:

https://projectlombok.org/setup/eclipse

安装完成后,它将显示为:

如何在SpringBoot中使用Lombok依赖?

英文:

You need to install it in your IDE as well. Find the link below for steps

https://projectlombok.org/setup/eclipse

Once you install it it will show as

如何在SpringBoot中使用Lombok依赖?

huangapple
  • 本文由 发表于 2020年8月27日 21:51:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63617495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定