公共接口 LmsRepository 扩展了 CrudRepository 我没有 findOne 方法来获取单个计数,所以当我使用 findById 时,我得到了这个异常。“在类型 [java.util.Optional] 上找不到属性 [id]” 我该如何解决这个问题?这是我的 CrudRepo@Repositorypublic interface LmsRepository extends CrudRepository<Book, Long> {}实体文件@Entity(name="lms_tbl")public class Book {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private long id;@Column(name="book_name")private String bookName;private String author;@Column(name="purchase_date")@Temporal(TemporalType.DATE)private Date purchaseDate;public long getId() { return id;}public void setId(long id) { this.id = id;}and other....服务文件@Servicepublic class LmsService {@Autowiredprivate LmsRepository lmsRepository;public Collection<Book> findAllBooks(){ ArrayList<Book> books = new ArrayList<Book>(); for (Book book : lmsRepository.findAll()) { books.add(book); } return books;}public void deleteBook(long id) { lmsRepository.deleteById(id);}public Optional<Book> findById(Long id) { return lmsRepository.findById(id);}}控制器文件@Controllerpublic class MainController {@Autowiredprivate LmsService lmsService;@GetMapping("/")public String index(HttpServletRequest req) { req.setAttribute("books", lmsService.findAllBooks()); req.setAttribute("mode","BOOK_VIEW"); return "index";}@GetMapping("/updateBook")public String index(@RequestParam Long id,HttpServletRequest req) { req.setAttribute("book", lmsService.findById(id)); req.setAttribute("mode","BOOK_EDIT"); return "index";}}我尝试在 CrudRepo 中添加新方法,但它不起作用。
添加回答
举报
0/150
提交
取消