关联映射的一对多

  //查询经理角色   以及  该角色下对应的员工集合
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. Tomcat中不能通过访问自己IP,但可以通过localhost/127.0.0.1访问

    一.问题如下:局域网内,自己机器部署了一个tomcat应用,在本机上可以通过如下方式访问引用.  http://localhost:8080/xxxx http://127.0.0.1:8080/xx ...

  2. JAVA从文本文件(txt)读取一百万条数据保存到数据库

    Java读取大文本文件保存到数据库 1.追求效率 将文件读取到内存,效率比较高,经过测试读取1G左右的文本文件,机器内存消耗达到接近3个G,对内存消耗太大,不建议使用 2.通过调用第三方类库实现 通过 ...

  3. xsy 2412【BZOJ4569】【Scoi2016】萌萌哒

    Description Description 一个长度为n的大数,用S1S2S3...Sn表示,其中Si表示数的第i位,S1是数的最高位,告诉你一些限制条件,每个条件表示为四个数,l1,r1,l2, ...

  4. Python34之模块测试(__name__ == "__main__")

    def c2f(cel): fah = cel * 1.8 + 32 return fah def f2c(fah): cel = (fah -32) / 1.8 return cel def tes ...

  5. python 之 前端开发(HTTP协议、head标签、img标签、a标签、列表标签)

    第十一章前端开发 11.1 HTTP 1.1引入了许多关键性能优化:keepalive连接,请求流水线,chunked编码传输,字节范围请求等 1.keepalive连接: 1.长连接允许HTTP设备 ...

  6. Pythn基础课程笔记day03_学习内容概要及作业讲解

    第三天_学习内容概要 今日内容概要 1.整形 2.布尔类型 3.字符串 内容回顾和补充 内容回顾 利用思维导图,罗列复习自己学习的内容,巩固知识点. xmind 软件 processon 网站 补充 ...

  7. ALV报表——选择屏幕变量赋值

    ABAP选择屏幕变量赋值 运行效果: 代码: *&---------------------------------------------------------------------* ...

  8. Scratch(三)剪刀石头布

    经过上一讲的突击训练,我们从门外汉开始走向编程的深坑,我们今天还要对上一讲的游戏进行加强. 上一个游戏还能演变成什么游戏呢? 我其实知道你们想到的是老hu机什么的,确实,上一个游戏改改可以变成老hu机 ...

  9. 微信小程序的页面跳转==编程式导航传参 和 标签的方法传参==以及如何过去传递过来的参数

    小程序导航传参接收传递过来的参数 在onload中 实例

  10. 理解javascript中的立即执行函数(function(){})()(转)

    原文:https://www.cnblogs.com/yanzp/p/6371292.html