当前位置 博文首页 > 范栩的博客:Mybatis Collection联合查询

    范栩的博客:Mybatis Collection联合查询

    作者:[db:作者] 时间:2021-08-31 09:50

    因为在做图书管理系统的时候,有一个页面,显示一条图书数据,显示多条评论数据

    做到这里就很纠结了,使用了Controller跳转到Controller查询两次这个办法,但是对象经过Model的传递,跳转到第二个Controller的时候,对象的值就只剩下了id,其他的值为空。没找到解决办法。

    于是想到了联合查询,刚开始用association尝试的,但是发现association只能存普通数据类型。

    后来找到了解决办法:使用Collection将查询结果封装成集合

    Book类vo代码

        private Integer id;
        private String bookName;
        private String author;
        private Date publicationDate;
        ...
        private List<Comment> comment;	//封装了一个List集合,用来存多条Comment的数据
        ...//省略get set方法  
    

    Comment类vo代码

    	private Integer id;
    	private String commentUser;
    	private Integer commentBookId;
    	private Date commentTime;
    	private String content;
    	private Integer mylike;
    	private String time;
        ...//省略get set方法  
    

    Controller代码

    	@RequestMapping("getBookDetail")
    	public String getBookDetail(Model model,@RequestParam(value="id",required=true)int id){
    		Book book = bs.getBookById(id);
    		model.addAttribute("book",book);
    		return "bookDetail.jsp";
    	}
    

    ServiceImpl代码

    	@Override
    	public Book getBookById(int id) {
    		// TODO Auto-generated method stub
    		Book book = bm.getBookAndComment(id);
            //时间格式转化  此处可以省略
    		book.setTime(dateToString(book.getPublicationDate()));
             //时间格式转化  此处可以省略
    		for(Comment c : book.getComment()){
    			c.setTime(dateToString(c.getCommentTime()));
    		}
    		return book;
    	}
    

    mapper.xml代码

    <!-- Book getBookAndComment(Book book); -->
    	<select id="getBookAndComment" resultMap="BookAndComment">
    		select b.id bid, book_name, author, publication_date, publishing_house,
    		cover,book_type, status,inventory, price,c.id cid, comment_user,
    		comment_book_id, comment_time, content, mylike
    		from book b
    		join comment c
    		on b.id=c.comment_book_id
    		where c.comment_book_id=#{id}
    	</select>
    
    	<resultMap id="BookAndComment" type="com.book.vo.Book">
    		<id column="bid" property="id" jdbcType="INTEGER" />
    		<result column="book_name" property="bookName" jdbcType="VARCHAR" />
    		<result column="author" property="author" jdbcType="VARCHAR" />
    		<result column="publication_date" property="publicationDate"
    			jdbcType="TIMESTAMP" />
    		<result column="publishing_house" property="publishingHouse"
    			jdbcType="VARCHAR" />
    		<result column="cover" property="cover" jdbcType="VARCHAR" />
    		<result column="book_type" property="bookType" jdbcType="VARCHAR" />
    		<result column="status" property="status" jdbcType="INTEGER" />
    		<result column="inventory" property="inventory" jdbcType="INTEGER" />
    		<result column="price" property="price" jdbcType="DOUBLE" />
    		<collection property="comment" ofType="com.book.vo.Comment">
    			<id column="cid" property="id" />
    			<result column="comment_user" property="commentUser" />
    			<result column="comment_book_id" property="commentBookId" />
    			<result column="comment_time" property="commentTime" />
    			<result column="content" property="content" />
    			<result column="mylike" property="mylike" />
    		</collection>
    

    collection里的 property代表的是vo类里集合的名字,ofType代表的是集合里封装的是什么类型。

    此处还有一个问题,集合中查询到的结果只有一条,而数据库里存了三条。
    后经查询发现,是因为两个表中的主键都为id,在写查询语句的时候,给两个id都起个别名就好了。
    挂个参考链接,感谢作者。

    cs