因?yàn)樵谧鰣D書管理系統(tǒng)的時(shí)候航缀,有一個(gè)頁面序调,顯示一條圖書數(shù)據(jù),顯示多條評(píng)論數(shù)據(jù)余舶。
做到這里就很糾結(jié)了啊鸭,使用了Controller跳轉(zhuǎn)到Controller查詢兩次這個(gè)辦法,但是對(duì)象經(jīng)過Model的傳遞匿值,跳轉(zhuǎn)到第二個(gè)Controller的時(shí)候赠制,對(duì)象的值就只剩下了id,其他的值為空挟憔。沒找到解決辦法钟些。
于是想到了聯(lián)合查詢,剛開始用association嘗試的绊谭,但是發(fā)現(xiàn)association只能存普通數(shù)據(jù)類型厘唾。
后來找到了解決辦法:使用Collection將查詢結(jié)果封裝成集合
Book類vo代碼
private Integer id;
private String bookName;
private String author;
private Date publicationDate;
...
private List<Comment> comment; //封裝了一個(gè)List集合,用來存多條Comment的數(shù)據(jù)
...//省略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);
//時(shí)間格式轉(zhuǎn)化 此處可以省略
book.setTime(dateToString(book.getPublicationDate()));
//時(shí)間格式轉(zhuǎn)化 此處可以省略
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代表的是集合里封裝的是什么類型抚垃。
此處還有一個(gè)問題,集合中查詢到的結(jié)果只有一條,而數(shù)據(jù)庫(kù)里存了三條鹤树。
后經(jīng)查詢發(fā)現(xiàn)铣焊,是因?yàn)閮蓚€(gè)表中的主鍵都為id,在寫查詢語句的時(shí)候罕伯,給兩個(gè)id都起個(gè)別名就好了曲伊。
掛個(gè)原文鏈接,感謝作者追他。https://blog.csdn.net/yangjiehuan/article/details/78523080