我刚刚Lombok
在SpringBoot项目中添加了依赖项,因此不必重复获取,设置和构造函数的行。
这是我的代码示例:
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
所有的getter和setter都变成红色,并显示错误信息
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,则此处提供了说明。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句