一、目的

1、上一篇写了mybatis基本的CURD,接着上一篇实现一个 多级关联

获取一篇文章以及该文章的所有评论、评论的所有回复

二、三张表

news(文章表)comment(评论表)reply(回复表)

news对应的domain

package com.bjx.domain;
import java.util.List;
public class News {private int id;private String title;private String content;private String addtime;private int uid;private List<Comment> comment;public List<Comment> getComment() {return comment;}public void setComment(List<Comment> comment) {this.comment = comment;}//其它的getter、setter方法省略
}

comment对应的domain

package com.bjx.domain;
import java.util.List;
public class Comment {private int id;private String content;private int uid;private int nid;private String addtime;private List<Reply> reply;public List<Reply> getReply() {return reply;}public void setReply(List<Reply> reply) {this.reply = reply;}//其它的getter、setter方法省略
}

reply对应的domain

package com.bjx.domain;
public class Reply {private int id;private int comment_id;private String message;private String addtime;private int uid;//所有的getter、setter方法省略}

三、NewsMapper.xml映射文件(核心)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.bjx.dao.NewsDao"><resultMap type="com.bjx.domain.News" id="resultNewsAndComment" autoMapping="true"><id column="newsid" property="id"></id><collection property="comment" ofType="com.bjx.domain.Comment" resultMap="resultCommentAndReply"><!-- 如果仅仅是一层关联(文章关联评论)可以写这样但是现在 评论有关联了回复,就要多一个 resultMap<id property="id" column="cid" /><result column="ccontent" property="content" /><result column="cuid" property="uid" /><result column="caddtime" property="addtime" /> --></collection></resultMap><resultMap type="com.bjx.domain.Comment" id="resultCommentAndReply"><id property="id" column="cid" /><result column="ccontent" property="content" /><result column="cuid" property="uid" /><result column="caddtime" property="addtime" /><collection property="reply" ofType="com.bjx.domain.Reply"><id property="id" column="rid"></id><result column="rcommentid" property="comment_id" /><result column="message" property="message" /><result column="raddtime" property="addtime" /><result column="ruid"  property="uid"/></collection></resultMap><!-- //获取一篇文章以及文章的评论和评论对应的回复 --><select id="selectNewsById" resultMap="resultNewsAndComment" parameterType="int"><!-- select n.id as newsid,n.title,n.content,n.addtime,n.uid,c.nid,c.id as cid,c.content as ccontent,c.uid as cuid,c.addtime as caddtime,r.id as rid,r.message,r.addtime as raddtime,r.uid as ruid,r.comment_id as rcommentid,from news n,comment c,reply r where n.id=c.nid=r.comment_id and n.id=#{id} -->select n.id as newsid,n.title,n.addtime,n.uid,n.content,c.nid,c.id as cid,c.content as ccontent,c.uid as cuid,c.addtime as caddtime,r.id as rid,r.message,r.addtime as raddtime,r.uid as ruid,r.comment_id as rcommentidfrom news n left join comment c on n.id=c.nid left join reply r on c.id=r.comment_id where n.id=#{id};</select>
</mapper>

四、说明

以上映射关系既可以在不使用任何框架,单独的mybatis中获取数据,也可以在整合spring后使用

1、单独使用

先生成mybatis的sqlSession(方法省略),在直接调用:

News n = sqlSession.selectOne("com.bjx.dao.NewsDao.selectNewsById",1);

如果不使用【mapper动态代理】的方式,是可以不写Dao的,这里的语句也可以是:

News n = sqlSession.selectOne("selectNewsById",1);但是mapper方式也是比较主流的方式,一般都会写Dao

使用mapper方式:

NewsDao newsDao = sqlSession.getMapper(NewsDao.class);//这种方式Dao是必需的
News n = newsDao.selectNewsById(1);

2、spring整合后

直接在service层使用注解调用Dao即可

@Autowiredprivate NewsDao newsDao;//获取一篇文章以及文章的评论和评论对应的回复 public News getNewsRow(int id) {News news = newsDao.selectNewsById(id);return news;}

3、该示例返回的结果集:

News [id=1, title=爱满校园 静待花开, content=柳絮纷飞,艳阳高照, addtime=1567219393, uid=23, comment=[Comment [id=1, content=大学时光最难忘, uid=1, nid=0, addtime=1567219310, reply=[Reply [id=1, comment_id=1, message=人生中最美好的四年, addtime=1567219100, uid=26], Reply [id=2, comment_id=1, message=最难忘的四年, addtime=1567218100, uid=27]]], Comment [id=2, content=高中时光最幸福, uid=1, nid=0, addtime=1567219999, reply=[]]]]

二、mybatis 多级关联映射配置相关推荐

  1. MyBatis的关联映射之 一对一(嵌套查询/嵌套结果)

