mybatis一对一,一对多,XML配置

MyBatis一对多关联查询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/HaHa_Sir/article/details/88095198

发布了56 篇原创文章 · 获赞 5 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_37924509/article/details/102958237