联合查询

<!-- 处理关系查询相关的复杂返回数据类型(本例中未曾用到) -->
<resultMap type="SchoolStudent" id="SchoolStudentMap">
  <id property="schoolStudentId" column="schoolStudentId"/>
  <result property="teacher" column="teacher"/>
  <result property="teacherAge" column="teacherAge"/>
  <association property="studentId" column="studentId" javaType="int">
    <id property="studentId" column="studentId"/>
    <result property="name" column="name"/>
    <result property="age" column="age"/>
  </association>
</resultMap>
<select id="selectSchoolStudentJoin" resultMap="SchoolStudentMap">
  select * from schoolStudent join student on schoolStudent.studentId = student.studentId
</select>

id="SchoolStudentMap":调用该resultMap的id

type="SchoolStudent":返回的类型

id property="schoolStudentId":SchoolStudent类中的id主键字段

column="schoolStudentId":schoolstudent表中与schoolStudentId相对应的字段

result property="teacher" column="teacher":SchoolStudent类中其它字段

association property="studentId":与SchoolStudent关联的子查询表的配置,这里的property属性是SchoolStudent类中与子查询类中关联的属性

column="studentId":与SchoolStudent关联的子查询表的配置,这里的column属性是SchoolStudent表中对应property的字段

javaType="int":返回到SchoolStudent类中时的值的类型

association中的其它配置与SchoolStudent主表配置相同,不过是关联表中的属性与对应的字段

构造函数查询

<resultMap id="SchoolStudentConstructor" type="SchoolStudent" >
  <id property="id" column="schoolStudent.id" />
  <result property="teacher" column="teacher" />
  <result property="age" column="age" />
  <association property="student" column="studentId" javaType="Student">
    <constructor>
      <idArg column="studentId" javaType="int"/>
      <arg column="name" javaType="String"/>
      <arg column="zhuangtai" javaType="int"/>
    </constructor>
  </association>
</resultMap>

association property="student":与SchoolStudent关联的子查询表的配置,这里的property属性是SchoolStudent类中的属性(关联表的类对象)

column="studentId":与SchoolStudent关联的子查询表的配置,这里的column属性是SchoolStudent表中对应property的字段,该字段与关联表的某字段关联

javaType="Student":返回到SchoolStudent类中时的值的类型,这里只能是关联表的类对象(别名)

constructor 设置关联表的类中的构造函数的参数。这里传入了多少参数,类中的构造函数中就有多少参数。这里的int对应类中构造函数中数据类型为Integer。值得注意的是,使用这种方式,在类中必须包含一个无参的构造函数;查询的主表与关联表中的字段名不能相同(除关联字段以外)

子查询

联合查询一次查询,占用资源大;子查询N+1次查询,占用资源可大可小。MyBatis通过懒加载的方式处理了子查询N+1次查询的问题。懒加载的原理就是先查询需要返回的主表的类对象,当用到所包含的子类对象时,才会去查询所包含的子类对象

使用懒加载必须要先启动懒加载(将以下代码加入MyBatisConfig.xml文件中:需要放在创建别名之前)

<!-- 启动懒加载 -->
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>

在对应的map.xml中写查询

<resultMap id="SchoolStudentSubMap" type="SchoolStudent" >
  <id property="id" column="schoolStudent.id" />
  <result property="teacher" column="teacher" />
  <result property="age" column="age" />
  <association property="student" column="studentId" javaType="Student" select="getStudentById">
  </association>
</resultMap>
<select id="selectSchoolStudentJoin" resultMap="SchoolStudentSubMap">
  select * from schoolStudent where schoolStudent.studentId in (select studentid from student)
</select>

以上代码中association的select属性是用于调用查询并返回student子表类对象的select

<select id="getStudentById" parameterType="int" resultType="Student">
  select *
  from student where studentId=#{studentId}
</select>

测试与结果

