MyBatis 多表连接查询
多表连接的两种方式(数据库逻辑模型):
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 多表连接查询的更多相关文章
- MyBatis数据持久化(七)多表连接查询
本节继续以多表连接查询的案例介绍使用resultMap的好处,对于两张以上的表进行关联查询,当我们有选择的从不同表查询所需字段时,使用resultMap是相当方便的.例如我们有两张表,分别为用户表Us ...
- 三、mybatis多表关联查询和分布查询
前言 mybatis多表关联查询和懒查询,这篇文章通过一对一和一对多的实例来展示多表查询.不过需要掌握数据输出的这方面的知识.之前整理过了mybatis入门案例和mybatis数据输出,多表查询是在前 ...
- MybatisPlus多表连接查询
一.序言 (一)背景内容 软件应用技术架构中DAO层最常见的选型组件为MyBatis,熟悉MyBatis的朋友都清楚,曾几何时MyBatis是多么的风光,使用XML文件解决了复杂的数据库访问的难题.时 ...
- SQL多表连接查询(详细实例)
转载博客:joeleo博客(http://www.xker.com/page/e2012/0708/117368.html) 本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:stud ...
- SQL多表连接查询
SQL多表连接查询 本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:student 截图如下: 表2:course 截图如下: (此时这样建表只是为了演示连接SQL语句,当然实际 ...
- oracle(sql)基础篇系列(二)——多表连接查询、子查询、视图
多表连接查询 内连接(inner join) 目的:将多张表中能通过链接谓词或者链接运算符连接起来的数据查询出来. 等值连接(join...on(...=...)) --选出雇员的名字和雇员所 ...
- Access数据库多表连接查询
第一次在Access中写多表查询,就按照MS数据库中的写法,结果报语法错,原来Access的多表连接查询是不一样的 表A.B.C,A关联B,B关联C,均用ID键关联 一般写法:select * fro ...
- PostgreSQL-join多表连接查询和子查询
一.多表连接查询 1.连接方式概览 [inner] join 内连接:表A和表B以元组为单位做一个笛卡尔积,记为表C,然后在C中挑选出满足符合on 语句后边的限制条件的内容. left [outer] ...
- SQL表连接查询(inner join、full join、left join、right join)
SQL表连接查询(inner join.full join.left join.right join) 前提条件:假设有两个表,一个是学生表,一个是学生成绩表. 表的数据有: 一.内连接-inner ...
随机推荐
- Python3基础 yield send 变量名= yield i
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- asp.net core mvc 读取appsettings.config中文乱码问题
asp.net core mvc 读取appsettings.config中文乱码问题的解决方法如下: 用记事本打开appsettings.config,另存为的时候,编码设置为 “UTF-8”,
- layui table.reload的bug
bug1: bug描述:当cols列在reload中有变化时,渲染后部分cols列自动隐藏(并未对这些列设置hide:true) bug版本:2.3.5版本有此bug,今日更新最新版本2.5.5 仍有 ...
- LeetCode Top Interview Questions
LeetCode Top Interview Questions https://leetcode.com/problemset/top-interview-questions/ # No. Titl ...
- [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)
原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...
- [LeetCode] 112. Path Sum 路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- 【Linux】多线程同步的四种方式
背景问题:在特定的应用场景下,多线程不进行同步会造成什么问题? 通过多线程模拟多窗口售票为例: #include <iostream> #include<pthread.h> ...
- go 连接到数据库
package main import ( "fmt" _ "github.com/go-sql-driver/mysql" "github.com/ ...
- leetcode最长回文
给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &qu ...
- Qt5 源代码自动跳转
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/nixiaoxianggong/articl ...