问题描述
我刚刚在SpringBoot项目中添加了Lombok
依赖项,因此不必重复执行getter,setter和构造函数的行……
这是我的代码示例:
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'
这就是我尝试使用getter和setter的方法:
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);
}
为什么会这样?当我的getter和setter方法写为:
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
一切都还好,但是当我删除它并添加了Lombok时,似乎无法接触到我的吸气器和吸气器。
解决方法
似乎您正在使用IDE。为了使您的IDE能够识别Lombok自动生成的代码,您需要将其安装在IDE上,并将其作为项目的依赖项。龙目岛(Lombok)网站上有关于如何执行此操作的说明。例如,如果您使用的是Eclipse,the instructions are here。
,您还需要在IDE中安装它。在下面找到步骤的链接
https://projectlombok.org/setup/eclipse
安装后,它将显示为