英文:
How to use Lambok dependency in SpringBoot?
问题
我刚刚在我的SpringBoot项目中添加了Lombok
依赖,这样我就不必重复编写获取器、设置器和构造函数的代码了。
以下是我的代码示例:
Book.java
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
@Entity
@Table(name = "Books")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String title;
private String ISBN;
private String author;
private String issuer;
private Integer dateOfIssue;
private Boolean IsRented;
}
但是现在在我的BookService.java
中,所有的获取器和设置器都变成了红色,错误显示为:
Cannot resolve method 'setTitle' in 'Book'
这是我尝试使用获取器和设置器的方式:
public Book updateBook(Book book){
Book existingBook = bookRepo.findById(book.getId()).orElse(null);
existingBook.setTitle(book.getTitle());
existingBook.setISBN(book.getISBN());
existingBook.setAuthor(book.getAuthor());
existingBook.setIssuer(book.getIssuer());
existingBook.setDateOfIssue(book.getDateOfIssue());
existingBook.setDateOfIssue(book.getDateOfIssue());
existingBook.setRented(book.getRented());
return bookRepo.save(existingBook);
}
为什么会发生这种情况?当我像下面这样编写我的获取器和设置器时:
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
一切都还好,但是当我删除它并添加了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
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
@Entity
@Table(name = "Books")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String title;
private String ISBN;
private String author;
private String issuer;
private Integer dateOfIssue;
private Boolean IsRented;
}
But now in my BookService.java
I have all the getters and setters turned red with error saying
Cannot resolve method 'setTitle' in 'Book'
This is how I am trying to use getters and setters:
public Book updateBook(Book book){
Book existingBook = bookRepo.findById(book.getId()).orElse(null);
existingBook.setTitle(book.getTitle());
existingBook.setISBN(book.getISBN());
existingBook.setAuthor(book.getAuthor());
existingBook.setIssuer(book.getIssuer());
existingBook.setDateOfIssue(book.getDateOfIssue());
existingBook.setDateOfIssue(book.getDateOfIssue());
existingBook.setRented(book.getRented());
return bookRepo.save(existingBook);
}
Why is that happening? When I had my getters and setters written like:
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
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
安装完成后,它将显示为:
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论