MyBatis多表查询(一对一,一对多,多对多)
1、一对一
关键字:association
作用:针对pojo对象属性的映射
property:pojo的属性名
javaType:pojo类名
(1) 嵌套结果: 使用嵌套结果映射来处理重复的联合结果的子集
<resultMap type="com.gec.domain.Person" id="basePersonResultMap"><id column="id" property="personId"/><result column="name" property="name"/><result column="sex" property="sex"/><result column="age" property="age"/><association property="card" javaType="com.gec.domain.Card"><!-- 1、构造器注入<constructor><idArg column="id" javaType="int"/><arg column="code" javaType="string"/></constructor> --><!-- 2、setter注入 --><result column="id" property="cardId"/><result column="code" property="code"/></association>
</resultMap>
<select id="queryUserList" resultMap="basePersonResultMap">select p.id as personId,p.name,p.sex,p.age,c.*from tbl_person p,tbl_card c where p.card_id=c.id;
</select>
(2) 嵌套查询:通过执行另外一个SQL映射语句来返回预期的复杂类型
column=“引入执行另外定制sql方法的参数值(外键)”
select=“执行定制sql方法名”
PersonMapper2.xml
<resultMap type="com.gec.domain.Person" id="basePersonResultMap"><id column="id" property="personId"/><result column="name" property="name"/><result column="sex" property="sex"/><result column="age" property="age"/><association property="card"javaType="com.gec.domain.Card"column="card_id"select="com.gec.mapper.CardMapper.queryCardById"></association></resultMap><select id="queryUserList" resultMap="basePersonResultMap">
select * from tbl_person;</select>
<resultMap type="com.gec.domain.Card" id="baseCardResultMap"><id column="card_id" property="cardId"/><result column="code" property="code"/></resultMap><select id="queryCardById" resultMap="baseCardResultMap">select c.id as card_id,c.code from tbl_card c where c.id=#{id};
</select><resultMap type="com.gec.domain.Card" id="queryCardResultMap" extends="baseCardResultMap"><association property="person" javaType="com.gec.domain.Person"><id column="card_id" property="personId"/><result column="name" property="name"/><result column="sex" property="sex"/><result column="age" property="age"/></association></resultMap><select id="queryCardList" resultMap="queryCardResultMap">SELECT c.id AS card_id, c.code, p.* FROM tbl_card c,tbl_person p WHERE c.id=p.card_id;</select>
多对一
<!--
按结果查询
实现了select student.id,student.name,teacher.name from student,teacher where student.tid=teacher.id
-->
<select id="getStudentList2" resultMap="StudentTeacher2">select student.id,student.name,teacher.name as tname from student,teacher where student.tid=teacher.id
</select>
<resultMap id="StudentTeacher2" type="com.tjrac.pojo.Student"><result property="id" column="id"/><result property="name" column="name"/><association property="teacher" javaType="com.tjrac.pojo.Teacher"><result property="name" column="tname"/></association>
</resultMap><!--==========================================================-->
<!--
按嵌套查询
实现了select student.id,student.name,teacher.name from student,teacher where student.tid=teacher.id
-->
<select id="getStudentList" resultMap="StudentTeacher">select * from student
</select>
<select id="getTeacherList" resultType="com.tjrac.pojo.Teacher">select * from teacher where id=#{id}
</select>
<resultMap id="StudentTeacher" type="com.tjrac.pojo.Student" ><result property="id" column="id"/><result property="name" column="name"/><association property="teacher" column="tid" javaType="com.tjrac.pojo.Teacher" select="getTeacherList"/>
</resultMap>
多对多
商品表、订单表之间就是以多对多关联
商品与订单的关系表
描述多对多的数据表实现
(1)商品pojo:
public class Article implements Serializable {private Integer articleId;private String name;private Double price;private String remark;private List<Order> orders;省略setter/gettera方法
}
(2)商品表映射:
ArticleMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.gec.mapper.ArticleMapper"> <resultMap type="article" id="baseArticleResultMap"> <id column="id" property="articleId"/> <result column="NAME" property="name"/> <result column="price" property="price"/> <result column="remark" property="remark"/> </resultMap> <resultMap type="article" id="findArtcleByIdResultMap" extends="baseArticleResultMap"> <collection property="orders" javaType="ArrayList" ofType="com.gec.domain.Article" column="id" select="com.gec.mapper.OrderMapper.findOrderByArticleId"> </collection> </resultMap> <!-- 根据订单id查询商品 --> <select id="findArtcleByOrderId" resultMap="baseArticleResultMap"> select * from tb_article where id in (select article_id from tb_item where order_id=#{id}) </select> <select id="findArtcleById" resultMap="findArtcleByIdResultMap"> select * from tb_article where id=#{id} </select>
</mapper>
MyBatis多表查询(一对一,一对多,多对多)相关推荐
- 7. MyBatis多表查询 - 一对一 - 一对多 - 多对多
7. MyBatis多表查询 - 一对一 - 一对多 - 多对多 前言 在前面的篇章,我们已经熟悉了单表查询,下面我们来看看如何进行 多表查询. 数据准备 create database if not ...
- mybatis 多表查询 一对一 一对多查询
本文举例: 1.订单信息表 2.订单详情表 3.发票表 三表关系: 订单信息表 订单信息详情表 1:n 订单信息表 发票表 1:1 需求: 查询订单表订单详情表和发票表所有信息: <!-- 订单 ...
- mybatis多表查询(一对多,多对一,多对多)
mybatis多表查询.多对一,一对多,多对多 多对一.一对多 准备阶段 建立dept实体类和emp实体类 建立Dao接口 写Dao的Mapper映射 多对多 多对一.一对多 准备阶段 建立一个部门表 ...
- Mybatis—多表查询
Mybatis多表查询 一对一查询 一对一查询的模型MapperScannerConfigurer 用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户 创建Order和User实体 ...
- java day55【 Mybatis 连接池与事务深入 、 Mybatis 的动态 SQL 语句、 Mybatis 多表查询之一对多 、 Mybatis 多表查询之多对多】...
第1章 Mybatis 连接池与事务深入 1.1 Mybatis 的连接池技术 1.1.1 Mybatis 连接池的分类 1.1.2 Mybatis 中数据源的配置 1.1.3 Mybatis 中 D ...
- mybatis的一对一 一对多 多对多
mybatis的一对一 一对多 多对多 1.表 2.建表语句 order_t表 CREATE TABLE `order_t` ( `id` int(11) NOT NULL, `user_id` in ...
- Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作
Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作,单表查询,多表查询 一丶表与表之间的关系 背景: 由于如果只使用一张表存储所有的数据,就会操作数 ...
- Mybatis多表查询之一对多、多对一
目录 前言 一.建立数据库 二.多对一查询 2.1 编写实体类 2.2 编写接口 2.3 编写Mapper配置文件 (StudentMapper.xml) 2.4 多对一查询结果 三.一对多查询 3. ...
- MyBatis多表查询之XML和注解实现(resultMap结果集映射配置数据库字段与实体类属性一一映射)
MyBatis多表查询 多表模型分类 一对一:在任意一方建立外键,关联对方的主键. 一对多:在多的一方建立外键,关联一的一方的主键. 多对多:借助中间表,中间表至少两个字段,分别关联两张表的主键. 数 ...
- JAVA日记之mybatis-3一对一,一对多,多对多xml与注解配置 ----喝最烈的酒.
1.Mybatis多表查询 1.1 一对一查询 1.1.1 一对一查询的模型 用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户 一对一查询的需求:查询一个订单,与此同时查询出该订单 ...
最新文章
- FFmpeg简易播放器的实现5-音视频同步
- JavaWeb(一)——web服务器、Tomcat安装和配置
- Viewpager无限循环(首页与尾页平滑过渡)
- 计算机做游戏到大学要学什么,大学学什么专业,毕业才能从事电竞行业?
- test1 exam3编程计算图形的面积
- 怎样做好一个项目经理
- 美国:DFA(Dimensional fund advisors LP)基金介绍
- 打印机服务器启用后自动关闭,打印机print spooler服务启动后总是自动停止的解决方法(没测试)...
- 华为android解锁,华为bootloader怎么解锁 华为手机bootloader解锁教程
- matlab声明全局变量 global
- SQL Server中查询累计和与滑动平均值
- 小米和联想的“骁龙”之争,首发第一,友谊第二
- svn基础学习之常用知识
- MIT推出3D全息图生成新方法,可在智能手机上实时运行
- 77、自动喷水灭火系统的巡查内容
- 立即执行函数(IIFE)的定义及用法
- 华为手机android9隐藏游戏的方法,我的华为Mate10pro为什么玩不了安卓隐藏小游戏?...
- 量化分析师的Python日记【Q Quant兵器谱之函数插值】
- 人月神话 四十周年_14个神话般的节日礼物
- 钱诚10.26黄金原油行情策略分析及白银TD日内短线操作建议
热门文章
- [bzoj 1398] Vijos1382寻找主人 Necklace 解题报告(最小表示法)
- 【bzoj1532】[POI2005]Kos-Dicing 二分+网络流最大流
- Python_套接字、IPv4和简单的客户端/服务器编程
- 每个前端工程师都应该去了解的前端面试题总结(一)
- bind-html自动换行,如何实现textarea placeholder自动换行?
- Dom-to-image
- 4006基于邻接矩阵的顶点的删除(C++,附思路)
- java验证只能输入数字和字母_java:为什么我做的验证只能验证数字和字母不重复,不能验证汉字不重复...
- html5新增标签与删除标签,HTML5新增/删除标签
- python结束后找什么工作_python学习结束后找什么工作?