前言

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

一对一关联

<association /> JavaType是用来指定pojo中属性的类型
此处采用两种方式进行关联查询,一种是嵌套结果,一种是嵌套查询。
<!-- 根据班级id查询班级和教师信息 -->
<select id="getClass" resultMap="ClassResultMap">
select *
from class as c
left join
teacher as t on c.teacher_id = t.t_id
where c.c_id
= #{id}
</select> <!-- 嵌套结果 -->
<resultMap type="Classes" id="ClassResultMap">
<id property="id" column="c_id" />
<result property="name" column="c_name" />
<association property="teacher" javaType="Teacher">
<id property="id" column="t_id" />
<result property="name" column="t_name" />
</association>
</resultMap> <select id="getClass2" parameterType="int"
resultMap="ClassResultMap2">
select * from class where c_id=#{id}
</select> <!-- 嵌套查询 -->
<resultMap type="Classes" id="ClassResultMap2">
<id property="id" column="c_id" />
<result property="name" column="c_name" />
<association property="teacher" column="teacher_id"
select="getTeacher" />
</resultMap> <select id="getTeacher" parameterType="int" resultType="Teacher">
SELECT
t_id id, t_name name from teacher where t_id=#{id}
</select>

一对多关联

<collection /> ofType指定的是映射到list集合属性中pojo的类型
<select id="getClass3" parameterType="int"
resultMap="ClassResultMap3">
select *
from class c
left join teacher t on c.teacher_id =
t.t_id
left join student
s on c.c_id = s.class_id
where c.c_id = #{id}
</select> <resultMap type="Classes" id="ClassResultMap3">
<id property="id" column="c_id" />
<result property="name" column="c_name" />
<association property="teacher" column="teacher_id"
javaType="Teacher">
<id property="id" column="t_id" />
<result property="name" column="t_name" />
</association>
<collection property="students" ofType="Student">
<id property="id" column="s_id" />
<result property="name" column="s_name" />
</collection>
</resultMap>

动态SQL

  使用Mybatis的动态SQL特性可以很容易的串联SQL。if choose(when otherwise) foreach trim>。更为细致的介绍不再赘述,Mybatis3官方参考文档中以详细介绍。

    <!-- 用户的条件查询 姓名模糊查询 -->
<select id="selectUserByCondition" parameterType="UserCondition"
resultType="User">
select * from users
where age &lt;= #{maxAge} and age &gt;= #{minAge}
<if test='name!="%null%"'>
and name like #{name}
</if>
</select>

Mybatis之关联查询及动态SQL的更多相关文章

  1. MyBatis学习总结(三)——多表关联查询与动态SQL

    在上一章中我们学习了<MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射>,这一章主要是介绍一对一关联查询.一对多关联查询与动态SQL等内容. 一.多表关联查询 表与 ...

  2. MyBatis的关联映射和动态SQL

    CREATE TABLE tb_card ( id INT PRIMARY KEY AUTO_INCREMENT, CODE ) ); '); CREATE TABLE tb_person ( id ...

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

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

  4. Mybatis之关联查询

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

  5. MyBatis学习 之 三、动态SQL语句

    目录(?)[-] 三动态SQL语句 selectKey 标签 if标签 if where 的条件判断 if set 的更新语句 if trim代替whereset标签 trim代替set choose ...

  6. Mybatis第三篇【动态SQL】

    动态SQL 何为动态SQL??回顾一下我们之前写的SSH项目中,有多条件查询的情况,如下图 我们当时刚开始做的时候,是需要在Controller中判断SQL是否已经有条件了,因为SQL语句需要拼接起来 ...

  7. SpringBoot+Mybatis实现关联查询

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

  8. Mybatis 系列9-强大的动态sql 语句

    [Mybatis 系列10-结合源码解析mybatis 执行流程] [Mybatis 系列9-强大的动态sql 语句] [Mybatis 系列8-结合源码解析select.resultMap的用法] ...

  9. MyBatis的核心配置、动态sql、关联映射(快速总结)

    MyBatis的核心对象和配置 #1. SqlSessionFactory对象: 单个数据库映射关系经过编译的内存镜像: 作用:创建SQLSession对象. //读取配置文件 InputSteam ...

随机推荐

  1. IOS UIWebView(浏览器控件)

    什么是UIWebViewUIWebView是iOS内置的浏览器控件系统自带的Safari浏览器就是通过UIWebView实现的 UIWebView不但能加载远程的网页资源,还能加载绝大部分的常见文件h ...

  2. SSH2整合需要jar包解释

    hibernate3.jar, Hibernate的库,必须使用的jar包 antlr-2.7.6.jar, 语法分析生成器 语言转换工具,hibernate利用它实现HQL到SQL的转换 cglib ...

  3. MySQL数据库----------小知识点

    **********************补2016年5月23日的博客************************* MySQL数据库 常用数据类型: int                整数 ...

  4. HDU 6386 Age of Moyu 【BFS + 优先队列优化】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6386 Age of Moyu Time Limit: 5000/2500 MS (Java/Others ...

  5. Redis(RedisTemplate)使用hash哈希

    RedisTemplate配置:https://www.cnblogs.com/weibanggang/p/10188682.html package com.wbg.springRedis.test ...

  6. spring加载属性(properties)文件

    一.注解方式加载 jdbc.driver=org.mariadb.jdbc.Driver jdbc.url=jdbc:mariadb://localhost:3306/kt jdbc.user=roo ...

  7. HDU 1019 (多个数的最小公倍数)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1019 Least Common Multiple Time Limit: 2000/1000 MS (J ...

  8. java Thread 类 run 和 start 方法区别

    public class ThreadModle { public static void main(String[] args) throws InterruptedException { Thre ...

  9. c#数据库访问服务(综合数据库操作)

    前面给大家说封装了常用的数据库,并且整理了使用.最近我再次把项目整合了.做成比较完善的服务. 还是重复的说下数据库操作封装. berkeley db数据库,Redis数据库,sqlite数据库. 每个 ...

  10. 破损的键盘(悲剧文本)(Broken Keyboard(a.k.a. Beiju Text),Uva 11988)

    破损的键盘(悲剧文本)(Broken Keyboard(a.k.a. Beiju Text),Uva 11988) 题意描述 你在输入文章的时候,键盘上的Home键和End键出了问题,会不定时的按下. ...