关联映射的一对多

  //查询经理角色   以及  该角色下对应的员工集合
public SmbmsRole getRoleAndUser(Integer id); <resultMap id="roleAndUserMapper" type="SmbmsRole">
<id column="rid" property="rid"></id>
<result column="roleName" property="roleName"/>
<!--映射多的一方 property代表实体当中多的一方的属性名 ofType代表集合当中泛型类型-->
<!-- select 代表执行查询的ID column所引用的条件列 -->
<collection property="userList" ofType="SmbmsUser" select="getRoleAndUserMutilSQL" column="rid"> </collection>
</resultMap> <!--<select id="getRoleAndUser" resultMap="roleAndUserMapper">
select u.id,u.userName,u.userRole,r.rid,r.roleName from smbms_user as u,smbms_role as r where u.userRole=r.rid and r.rid=#{id}
</select>--> <select id="getRoleAndUser" resultMap="roleAndUserMapper">
select * from smbms_role where rid=#{id}
</select>
<select id="getRoleAndUserMutilSQL" resultType="SmbmsUser">
select * from smbms_user where userRole=#{rid}
</select>

关联查询的多对一

//查询所有用户信息  包含角色信息
public List<SmbmsUser> getUserList(); <resultMap id="userListAndRole" type="SmbmsUser">
<id column="id" property="id"></id>
<result column="userName" property="userName"/>
<association property="role" javaType="SmbmsRole" select="getRole" column="userRole">
<id column="rid" property="rid"></id>
<result column="roleName" property="roleName"/>
</association>
</resultMap>
<!--<select id="getUserList" resultMap="userListAndRole">
select u.id,u.userName,u.userRole,r.rid,r.roleName from smbms_user as u,smbms_role as r where u.userRole=r.rid
</select>--> <select id="getUserList" resultMap="userListAndRole">
select * from smbms_user
</select>
<select id="getRole" resultType="SmbmsRole">
select * from smbms_role where rid=#{userRole}
</select>

关联查询的多对多

//查询所有学生信息  以及授课教员
public List<Student> getStudentInfo(); <resultMap id="studentAndTeacherMapper" type="Student">
<id column="stuid" property="stuid"/>
<result column="stuname" property="stuname"/>
<collection property="teachers" ofType="Teacher">
<id column="tid" property="tid"></id>
<result property="tname" column="tname"/>
</collection>
</resultMap>
<select id="getStudentInfo" resultMap="studentAndTeacherMapper">
select * from student,teacher,stu_t where student.stuid=stu_t.stuid and teacher.tid=stu_t.tid
</select>

关联映射的自查询

 //查询河南省  下的所有子集
public City getCityAndChildCitys(Integer cid); <resultMap id="CityAndChildCitysMapper" type="City">
<id column="cid" property="cid"></id>
<result column="cname" property="cname"/>
<result column="pid" property="pid"/>
<collection property="childCitys" ofType="City" select="getCityAndChildCitysMutilSQL" column="cid">
<id column="cid" property="cid"></id>
<result column="cname" property="cname"/>
<result column="pid" property="pid"/>
</collection>
</resultMap> <select id="getCityAndChildCitys" resultMap="CityAndChildCitysMapper">
select * from city where cid=#{cid}
</select>
<select id="getCityAndChildCitysMutilSQL" resultMap="CityAndChildCitysMapper">
select * from city where pid=#{cid}
</select>

