MyBatis中一对多的xml配置方式(嵌套查询/嵌套结果)
作者:CsbLanca 发布时间:2023-11-16 16:34:23
MyBatis一对多的xml配置
用的是window上面的画图板,没法以文字的方式展示出来,见谅
嵌套查询
嵌套结果
一对多关联查询xml配置写法
情景概述
1、有一张客户表 Client ,存储客户信息, 姓名 name ,年龄 age等。
2、有一张客户附件表 client_file ,存储客户附件信息,附件名 name ,路径 path 等,其中通过外键 client_id 关联到 client表,获取附件对应的客户信息。
3、需求如下:
查询某个客户时,获取客户名下的所有附件信息。 (一对多查询)
查询某个附件时,获取附件所属的客户信息。 (一对一 查询)
创建表
1、创建 client 表,并插入数据
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for client
-- ----------------------------
DROP TABLE IF EXISTS `client`;
CREATE TABLE `client` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号',
`name` varchar(255) DEFAULT NULL COMMENT '姓名',
`age` int(255) DEFAULT NULL COMMENT '年龄',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of client
-- ----------------------------
INSERT INTO `client` VALUES ('1', '小明', '18');
INSERT INTO `client` VALUES ('2', '小红', '22');
INSERT INTO `client` VALUES ('3', '小刚', '27');
2、创建 client_file 表,并插入数据
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for client_file
-- ----------------------------
DROP TABLE IF EXISTS `client_file`;
CREATE TABLE `client_file` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号',
`name` varchar(255) DEFAULT NULL COMMENT '附件名',
`path` varchar(255) DEFAULT NULL COMMENT '附件路径',
`client_id` int(11) DEFAULT NULL COMMENT '客户id',
PRIMARY KEY (`id`),
KEY `client_id` (`client_id`),
CONSTRAINT `client_id` FOREIGN KEY (`client_id`) REFERENCES `client` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of client_file
-- ----------------------------
INSERT INTO `client_file` VALUES ('1', '小明电影', '/usr/local/ * 飞.mp4', '1');
INSERT INTO `client_file` VALUES ('2', '小明简历', '/usr/doc/小明.docx', '1');
INSERT INTO `client_file` VALUES ('3', '小明代码', '/usr/code/main.java', '1');
INSERT INTO `client_file` VALUES ('4', '小红靓照', 'E:\\image\\xiaohong.jpg', '2');
3、关系如下:
对应 java Pojo
1、client表对应 Client.java
public class Client2 implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String name;// 姓名
private String age;// 年龄
private List<ClientFile> clientFileList ; // 客户名下的附件 list
// ======= ignore getter,setter ========= //
}
2、client_file表对应 ClientFile.java
public class ClientFile implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String path;// 附件路径
private String clientId;// 客户id
private Client clientInfo ; // 客户信息
// ======= ignore getter,setter ========= //
}
查询 客户表client 获取客户名下的附件信息
1、方法一: 使用一个结果集实现获取client表客户关联的附件信息(错误方法)
1.1、 resultMap 定义:
<resultMap type="Client" id="clientMapError">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<collection property="clientFileList" ofType="ClientFile">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="path" column="path"/>
<result property="clientId" column="client_id"/>
</collection>
</resultMap>
1.2、SQL查询代码:
<select id="getMapError" resultMap="clientMapError" parameterType="Client">
SELECT
a.id , a.name , a.age ,
cf.id, cf.name , cf.path , cf.client_id
FROM client a
LEFT JOIN client_file cf on cf.client_id = a.id
WHERE a.id = #{id}
</select>
1.3、测试:获取客户 id=1的客户名下附件,查询结果如下JSON:
1.4、原因分析:通过查看client_file表,client_id=1,对应有3个附件,而本次查询中只查到了一个附件,且client_file表中id=1的name值为"小明电影"。可以看到 client表和client_file表中有共同字段id,name 在映射的时候将client表中数据映射到 client_file表中去了,解决这一问题,需要将 client_file表中重名字段做别名处理。
2、方法一: 使用一个结果集实现获取client表客户关联的附件信息。(重名字段做别名处理)
2.1、 resultMap 定义:
<resultMap type="Client" id="clientMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<collection property="clientFileList" ofType="ClientFile">
<id property="id" column="fileId"/>
<result property="name" column="fileName"/>
<result property="path" column="path"/>
<result property="clientId" column="client_id"/>
</collection>
</resultMap>
2.2、SQL 查询代码:
<select id="getMap" resultMap="clientMap" parameterType="Client">
SELECT
a.id , a.name , a.age ,
cf.id AS "fileId", cf.name AS "fileName" , cf.path , cf.client_id
FROM client a
LEFT JOIN client_file cf on cf.client_id = a.id
WHERE a.id = #{id}
</select>
2.3、测试:获取客户 id=1 的客户名下附件,查询结果如下JSON:
3、方法二:使用多个结果集实现获取client表客户关联的附件信息。(重名字段不做别名处理)
3.1、resultMap 定义:
<resultMap type="Client" id="clientMap2">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<!-- column: 对应的外键。 client表的id是client_file表的 外键 -->
<collection property="clientFileList" ofType="ClientFile" column="id"
select="getClientFileList"></collection>
</resultMap>
3.2、SQL查询代码:
<select id="getMap2" resultMap="clientMap2" parameterType="Client">
SELECT
a.id , a.name , a.age
FROM client a
WHERE id = #{id}
</select>
<select id="getClientFileList" resultType="ClientFile" parameterType="int">
SELECT
b.id , b.name , b.path , b.client_id
FROM client_file b
WHERE client_id = #{id}
</select>
3.3、获取客户 id=2 的客户名下附件,查询结果如下JSON:
查询 客户附件表 client_file 获取附件所属的客户信息
1、方法一:使用一个结果集实现获取client_file表所属的客户信息(错误方法)
1.1、resultMap 定义:
<resultMap type="ClientFile" id="clientFileMapError">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="path" column="path"/>
<result property="clientId" column="client_id"/>
<association property="clientInfo" javaType="Client">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</association>
</resultMap>
1.2、SQL查询代码:
<select id="getMapError" resultMap="clientFileMapError" parameterType="ClientFile">
SELECT
cf.id , cf.name , cf.path ,cf.client_id
, a.id , a.name , age
FROM client_file cf
LEFT JOIN client a on a.id = cf.client_id
WHERE cf.id = #{id}
</select>
1.3、测试:获取附件表 id=3 的附件所属客户信息,查询结果如下JSON:
1.4、原因分析:通过查看client_file表id=3,对应client_id为1,而查询到的clientInfo对象中id=3,name="小明代码",这里和client表中的数据不对,原因同上----字段对应属性值映射错误。解决这个问题,需要将重名字段进行别名处理。
2、方法一:使用一个结果集实现获取client_file表所属的客户信息。(重名字段别名处理)
2.1、resultMap 定义:
<resultMap type="ClientFile" id="clientFileMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="path" column="path"/>
<result property="clientId" column="client_id"/>
<association property="clientInfo" javaType="Client">
<id property="id" column="c_id"/>
<result property="name" column="clientName"/>
<result property="age" column="age"/>
</association>
</resultMap>
2.2、SQL查询代码:
<select id="getMap" resultMap="clientFileMap" parameterType="ClientFile">
SELECT
cf.id , cf.name , cf.path ,cf.client_id
, a.id AS "c_id" , a.name AS "clientName", age
FROM client_file cf
LEFT JOIN client a on a.id = cf.client_id
WHERE cf.id= #{id}
</select>
2.3、测试:获取附件表 id=2 的附件所属客户信息,查询结果如下JSON:
3、方法二:使用多个结果集实现获取client_file表所属的客户信息(重名字段不处理)
3.1、resultMap 定义:
<resultMap type="ClientFile" id="clientFileMap2">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="path" column="path"/>
<result property="clientId" column="client_id"/>
<association property="clientInfo" column="client_id" select="getClientInfo"></association>
</resultMap>
3.2、SQL查询代码:
<select id="getMap2" resultMap="clientFileMap2" parameterType="ClientFile">
SELECT
cf.id , cf.name , cf.path ,cf.client_id
FROM client_file cf
WHERE cf.id= #{id}
</select>
<select id="getClientInfo" resultType="Client" parameterType="int">
SELECT
id , name ,age
FROM client
WHERE id = #{id}
</select>
3.3、获取附件表 id=4 的附件所属客户信息,查询结果如下JSON:
小结一下
1、MyBatis中一对多关联查询使用 <collection> 标签来实现关联,主要属性作用如下:
property
:java对象名称ofType
:java对象的类型column
:当前表对应的外键字段名称select
:另一个数据集的名称
2、MyBatis中一对一关联查询使用 <association> 标签来实现关联,主要属性作用如下:
property
:java对象名称javaType
:java对象类型colmun
:当前表对应的外键字段名称seelct
:另一个数据集的名称
3、使用一个数据集时,注意关联表之间的字段是否有重名情况,若有重名,需要别名处理。
4、使用多个数据集时,无需关注字段重名情况。
5、一个 resultMap标签可以配置多个<collection>标签和<association>标签。
来源:https://blog.csdn.net/CsbLanca/article/details/89527918