    关联映射概述 在实际的开发中,对数据库的操作常常会涉及多张表,这在面向对象中就涉及了对象与对象之间的关联关系 针对多表之间的操作, MyBatis 提供了关联映射,通过关联映射就可以很好地处理对象与对 ...

  2. 第4章 MyBatis的关联映射和缓存机制

    目录/Contents 第4章 MyBatis的关联映射和缓存机制 学习目标 了解数据表之间的三种关联关系 了解对象之间的三种关系 熟悉关联关系中的嵌套查询和嵌套结果 掌握一对一关联映射 掌握一对多关 ...

  3. Hibernate一对一关联映射配置

    一对一关联 Hibernate提供了两种映射一对一关联关系的方式:按照外键映射和按照主键映射.下面以员工账号和员工档案表为例,介绍这两种映射方式,并使用这两种映射方式分别完成以下持久化操作: (1)保 ...

  4. Mybatis关联映射;Mybatis注解

    Mybatis关联映射 Mybatis关联映射的用途: 在实际的开发过程中,对于数据库的操作除了单表外往往会涉及到多张表,这些操作在面向对象中就涉及到了对象与对象之间的关联关系.针对多表之间的操作,M ...

  5. MyBatis二 MyBatis常见面试题

    一 MyBatis是什么? MyBatis是一款优秀的持久层框架,一个半ORM (对象关系映射)框架,它支持定制化SQL.存储过程以及高级映射.MyBatis避免了几乎所有的JDBC代码和手动设置参数 ...

  6. Hibernatel框架关联映射

    Hibernatel框架关联映射 Hibernate程序执行流程: 1.集合映射 需求:网络购物时,用户购买商品,填写地址 每个用户会有不确定的地址数目,或者只有一个或者有很多.这个时候不能把每条地址 ...

  7. MyBatis学习 之 二、SQL语句映射文件(1)resultMap

    二.SQL语句映射文件(1)resultMap SQL 映射XML 文件是所有sql语句放置的地方.需要定义一个workspace,一般定义为对应的接口类的路径.写好SQL语句映射文件后,需要在MyB ...

  8. (转)MyBatis框架的学习(五)——一对一关联映射和一对多关联映射

    http://blog.csdn.net/yerenyuan_pku/article/details/71894172 在实际开发中我们不可能只是对单表进行操作,必然要操作多表,本文就来讲解多表操作中 ...

  9. Java-Mybatis(二): Mybatis配置解析、resultMap结果集映射、日志、分页、注解开发、Mybatis执行流程分析

    Java-Mybatis-02 学习视频:B站 狂神说Java – https://www.bilibili.com/video/BV1NE411Q7Nx 学习资料:mybatis 参考文档 – ht ...

最新文章

  1. 我的android绘图学习笔记(二)
  2. (shell脚本编程)linux如何利用脚本执行多条命令以及linux如何执行定时任务
  3. css案例学习之div与span的区别
  4. 批量Excel数据导入Oracle数据库
  5. Linux远程复制命令SCP
  6. POJ - 2289 Jamie's Contact Groups(二分图多重匹配)
  7. linux分区 挂盘,linux分区与格式化磁盘挂载磁盘与自动挂载详细教程
  8. 机器学习之基于Fisher线性分类器实现多类人脸的识别
  9. Acrobat DC发布一周年 激活移动时代文件处理革命
  10. 进击的数据中台,企业数字化转型的新引擎
  11. php 显示目录列表,php读取目录列表与文件列表的代码举例
  12. Hadoop核心生态
  13. tp5中php正则怎么写,详解tp5中phpmailer的使用
  14. UWA发布 | 2017 Unity手游体检蓝皮书 — ARPG篇
  15. Django------多表操作
  16. Hyperledger Fabric Composer安装blockchain explorer
  17. 五种 必须了解的CSS选择器
  18. MySQL高级SQL语句(二)
  19. 华为linux系统安装包,一、Linux系统安装
  20. 【Anki 牌组+Markdown笔记分享】汇编语言

热门文章

  1. 7-13 盲盒包装流水线
  2. 6-27 实验9_7_设计函数int getVowel(char str[],char vowel[]); (100 分)
  3. 漫画|假如一个程序员有“社交牛逼症”
  4. xp下对dinput8.dll 游戏键盘输入的模拟 非函数hook
  5. 柳岩直播卖货,三小时豪赚1500万?快手为什么那么带货?
  6. 【踩坑笔记】从零开始部署安装Stable Diffusion 2 WebUI
  7. 敲诈完微软,又让苹果赔29亿,有文化的流氓真可怕
  8. 计算机重启恢复到推荐分辨率,电脑重启后分辨率变低?Win10分辨率调整
  9. 数据科学 5.1 数据处理(概念)
  10. 鼎沐素食:还生命于自然,还身心于自己!