关联映射的一对多

  //查询经理角色   以及  该角色下对应的员工集合
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. Spring之3:ListableBeanFactory

    ListableBeanFactory在BeanFactory的位置见<Spring源码阅览——BeanFactory体系结构> ListableBeanFactory:同样扩展BeanF ...

  2. Java基础笔试练习(六)

    1.在Java中,一个类可同时定义许多同名的方法,这些方法的形式参数个数.类型或顺序各不相同,传回的值也可以不相同.这种面向对象程序的特性称为? A.隐藏 B.覆盖 C.重载 D.Java不支持此特性 ...

  3. docker 实践十:docker 网络管理

    本篇是关于 docker 网络管理的内容,同时也包含了 docker 网络的高级应用. 注:环境为 CentOS7,docker 19.03. docker 网络基础 docker 网络模型 在 do ...

  4. 网络地址转换(NAT)

    NAT是解决ipv4地址短缺的方案之一 NAT是将位于子网中的主机与外网连通,子网中所有的主机都可以通过路由器的网络地址转换访问外网.对于外网来说该路由器相当于一台完整的主机,子网内所有主机对外网的访 ...

  5. 实现CodeFirst自动数据迁移无需手动执行命令

    本主题假设您掌握了实体框架中 Code First 迁移的基本知识. 借助自动迁移功能,您无需对您所做的每一个更改都在程序包管理器控制台中手动Update-Database . 启用迁移 只需执行一次 ...

  6. java模板模式项目中使用--封装一个http请求工具类

    需要调用http接口的代码继承FundHttpTemplate类,重写getParamData方法,在getParamDate里写调用逻辑. 模板: package com.crb.ocms.fund ...

  7. python路径相关处理

    一.绝对路径 import os path1=os.path.abspath('.') #表示当前所处的文件夹的绝对路径 print(path1) path2=os.path.abspath('..' ...

  8. @Transactional 同一个类中无事务方法a()内部调用有事务方法b()的问题

    https://blog.csdn.net/u010235716/article/details/90171802 1. 事务的4种特性       序号 参数 含义1 原子性(Atomicity) ...

  9. py网络编程学习笔记

    一.异常处理 异常就是程序运行时发生错误的信号(在程序出现错误时,则会产生一个异常,若程序没有处理它,则会抛出该异常,程序的运行也随之终止),在python中,错误触发的异常如下 而错误分为两种: 1 ...

  10. centos 安装 kibana

    因为我本机安装的 elasticsearch 是 6.2.4 版本,所以kibana也要安装对应的 6.2.4 版本 wget https://artifacts.elastic.co/downloa ...