多表连接的两种方式(数据库逻辑模型):

1.一对一关系

2.一对多关系

一、通过 resultMap 和 association 实现一对一关系

在 mapper.xml 文件里面的代码:

 <resultMap type="com.pojo.TRecruitment" id="tRecruitmentCollegeResultMap">
<id property="id" column="id" />
<result property="title" column="title" />
<result property="litimg" column="litimg" />
<result property="publishedTime" column="published_time" />
<result property="author" column="author" />
<result property="collegeId" column="college_id" />
<result property="type" column="type" />
<result property="details" column="details" /> <!-- association :配置一对一属性 -->
<!-- property:实体类中里面的 TCollege 属性名 -->
<!-- javaType:属性类型 -->
<association property="tCollege" javaType="com.pojo.TCollege" >
<!-- id:声明主键,表示 college_id 是关联查询对象的唯一标识-->
<id property="collegeId" column="college_id" />
<result property="collegeName" column="college_name" />
<result property="collegeImg" column="college_img" />
</association>
</resultMap> <!-- 一对一关联,查询订单,订单内部包含用户属性 -->
<select id="querytTRecruitmentResultMap" resultMap="tRecruitmentCollegeResultMap">
SELECT
r.id,
r.title,
r.litimg,
r.published_time,
r.author,
r.type,
r.details,
c.college_name
FROM
`t_recruitment` r
LEFT JOIN `t_college` c ON r.college_id = c.college_id
</select>

在 mapper.java 文件里面写接口:

List<TRecruitment> querytTRecruitmentResultMap();

在对应的实体类中声明另外一个实体类:

二、通过 resultMap 和 collection 实现一对多关系

xml 文件:

<!-- 一个用户,拥有多个订单 -->
<resultMap type="User" id="UserAndOrdersResultMap"> <!-- 先配置 User 的属性 -->
<id column="id" property="id" />
<result column="username" property="username" />
<result column="birthday" property="birthday" />
<result column="sex" property="sex" />
<result column="address" property="address" /> <!-- 再配置 Orders 集合 -->
<collection property="ordersList" ofType="Orders">
<id column="oid" property="id" />
<result column="user_id" property="userId" />
<result column="number" property="number" />
<result column="createtime" property="createtime" />
</collection> </resultMap> <select id="findUserAndOrders" resultMap="UserAndOrdersResultMap">
SELECT u.*, o.`id` oid, o.`number`, o.`createtime`
FROM USER u, orders o
WHERE u.`id` = o.`user_id`;
</select>

原文:https://blog.csdn.net/weidong_y/article/details/80557941

MyBatis 多表连接查询的更多相关文章

  1. MyBatis数据持久化(七)多表连接查询

    本节继续以多表连接查询的案例介绍使用resultMap的好处,对于两张以上的表进行关联查询,当我们有选择的从不同表查询所需字段时,使用resultMap是相当方便的.例如我们有两张表,分别为用户表Us ...

  2. 三、mybatis多表关联查询和分布查询

    前言 mybatis多表关联查询和懒查询,这篇文章通过一对一和一对多的实例来展示多表查询.不过需要掌握数据输出的这方面的知识.之前整理过了mybatis入门案例和mybatis数据输出,多表查询是在前 ...

  3. MybatisPlus多表连接查询

    一.序言 (一)背景内容 软件应用技术架构中DAO层最常见的选型组件为MyBatis,熟悉MyBatis的朋友都清楚,曾几何时MyBatis是多么的风光,使用XML文件解决了复杂的数据库访问的难题.时 ...

  4. SQL多表连接查询(详细实例)

    转载博客:joeleo博客(http://www.xker.com/page/e2012/0708/117368.html) 本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:stud ...

  5. SQL多表连接查询

    SQL多表连接查询 本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:student  截图如下: 表2:course  截图如下: (此时这样建表只是为了演示连接SQL语句,当然实际 ...

  6. oracle(sql)基础篇系列(二)——多表连接查询、子查询、视图

        多表连接查询 内连接(inner join) 目的:将多张表中能通过链接谓词或者链接运算符连接起来的数据查询出来. 等值连接(join...on(...=...)) --选出雇员的名字和雇员所 ...

  7. Access数据库多表连接查询

    第一次在Access中写多表查询,就按照MS数据库中的写法,结果报语法错,原来Access的多表连接查询是不一样的 表A.B.C,A关联B,B关联C,均用ID键关联 一般写法:select * fro ...

  8. PostgreSQL-join多表连接查询和子查询

    一.多表连接查询 1.连接方式概览 [inner] join 内连接:表A和表B以元组为单位做一个笛卡尔积,记为表C,然后在C中挑选出满足符合on 语句后边的限制条件的内容. left [outer] ...

  9. SQL表连接查询(inner join、full join、left join、right join)

    SQL表连接查询(inner join.full join.left join.right join) 前提条件:假设有两个表,一个是学生表,一个是学生成绩表. 表的数据有: 一.内连接-inner ...

随机推荐

  1. Docker容器(六)——创建docker私有化仓库

    docker私有化仓库是为了节约带宽(外网速度慢或者干脆不能连外网),以及自己定制系统. (1).环境 youxi1 192.168.5.101 docker私有化仓库 youxi2 192.168. ...

  2. iOS开发应该知道的7个编程概念

    对流行工具(如Xcode)和编程概念(如视图控制器)的高级讨论,这些对iOS开发本身很有用. 1. Xcode Xcode是iOS应用开发社区所见过的最通用的IDE.由于集成开发环境来自Apple,它 ...

  3. VC++6.0/VC6使用c99的stdint.h

    如果使用https://github.com/mattn/gntp-send/blob/master/include/msinttypes/stdint.h会报错: error C2733: seco ...

  4. datatables:initComplete和drawCallback比较

    drawCallback: 对表的每个绘制事件执行操作非常有用 - 例如,您可能希望使用新显示的数据更新外部控件,或者启用服务器端处理,您可能希望将事件分配给新创建的元素.此回调旨在实现此目的,并将在 ...

  5. charles安装和使用(转)

    转发链接:https://blog.csdn.net/zhangxiang_1102/article/details/77855548

  6. ObjectARX创建带文字的线型实例代码

    AcDbLinetypeTable* pLinetypeTable=NULL; Acad::ErrorStatus es = acdbHostApplicationServices()->wor ...

  7. node + promise 实现文件读写

    const fs = require('fs'); const promise = new Promise((resolve, reject) => {     fs.open('./c.txt ...

  8. HDU6608-Fansblog(Miller_Rabbin素数判定,威尔逊定理应用,乘法逆元)

    Problem Description Farmer John keeps a website called ‘FansBlog’ .Everyday , there are many people ...

  9. PHP反射API (转)

    http://www.cnblogs.com/zyf-zhaoyafei/p/4922893.html 近期忙着写项目,没有学习什么特别新的东西,所以好长时间没有更新博客.我们的项目用的是lumen, ...

  10. sync包 — 汇总

    sync包 package main; import ( "time" "fmt" ) func main() { //time.Time代表一个纳秒精度的时间 ...