Mybatis多表联查
作者:互联网
之前写代码一直用for循环读写数据库操作,直到在某个公众号看到一篇每日离职小技巧的文章,才知道问题的严重性。”读写数据库操作,写在for循环中,什么子查询,关联查询,左连接,右连接,通通不用,就是要先查一张表,然后遍历结果,再查另一张表…”。想想这不就是我嘛?这才明白当读取上万数据的时候,服务器内存是非常容易爆掉的,吓的我连夜学习了多表关联查询,,最终写了mapper.xml里的一对多查询如下:
<resultMap id="NoteVO" type="com.easy.domain.NoteVO">
<result property="id" column="n_id"/>
<result property="nickName" column="nick_name"/>
<result property="openid" column="n_openid"/>
<result property="avatarUrl" column="avatar_url"/>
<result property="desc" column="desc"/>
<result property="createTime" column="create_time"/>
<collection notNullColumn="comment" property="commentsList" javaType="java.util.List" ofType="com.easy.domain.NoteComments">
<result property="noteId" column="n_id"/>
<result property="comment" column="comment"/>
<result property="openid" column="c_openid"/>
<result property="nickName" column="c_nick_name"/>
</collection>
<collection property="noteLikes" javaType="java.util.List" ofType="com.easy.domain.NoteLike">
<result property="noteId" column="l_n_id"/>
<result property="id" column="l_id"/>
<result property="openid" column="l_openid"/>
<result property="nickName" column="l_nick_name"/>
</collection>
<collection property="noteImgs" javaType="java.util.List" ofType="com.easy.domain.NoteImgs">
<result property="noteId" column="im_n_id"/>
<result property="imgUrl" column="img_url"/>
</collection>
</resultMap>
<select id="getAllList" resultMap="NoteVO" parameterType="int">
SELECT note.id n_id,note.openid n_openid,note.`desc`,note.create_time,note.`type`,
note_comments.note_id c_n_id,note_comments.`comment`,note_comments.openid c_openid,note_comments.nick_name c_nick_name,
note_like.id l_id,note_like.note_id l_n_id,note_like.openid l_openid,note_like.nick_name l_nick_name,
note_imgs.note_id im_n_id,note_imgs.img_url,
`user`.openid,`user`.avatar_url,`user`.nick_name
FROM note
LEFT OUTER JOIN note_comments ON note.id=note_comments.note_id
LEFT OUTER JOIN note_like ON note.id= note_like.note_id
LEFT OUTER JOIN note_imgs ON note.id=note_imgs.note_id
LEFT OUTER JOIN `user` ON note.openid=`user`.openid
WHERE note.type=#{type}
ORDER BY note.create_time DESC
</select>
再改了java代码:
@Override
public CommonResult getAllList(int type) {
List<NoteVO> allList = noteMapper.getAllList(type);
return CommonResult.success(allList);
}
对比之前写的代码:
@Override
public CommonResult<List<NoteVO>> getAll(int type) {
List<Note> notes = lambdaQuery().eq(Note::getType,type).orderByDesc(Note::getCreateTime).list();
// List<Note> notes = noteMapper.selectList(new QueryWrapper<>());
List<NoteVO> noteVOList = new ArrayList<>();
for (Note note : notes) {
Long noteId = note.getId();
Date createTime = note.getCreateTime();
String openid = note.getOpenid();
String desc = note.getDesc();
User user = userService.lambdaQuery().eq(User::getOpenid, openid).one();
List<NoteLike> likeList = noteLikeService.lambdaQuery().eq(NoteLike::getNoteId, noteId).list();
List<NoteImgs> imgsList = noteImgsService.lambdaQuery().eq(NoteImgs::getNoteId, noteId).list();
List<NoteComments> commentsList = noteCommentsService.lambdaQuery().eq(NoteComments::getNoteId, noteId).list();
NoteVO noteVO = new NoteVO();
String nickName = user.getNickName();
String avatarUrl = user.getAvatarUrl();
//System.out.println(user);
noteVO.setId(noteId);
noteVO.setOpenid(openid);
noteVO.setAvatarUrl(avatarUrl);
noteVO.setCommentsList(commentsList);
noteVO.setCreateTime(createTime);
noteVO.setDesc(desc);
noteVO.setNoteImgs(imgsList);
noteVO.setNoteLikes(likeList);
noteVO.setNickName(nickName);
noteVOList.add(noteVO);
}
return CommonResult.success(noteVOList);
}
查询的效率大大提高。
还是需要继续学习啊。。
标签:openid,多表,note,user,noteVO,Mybatis,联查,type,id 来源: https://blog.csdn.net/adminzzk/article/details/120659079