猜你喜欢
- java 中http请求为了防止乱码解决方案今天做一个与地图有关的项目,需要发起http请求地图数据 写了一个工具类,希望大家都能用上吧pa
- 前言C#方法中参数类型有4种参数类型,有时候很难记住它们的不同特征,下图对它们做一个总结大家可能在编码中或多或少的使用过out的ref,但是
- 本文所述为一个C#使用iCSharpcode压缩的使用类,经测试效果不错。分享给大家供大家参考之用。具体方法如下:1.参数类using Sy
- 一、概述1.目标:要在Tank的move()方法做时间代理及日志代理(可以设想以后还要增加很多代理处理),且代理间的顺序可活更换2.思路:(
- 本文实例为大家分享了C++实现连连看游戏的具体代码,供大家参考,具体内容如下这个项目还是挺不错的,运行后也比较有意思,可以看看。#inclu
- 项目中经常会用到分享的功能,有分享链接也有分享图片,其中分享图片有的需要移动端对屏幕内容进行截取分享,说白了就是将view 转成bitmap
- 使用前准备Build.gradle文件配置dependencies配置compile 'com.squareup.retrofit2
- 1:Maven命令下载源码和javadocs当在IDE中使用Maven时如果想要看引用的jar包中类的源码和javadoc需要通过maven
- 本文实例为大家分享了C#支付宝扫码支付示的具体代码,供大家参考,具体内容如下支付宝工具类using System; using System
- 本文的目的是要实现左右滑动的指引效果。那么什么是指引效果呢?现在的应用为了有更好的用户体验,一般会在应用开始显示一些指引帮助页面,使用户能更
- 介绍备忘录模式(Memento Pattern)是一种行为型设计模式,它允许在不破坏封装性的前提下,捕获并保存一个对象的内部状态,并在之后可
- spring Session 提供了一套用于管理用户 session 信息的API和实现。Spring Session为企业级Java应用的
- 前言上一篇博客内容对 RecyclerView 回收复用机制相关源码进行了分析,本博客从自定义 View 三大流程 measure、layo
- 一、什么是抽象工厂模式为创建一组相关或相互依赖的对象提供一个接口,而且无需指定他们的具体类。抽象工厂模式是所有形态的工厂模式中最为抽象和最具
- 本文简单介绍如何引入validation的步骤,如何通过自定义validation减少代码量,提高生产力。特别提及:非基本类型属性的vali
- 前言刚看到这个题目的朋友第一反应肯定是好奇,之后再细细思考下就会发现这个题目眼熟了。就算是同一个答案,如果提问的方式不同,往往会对回答造成干
- 组合模式是一种常见的设计模式(但我感觉有点复杂)也叫合成模式,有时又叫做部分-整体模式,主要是用来描述部分与整体的关系。个人理解:组合模式就
- 开发项目的时候,表很多,是不可能一点点的自己去写xml ,dao文件的,这里就需要用到代码的自动生成工具了。第一步:导入jar包,当然,这之
- 工作中有做过手机App项目,前端和android或ios程序员配合完成整个项目的开发,开发过程中与ios程序配合基本没什么问题,而andro
- RecyclerView 是 android-support-v7-21 版本中新增的一个 Widgets, 还有一个 CardView 会