List<SchoolStudent> schoolStudents = sqlSession.selectList("selectSchoolStudentJoin");
//for(SchoolStudent schoolStudent : schoolStudents) {
//  System.out.println(schoolStudent.getTeacher());
//  System.out.println("========================");
//  int id = schoolStudent.getStudent().getStudentId();
//  System.out.println("学生id" + id);
//}
if (schoolStudents != null) {
  System.out.println(schoolStudents.get(0).getTeacher());
  System.out.println("========================");
  int id = schoolStudents.get(0).getStudent().getStudentId();
  System.out.println("学生id" + id);
}

控制台输出

只使用了其中一条数据

2017-12-15 22:58:31,801 [main] DEBUG [/.selectSchoolStudentJoin] - ==>  Preparing: select * from schoolStudent where schoolStudent.studentId in (select studentid from student)
2017-12-15 22:58:31,864 [main] DEBUG [/.selectSchoolStudentJoin] - ==> Parameters:
2017-12-15 22:58:31,947 [main] DEBUG [/.selectSchoolStudentJoin] - <== Total: 2
李四的教师
========================
2017-12-15 22:58:31,949 [main] DEBUG [/.getStudentById] - ==> Preparing: select * from student where studentId=?
2017-12-15 22:58:31,950 [main] DEBUG [/.getStudentById] - ==> Parameters: 2(Integer)
2017-12-15 22:58:31,953 [main] DEBUG [/.getStudentById] - <== Total: 1
学生id2

使用了全部数据(测试中使用for)

2017-12-15 23:09:27,260 [main] DEBUG [/.selectSchoolStudentJoin] - ==>  Preparing: select * from schoolStudent where schoolStudent.studentId in (select studentid from student)
2017-12-15 23:09:27,305 [main] DEBUG [/.selectSchoolStudentJoin] - ==> Parameters:
2017-12-15 23:09:27,377 [main] DEBUG [/.selectSchoolStudentJoin] - <== Total: 2
李四的教师
========================
2017-12-15 23:09:27,378 [main] DEBUG [/.getStudentById] - ==> Preparing: select * from student where studentId=?
2017-12-15 23:09:27,378 [main] DEBUG [/.getStudentById] - ==> Parameters: 2(Integer)
2017-12-15 23:09:27,380 [main] DEBUG [/.getStudentById] - <== Total: 1
学生id2
王五的教师
========================
2017-12-15 23:09:27,381 [main] DEBUG [/.getStudentById] - ==> Preparing: select * from student where studentId=?
2017-12-15 23:09:27,381 [main] DEBUG [/.getStudentById] - ==> Parameters: 3(Integer)
2017-12-15 23:09:27,383 [main] DEBUG [/.getStudentById] - <== Total: 1
学生id3

特别注意:

  • 这里的联合查询,在SchoolStudent表中都包含了关联的子表对象。相关联的表中的字段名除关联字段以外不能有相同的,否则便得不到对应的值。比如SchoolStudent中有id,而Student表中同样有id,那么查询时,得不到id的值

