【发布时间】:2020-10-12 13:57:53
【问题描述】:
您好,我在外键和链接表之间编写了内部连接,但我无法打印第二个表中的数据。我在 Javascript 中遇到错误,可能第二个表中的数据没有出现。我需要用户表中的用户名,但我无法访问。
图书控制器
@Autowired
private BooksService booksService;
@Autowired
private BookCommentsService bookCommentsService;
@RequestMapping(value = "/bookdetail")
public ModelAndView showBookDetailForm(@RequestParam long book_id)
{
ModelAndView mav= new ModelAndView("bookdetail");
List<Books> book=booksService.bookGetWithId(book_id);
List<BookComments> listBookComments= bookCommentsService.listAllDetailComments(book_id);
mav.addObject("listBook",book);
mav.addObject("listBookComments", listBookComments);
return mav;
}
BookCommentsService
@Service
public class BookCommentsService {
@Autowired
private BookCommentsRepository repository;
public List<BookComments> listAllDetailComments(long book_id)
{
return repository.listAllDetailComments(book_id);
}
}
BookCommentsRepository
public interface BookCommentsRepository extends CrudRepository<BookComments, Long> {
@Query(value = "Select b From BookComments b inner join Users u on b.comment_user_id = u.user_id where b.comment_book_id= :book_id")
public List<BookComments> listAllDetailComments(@Param("book_id") long book_id);
}
BookDetail.js
<c:forEach items="${listBookComments}" var="bookcomments">
(No problem here) ${bookcomments.book_comment}
(The error is here) <h4>${bookcomments.user_name}</h4>
</c:forEach>
【问题讨论】:
标签: java mysql spring hibernate spring-mvc