轉載請註明: TheViper http://www.cnblogs.com/TheViper html
在以前的文章中說的是一個一對多狀況下mybatis的自動分組,這篇說下多個一對多的狀況。java
好比qq空間裏的說說sql
能夠看到,說說和評論是一對多,評論又和回覆是一對多。mybatis
moodpost
public class Mood { private int mood_id; private String mood_content; private String mood_time; private User user; private List<MoodComment> moodComments; //getter,setter.. }
mood commenturl
public class MoodComment { private int moodcommentid; private String comment_content; private String comment_time; private User user; private Mood mood; private List<MoodCommentReply> moodCommentReplys; //getter,setter }
mood replyspa
public class MoodCommentReply { private int moodreplyid; private String reply_content; private String reply_time; private User user; private MoodComment moodComment; //getter,setter }
mood表code
moodcomment表htm
moodcommentreply表blog
user表
作法很容易想到,就是把上一篇resultmap改一下就能夠了。
<resultMap id="MoodResult" type="Mood"> <id property="mood_id" column="mood_id" /> <association property="user" javaType="User"> <id property="id" column="mood_userid"/> <result property="name" column="mood_user"/> </association> <collection property="moodComments" ofType="MoodComment"> <id property="moodcommentid" column="moodcommentid" /> <association property="user" javaType="User"> <id property="id" column="comment_userid" /> <result property="name" column="comment_user"/> </association> <collection property="moodCommentReplys" ofType="MoodCommentReply"> <association property="user" javaType="User"> <id property="id" column="reply_userid" /> <result property="name" column="reply_user"/> </association> </collection> </collection> </resultMap>
能夠看到在<collection>裏面再加一個<collection>.不過,要注意在裏面的<collection>加上<id>,不然mybatis不會自動進行第二次分組。
至於sql,仍是把三個表數據一併取出就能夠了,mybatis會自動分組。
SELECT u1.name AS mood_user,u2.name AS comment_user,u3.name AS reply_user, mood.mood_id,mood.id AS mood_userid,mood_content,mood_time, moodcomment.moodcommentid,moodcomment.id AS comment_userid,moodcomment.comment_content,moodcomment.comment_time, moodcommentreply.moodcommentid,moodcommentreply.id AS reply_userid,reply_content,reply_time FROM mood LEFT JOIN moodcomment ON moodcomment.mood_id=mood.mood_id LEFT JOIN moodcommentreply ON moodcommentreply.moodcommentid=moodcomment.moodcommentid LEFT JOIN USER AS u1 ON mood.id=u1.id LEFT JOIN USER AS u2 ON moodcomment.id=u2.id LEFT JOIN USER AS u3 ON moodcommentreply.id=u3.id ORDER BY mood_time DESC,moodcomment.comment_time DESC,reply_time DESC
問題有來了,若是隻要評論的前2條,可是那2條評論的全部回覆要所有選出。
好比
仍是用(譯)如何在sql中選取每一組的第一行/最後行/前幾行裏面提到的user variables方法。
SET @num := 0, @type := ''; SELECT u1.name AS mood_user,u2.name AS comment_user,u3.name AS reply_user, mood.mood_id,mood.id AS mood_userid,mood_content,mood_time, moodcomment2.moodcommentid,moodcomment2.id AS comment_userid,moodcomment2.comment_content,moodcomment2.comment_time, moodcommentreply.moodcommentid,moodcommentreply.id AS reply_userid,reply_content,reply_time FROM mood LEFT JOIN (SELECT * FROM ( SELECT moodcommentid,comment_content,comment_time,id,mood_id, @num := IF(@type = mood_id, @num + 1, 1) AS row_number, @type := mood_id AS dummy FROM moodcomment ORDER BY mood_id ) AS moodcomment1 WHERE moodcomment1.row_number <= 2) AS moodcomment2 ON moodcomment2.mood_id=mood.mood_id LEFT JOIN moodcommentreply ON moodcommentreply.moodcommentid=moodcomment2.moodcommentid LEFT JOIN USER AS u1 ON mood.id=u1.id LEFT JOIN USER AS u2 ON moodcomment2.id=u2.id LEFT JOIN USER AS u3 ON moodcommentreply.id=u3.id ORDER BY mood_time DESC,moodcomment2.comment_time DESC,reply_time DESC
注意幾個moodcomment的命名空間。還有須要在jdbcUrl中加上allowMultiQueries=true,由於set variable也是一次查詢。