Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在SpringMVC中使用内部联接_Java_Mysql_Spring_Hibernate_Spring Mvc - Fatal编程技术网

Java 如何在SpringMVC中使用内部联接

Java 如何在SpringMVC中使用内部联接,java,mysql,spring,hibernate,spring-mvc,Java,Mysql,Spring,Hibernate,Spring Mvc,您好,我编写了外键和链接表之间的内部联接,但无法打印第二个表中的数据。我在Javascript中遇到一个错误,可能第二个表中的数据没有出现。我需要用户表中的用户名,但我无法访问 图书管理员 @Autowired private BooksService booksService; @Autowired private BookCommentsService bookCommentsService; @RequestMapping(value = "/bookd

您好,我编写了外键和链接表之间的内部联接,但无法打印第二个表中的数据。我在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;
    }
@Autowired
私人书店服务;
@自动连线
私人书籍评论服务书籍评论服务;
@请求映射(value=“/bookdetail”)
公共模型和视图showBookDetailForm(@RequestParam long book\u id)
{
ModelAndView mav=新的ModelAndView(“bookdetail”);
List book=bookservice.bookGetWithId(图书id);
List listBookComments=bookCommentsService.listAllDetailComments(图书id);
mav.addObject(“listBook”,book);
mav.addObject(“listBookComments”,listBookComments);
返回mav;
}
图书评论服务

@Service
public class BookCommentsService {
    @Autowired
    private BookCommentsRepository repository;
    
    public List<BookComments> listAllDetailComments(long book_id)
    {
        return repository.listAllDetailComments(book_id);
    }
}
@Service
public class BookCommentsService {
    @Autowired
    private BookCommentsRepository repository;
    
    public List<BookCommentsDto> listAllDetailComments(long book_id)
    {
        return repository.listAllDetailComments(book_id);
    }
}
@服务
公共类图书评论服务{
@自动连线
私有图书评论存储库;
公共列表listAllDetailComments(long book\u id)
{
返回repository.listAllDetailComments(图书id);
}
}
书评

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);    
}
公共接口BookCommentsRepository扩展了Crudepository{
@查询(value=“从BookComments中选择b b内部加入用户u on b.comment\u user\u id=u.user\u id,其中b.comment\u book\u id=:book\u id”)
公共列表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>

(这里没问题)${bookcomments.book_comment}
(错误在这里)${bookcomments.user_name}

您应该使用comment\u user\u id并关闭大括号,${bookcomments.comment\u user\u id}应该可以正常工作。

您应该使用comment\u user\u id并关闭大括号,${bookcomments.comment\u user\u id}应该可以正常工作。

要解决问题,必须使用另一个对象(称为数据传输对象;DTO)其中将包含BookComments中的详细信息和用户名

例如:

package com.myapp.book;

public class BookCommentsDto{
    private String bookComment;
    private String username;
    // You can add any other attributes that you need from the table book_comments
    // add them in the constructor as well

    public BookCommentsDto(String bookComment, String username){
         this.bookComment = bookComment;
         this.username = username;
    }
}
我将把类
bookcommentsdo
称为dto。上面的dto是您可能需要的简单dto。您可以从dto中的book comment中添加所需的其他详细信息,并确保将它们也添加到构造函数中。我在顶部包含了一个虚拟包名,因为我们需要在将要编写的查询中提到dto的完整类路径。在下面的查询中,我根据sql的结果创建dto。在查询中,我使用
BookCommentsDto
的构造函数创建将返回的对象

@Query(value = "Select new com.myapp.book.BookCommentsDto(b.book_comment, u.user_name) From BookComments b inner join Users u on b.comment_user_id = u.user_id where b.comment_book_id= :book_id")
public List<BookCommentsDto> listAllDetailComments(@Param("book_id") long book_id);
控制器级

Book book = bookService.getBook(bookId); // only this one is querying the DB
List<BookComment> bookComments = book.getComments();
List<User> users = book.getComments().getUsers();
Book Book=bookService.getBook(bookId);//只有这一个正在查询数据库
List bookComments=book.getComments();
列表用户=book.getComments().getUsers();
提前阅读主题(阅读顺序与发布顺序相同):

  • HQL-Hibernate查询语言
  • Hibernate的N+1问题
  • JPA规范(用于创建动态查询)
  • QueryDsl——JPA规范的改进

  • 要解决您的问题,您必须使用另一个对象(称为数据传输对象;DTO),该对象将包含BookComments中的详细信息和用户名

    例如:

    package com.myapp.book;
    
    public class BookCommentsDto{
        private String bookComment;
        private String username;
        // You can add any other attributes that you need from the table book_comments
        // add them in the constructor as well
    
        public BookCommentsDto(String bookComment, String username){
             this.bookComment = bookComment;
             this.username = username;
        }
    }
    
    我将把类
    bookcommentsdo
    称为dto。上面的dto是您可能需要的简单dto。您可以从dto中的book comment中添加所需的其他详细信息,并确保将它们也添加到构造函数中。我在顶部包含了一个虚拟包名,因为我们需要在将要编写的查询中提到dto的完整类路径。在下面的查询中,我根据sql的结果创建dto。在查询中,我使用
    BookCommentsDto
    的构造函数创建将返回的对象

    @Query(value = "Select new com.myapp.book.BookCommentsDto(b.book_comment, u.user_name) From BookComments b inner join Users u on b.comment_user_id = u.user_id where b.comment_book_id= :book_id")
    public List<BookCommentsDto> listAllDetailComments(@Param("book_id") long book_id);
    
    控制器级

    Book book = bookService.getBook(bookId); // only this one is querying the DB
    List<BookComment> bookComments = book.getComments();
    List<User> users = book.getComments().getUsers();
    
    Book Book=bookService.getBook(bookId);//只有这一个正在查询数据库
    List bookComments=book.getComments();
    列表用户=book.getComments().getUsers();
    
    提前阅读主题(阅读顺序与发布顺序相同):

  • HQL-Hibernate查询语言
  • Hibernate的N+1问题
  • JPA规范(用于创建动态查询)
  • QueryDsl——JPA规范的改进

  • 哦,我很抱歉复制粘贴问题。我改变了它,但不再工作。我可以访问bookcomments.comment\u user\u id,但我需要bookcomments.comment\u user\u name(user\u name在Users表中),bookcomments表没有user\u name列,我使用内部联接,但我无法访问Users表中的user\u name。哦,我很抱歉复制粘贴问题。我改变了它,但不再工作。我可以访问bookcomments.comment\u user\u id,但我需要bookcomments.comment\u user\u name(user\u name在Users表中),bookcomments表没有user\u name列,我使用内部联接,但我无法访问Users表中的user\u name。非常感谢。我是Java、spring和hibernate的新手。编辑代码真的很棒,对我来说非常有用。我注意到了我应该审查的清单,我将按顺序仔细审查这些清单。再次非常感谢。你是非常受欢迎的朋友,我很高兴能帮助你。快乐编码:非常感谢你。我是Java、spring和hibernate的新手。编辑代码真的很棒,对我来说非常有用。我注意到了我应该审查的清单,我将按顺序仔细审查这些清单。再次非常感谢。你是非常受欢迎的朋友,我很高兴能帮助你。快乐编码:D