MyBatis的关联查询的更多相关文章

  1. Mybatis之关联查询

    一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...

  2. SpringBoot+Mybatis实现关联查询

    SpringBoot+Mybatis实现关联查询 今天学习了下Mybatis的动态查询,然后接着上次的Demo改造了下实现表的关联查询. 话不多说,开始今天的小Demo 首先接着上次的项目 https ...

  3. Mybatis之关联查询及动态SQL

    前言 实际开发项目中,很少是针对单表操作,基本都会联查多表进行操作,尤其是出一些报表的内容.此时,就可以使用Mybatis的关联查询还有动态SQL.前几篇文章已经介绍过了怎么调用及相关内容,因此这里只 ...

  4. JavaWeb_(Mybatis框架)关联查询_六

    系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...

  5. mybatis一对一关联查询——(八)

    1.需求 查询所有订单信息,关联查询下单用户信息. 注意: 因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询.如果从用户信息出发查询用户下的订单信息则为一对多查 ...

  6. Mybatis一对一关联查询

    有两张表,老师表teacher和班级表class,一个class班级对应一个teacher,一个teacher对应一个class 需求是根据班级id查询班级信息(带老师的信息) 创建teacher和c ...

  7. MyBatis学习(四)MyBatis一对一关联查询

    一对一关联查询即.两张表通过外键进行关联.从而达到查询外键直接获得两张表的信息.本文基于业务拓展类的方式实现. 项目骨架 配置文件conf.xml和db.properties前几节讲过.这里就不细说了 ...

  8. SSM-MyBatis-15:Mybatis中关联查询(多表操作)

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 先简单提及一下关联查询的分类 1.一对多 1.1单条SQL操作的 1.2多条SQL操作的 2.多对一 2.1单 ...

  9. Mybatis的关联查询(一)

    一对一的关联查询 一.使用resultType进行输出映射   1. 创建一个新的PO类,由于要将查询出来的属性映射到新的PO类.所有该PO类中应该有查询出来的所有列对应的属性. //定义新的PO类, ...

随机推荐

  1. Python 容器用法整理

    本文整理几种基本容器:列表.元组.字典和集合的用法和collections中几种已经预先实现的容器数据结构:namedtuple(),双向链表deque,ChainMap,Counter,Ordere ...

  2. 【MIT 6.824 】分布式系统 课程笔记(一)

    Lecture 02 Infrastructure: RPC & threads 一.多线程挑战 共享数据: 使用互斥信号量.或者避免共享 线程间协作: 使用channels 或者 waitg ...

  3. 图像处理库 Pillow与PIL

    PIL只支持python2的版本到2.7: Python imaging Library : Pillow 是PIL派生的一个分支,支持3以上Python版本. 命令使用pip安装: pip inst ...

  4. PAT(B) 1029 旧键盘(Java)字符串

    题目链接:1029 旧键盘 (20 point(s)) 题目描述 旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及实际被输入的文字,请你列出肯定坏掉的 ...

  5. mysql创建唯一索引UNIQUE INDEX,以及报错“#失败原因: [Execute: Duplicate entry '733186700' for key 'uniq_video_id_index']”

    要给t_video_prods表的video_id字段创建唯一所以,可以使用下面这条语句: alter table t_video_prods add UNIQUE INDEX `uniq_video ...

  6. libhura的建立

    libharu 是一个开源的导出pdf的库,在编译libharu需要用到zlib库和libpng库(libpng 依赖于zlib库).如果项目中不需要导出带有图片的pdf,可以将涉及到"pn ...

  7. python 实现 websocket

    一.websocket概要: websocket是基于TCP传输层协议实现的一种标准协议(关于网络协议,可以看看文末的图片),用于在客户端和服务端双向传输数据 传统的客户端想要知道服务端处理进度有两个 ...

  8. git 如何取消add操作

    可以直接使用命令    git reset HEAD 这个是整体回到上次一次操作 绿字变红字(撤销add) 如果是某个文件回滚到上一次操作:  git reset HEAD  文件名 红字变无 (撤销 ...

  9. C#使用消息队列(MSMQ)

    最近项目用到消息队列,找资料学习了下.把学习的结果 分享出来 首先说一下,消息队列 (MSMQ Microsoft Message Queuing)是MS提供的服务,也就是Windows操作系统的功能 ...

  10. WinForm 无焦点获取键盘输入

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...