MyBatis---简单关系查询的更多相关文章

  1. MyBatis Generator 生成的example 使用 and or 简单混合查询

    MyBatis Generator 生成的example 使用 and or 简单混合查询 参考博客:https://www.cnblogs.com/kangping/p/6001519.html 简 ...

  2. MyBatis简单的增删改查以及简单的分页查询实现

    MyBatis简单的增删改查以及简单的分页查询实现 <? xml version="1.0" encoding="UTF-8"? > <!DO ...

  3. 使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(三)(错误整理篇)

    使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(一) 使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(二) 以上两篇已经把流 ...

  4. 使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(二)(代码篇)

    这篇是上一篇的延续: 用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(一) 源代码在github上可以下载,地址:https://github.com/guoxia ...

  5. 使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(一)

    梳理下使用spring+springMVC+mybatis 整合后的一个简单实例:输入用户的 ID,之后显示用户的信息(此次由于篇幅问题,会分几次进行说明,此次是工程的创建,逆向生成文件以及这个简单查 ...

  6. mybatis多表查询之多对多关系查询的实现-xml方式

    Mybatis对于多对多关系下的查询提供了集合(collection)的概念来解决,collection属性是resultMap高级结果映射的子集,首先,在本例中我们使用的是集合元素来解决多对多的查询 ...

  7. mybatis的collection查询问题以及使用原生解决方案的结果

    之前在springboot+mybatis环境的坑和sql语句简化技巧的第2点提到,数据库的一对多查询可以一次查询多级数据,并且把拿到的数据按id聚合,使父级表和子级表都有数据. 但是这种查询,必然要 ...

  8. mybatis的嵌套查询(嵌套查询nested select和嵌套结果nested results查询)区别

    (转自:http://blog.csdn.net/canot/article/details/51485955) Mybatis表现关联关系比hibernate简单,没有分那么细致one-to-man ...

  9. MyBatis 示例-联合查询

    简介 MyBatis 提供了两种联合查询的方式,一种是嵌套查询,一种是嵌套结果.先说结论:在项目中不建议使用嵌套查询,会出现性能问题,可以使用嵌套结果. 测试类:com.yjw.demo.JointQ ...

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

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

随机推荐

  1. 算法练习-寻找最小的k个数

    练习问题来源 https://wizardforcel.gitbooks.io/the-art-of-programming-by-july/content/02.01.html 要求 输入n个整数, ...

  2. selenium产生的垃圾文件清理

    C:\Users\XXXX\AppData\Local\Temp\anonymous7822503.webdriver-profile. 这个地址就是我的本地临时文件夹中seleniumdriver的 ...

  3. Laravel 5.5 官方推荐 Nginx 配置学习

    Laravel 5.5 版本官方放出了 Nginx 服务器的配置,中文文档:服务器配置 Nginx server { listen 80; server_name example.com; root ...

  4. 微信小程序之怎样识别一个小程序用户

    本节主要是说下怎样识别一个小程序的用户,需要用什么数据来做标识呢: 我们应该都知道判断是不是一个用户大部分都是通过userid来判断,如果这个用户访问的应用发送了一个请求,把userid之类的数据发给 ...

  5. 花3分钟了解下C/C++中的函数可变参简单实现

    1.可变参函数的原理 C/C++函数的参数是存放在栈区的,并且参数的入栈是从参数的右边开始,即最后一个参数先入栈,而第一个参数最后才入栈,所以,根据栈的后进先出性质,函数总能找到第一个参数.所以,可变 ...

  6. 【[SDOI2017]新生舞会】

    题目 好题啊 我们要求的是 \[C=\frac{\sum_{i=1}^na_i}{\sum_{i=1}^nb_i}\] 使得\(C\)最大 显然 \[C\times \sum_{i=1}^nb_i=\ ...

  7. php简单开启gzip压缩方法(zlib.output_compression)

    网上的教程基本是你抄我来我抄他,不外乎加头加尾或者自构函数两种写法.实际上每个php页面都要去加代码——当然也可以include引用,不过总显得略微麻烦   一般而言,页面文件开启gzip压缩以后,其 ...

  8. 系统运行时间悬浮框(demo)

    此项目基于.net framework 4.0 思路: 拖一个定时器控件,每秒执行一次,调用函数查询当前运行时间并更新到label控件. private void Form1_Load(object ...

  9. P1290 【欧几里德的游戏】

    P1290 [欧几里德的游戏] 真·做题全凭感性 从题目中很容易看出 这是一道\(Gcd\)的题 同时又结合了一些略略的博弈论(丢下锅跑真爽 我们看,辗转相减的\(a,b\)一共只有两种情况 \(a- ...

  10. 改Chrome的User Agent,移动版网络

    理论上访问手机版或者iPad等平板电脑版的网络,应该可以剩些流量的,毕竟移动网络是经过优化压缩的,但是PC电脑如果访问移动版的网站呢?我主要使用的浏览器是Chrome,这几天也找了下Chrome下的修 ...