MyBatis中多对一关系的三种处理方法

server/2024/9/22 16:56:46/

目录

MyBatis中多对一关系的三种处理方法

1.通过级联属性赋值

1)mapper

2)mapper.xml

3)测试代码

4)测试结果

2.通过标签

1)mapper

2)mapper.xml

3)测试代码

4)测试结果

3.分步查询

1)mapper

2)mapper.xml

3)测试代码

4)测试结果

附录

1)ManyToOneMapper

2)ManyToOneMapper.xml

3)ManyToOneMapperTest

4)sql

        studentSql

        classesSql


MyBatis中多对一关系的三种处理方法

1.通过级联属性赋值

1)mapper

/*** 级联属性赋值*/
Student queryStudentAndClasses(int id);

2)mapper.xml

<!--级联属性赋值--><resultMap id="studentByJiLian" type="org.xiji.enty.Student"> <!--id映射--><id property="id" column="id"/><!--学生名字映射--><result property="studentName" column="studentName"/><result property="studentAge" column="studentAge"/><result property="classId" column="classId"/><!--级联赋值--><result property="classes.id" column ="classId"/><result property="classes.className" column="className"/></resultMap><select id="queryStudentAndClasses" resultMap="studentByJiLian">select * from student left join classes on student.classId = classes.id where student.id=#{id}
</select>

设置resultMap之后,使用resultMap接受查询结果,不是通过resultType接受查询结果

解释:

  1. <resultMap> 标签
    1. 定义了一个名为 studentByJiLian 的结果映射,指定其类型为 org.xiji.enty.Student。
  2. <id> 标签
    1. 映射 Student 类的 id 属性到数据库表中的 id 列。
  3. <result> 标签
    1. 映射 Student 类的 studentName、studentAge 和 classId 属性分别到数据库表中的 studentName、studentAge 和 classId 列。
  4. 级联属性映射
    1. classes 是 Student 类的一个属性,表示关联的班级信息。
    2. <result property="classes.id" column="classId"/> 映射 classes 对象的 id 属性到数据库表中的 classId 列。
    3. <result property="classes.className" column="className"/> 映射 classes 对象的 className 属性到数据库表中的 className 列。


 

3)测试代码

/*** 级联属性赋值*/
@Test
public void testManyToOne(){Student student = manyToOneMapper.queryStudentAndClasses(1);System.out.println(student.toString());}

4)测试结果

2.通过<association>标签

1)mapper

/***  association*/
Student queryStudentAndClassesByAssociation(int id);

2)mapper.xml

<!--association赋值-->
<resultMap id="associationByResultMap" type="org.xiji.enty.Student"> <!--id映射--><id property="id" column="id"/> <!--学生名字映射--><result property="studentName" column="studentName"/><result property="studentAge" column="studentAge"/><result property="classId" column="classId"/><!--association--><association property="classes" javaType="org.xiji.enty.Classes"><id property="id" column="classId"/><result property="className" column="className"/></association>
</resultMap><select id="queryStudentAndClassesByAssociation"  resultMap="associationByResultMap">select * from student left join classes on student.classId = classes.id where student.id=#{id}
</select>

设置resultMap之后,使用resultMap接受查询结果,不是通过resultType接受查询结果

解释:

  1. <resultMap> 标签
    1. 定义了一个名为 associationByResultMap 的结果映射,指定其类型为 org.xiji.enty.Student。
  2. <id> 标签
    1. 映射 Student 类的 id 属性到数据库表中的 id 列。
  3. <result> 标签
    1. 映射 Student 类的 studentName、studentAge 和 classId 属性分别到数据库表中的 studentName、studentAge 和 classId 列。
  4. <association> 标签
    1. 映射 Student 类的 classes 属性到 org.xiji.enty.Classes 类型的对象。
    2. 内部包含两个子标签:
      1. <id>:映射 Classes 类的 id 属性到数据库表中的 classId 列。
      2. <result>:映射 Classes 类的 className 属性到数据库表中的 className 列。


 

3)测试代码

/**
 * association*/
@Test
public void testManyToOneByassociation(){Student student = manyToOneMapper.queryStudentAndClassesByAssociation(2);System.out.println(student.toString());System.out.println(student.getClasses().toString());
}

4)测试结果

3.分步查询

1)mapper

/*** 分步查询*/
Student queryStudentAndClassesByStep(int id);

2)mapper.xml

<!--分步查询-->
<resultMap id="stepByResultMap" type="org.xiji.enty.Student"><id property="id" column="id"/><result property="studentName" column="studentName"/><result property="studentAge" column="studentAge"/><result property="classId" column="classId"/><association property="classes" select="queryClassesByStep" column="classId"><id property="id" column="id"/><result property="className" column="className"/></association>
</resultMap><!--第一步-->
<select id="queryStudentAndClassesByStep" resultMap="stepByResultMap" >select * from student where id=#{id}
</select>
<!--第二步-->
<select id="queryClassesByStep" resultType="org.xiji.enty.Classes">select * from classes where id=#{id}
</select>

设置resultMap之后,使用resultMap接受查询结果,不是通过resultType接受查询结果

解释:

  1. <resultMap> 标签
    1. 定义了一个名为 stepByResultMap 的结果映射,指定其类型为 org.xiji.enty.Student。
  2. 基本属性映射
    1. <id>:映射 Student 类的 id 属性到数据库表中的 id 列。
    2. <result>:映射 Student 类的 studentName、studentAge 和 classId 属性分别到数据库表中的 studentName、studentAge 和 classId 列。
  3. <association> 标签
    1. 映射 Student 类的 classes 属性到 org.xiji.enty.Classes 类型的对象。
    2. 使用 select 属性指定一个子查询语句 queryClassesByStep,该查询将根据 classId 获取班级信息。
    3. column 属性指定用于子查询的列名,这里是 classId。

3)测试代码

/*** 分步查询*/
@Test
public void testManyToOneByStep(){Student student = manyToOneMapper.queryStudentAndClassesByStep(3);System.out.println(student.toString());System.out.println(student.getClasses().toString());
}

4)测试结果

附录

1)ManyToOneMapper

java">package org.xiji.mapper;import org.apache.ibatis.annotations.Mapper;
import org.xiji.enty.Student;@Mapper
public interface ManyToOneMapper {/*** 级联属性赋值*/Student queryStudentAndClasses(int id);/***  association*/Student queryStudentAndClassesByAssociation(int id);/*** 分步查询*/Student queryStudentAndClassesByStep(int id);}

2)ManyToOneMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.xiji.mapper.ManyToOneMapper"><!--级联属性赋值--><resultMap id="studentByJiLian" type="org.xiji.enty.Student"><!--id映射--><id property="id" column="id"/><!--学生名字映射--><result property="studentName" column="studentName"/><result property="studentAge" column="studentAge"/><result property="classId" column="classId"/><!----><result property="classes.id" column ="classId"/><result property="classes.className" column="className"/></resultMap><select id="queryStudentAndClasses" resultMap="studentByJiLian">select * from student left join classes on student.classId = classes.id where student.id=#{id}</select><!--association赋值--><resultMap id="associationByResultMap" type="org.xiji.enty.Student"><!--id映射--><id property="id" column="id"/><!--学生名字映射--><result property="studentName" column="studentName"/><result property="studentAge" column="studentAge"/><result property="classId" column="classId"/><!--association--><association property="classes" javaType="org.xiji.enty.Classes"><id property="id" column="classId"/><result property="className" column="className"/></association></resultMap><select id="queryStudentAndClassesByAssociation"  resultMap="associationByResultMap">select * from student left join classes on student.classId = classes.id where student.id=#{id}</select><!--分步查询--><resultMap id="stepByResultMap" type="org.xiji.enty.Student"><id property="id" column="id"/><result property="studentName" column="studentName"/><result property="studentAge" column="studentAge"/><result property="classId" column="classId"/><association property="classes" select="queryClassesByStep" column="classId"><id property="id" column="id"/><result property="className" column="className"/></association></resultMap><!--第一步--><select id="queryStudentAndClassesByStep" resultMap="stepByResultMap" >select * from student where id=#{id}</select><!--第二步--><select id="queryClassesByStep" resultType="org.xiji.enty.Classes">select * from classes where id=#{id}</select></mapper>

3)ManyToOneMapperTest

java">import org.apache.ibatis.annotations.Mapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.xiji.enty.Student;
import org.xiji.mapper.ManyToOneMapper;@SpringJUnitConfig(locations = {"classpath:springConfig.xml"})
public class ManyToOneMapperTest {@Autowiredprivate ManyToOneMapper manyToOneMapper;/*** 级联属性赋值*/@Testpublic void testManyToOne(){Student student = manyToOneMapper.queryStudentAndClasses(1);System.out.println(student.toString());}/*** association*/@Testpublic void testManyToOneByassociation(){Student student = manyToOneMapper.queryStudentAndClassesByAssociation(2);System.out.println(student.toString());System.out.println(student.getClasses().toString());}/*** 分步查询*/@Testpublic void testManyToOneByStep(){Student student = manyToOneMapper.queryStudentAndClassesByStep(3);System.out.println(student.toString());System.out.println(student.getClasses().toString());}}

4)sql

        studentSql

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`  (`id` int NOT NULL AUTO_INCREMENT COMMENT '学生id',`studentName` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '学生姓名',`studentAge` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '学生年龄',`classId` int NULL DEFAULT NULL COMMENT '班级id',PRIMARY KEY (`id`) USING BTREE,INDEX `classId`(`classId` ASC) USING BTREE,CONSTRAINT `classId` FOREIGN KEY (`classId`) REFERENCES `classes` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (1, '张三', '18', 1);
INSERT INTO `student` VALUES (2, '李四', '20 ', 1);
INSERT INTO `student` VALUES (3, '小久', '21', 1);
INSERT INTO `student` VALUES (4, 'xiji', '22', 1);SET FOREIGN_KEY_CHECKS = 1;

        classesSql

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;-- ----------------------------
-- Table structure for classes
-- ----------------------------
DROP TABLE IF EXISTS `classes`;
CREATE TABLE `classes`  (`id` int NOT NULL AUTO_INCREMENT COMMENT '班级id',`className` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '班级名称',PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of classes
-- ----------------------------
INSERT INTO `classes` VALUES (1, '一班');
INSERT INTO `classes` VALUES (2, '二班');
INSERT INTO `classes` VALUES (3, '三班');
INSERT INTO `classes` VALUES (5, '五班');SET FOREIGN_KEY_CHECKS = 1;


http://www.ppmy.cn/server/117519.html

相关文章

PPT技巧:如何在幻灯片中生成目录?

PPT文件如何制作目录&#xff0c;如何点击目录标题立即跳转到相应幻灯片&#xff1f;今天小奥超人和大家一起来学习一下。 现在幻灯片里制作好目录页&#xff0c;制作好目录之后&#xff0c;选中一个目录&#xff0c;点击插入 – 链接 在插入链接界面中&#xff0c;选择【本文…

一个用于翻译 CSV 文件的 Python 脚本,适用于将英文内容批量翻译成中文(或其他语言),并解决文件编码导致的中文乱码和无法翻译的问题。

将CSV文件中的英文批量翻译成中文的 Python 脚本 一个用于翻译 CSV 文件的 Python 脚本&#xff0c;适用于将英文内容批量翻译成中文&#xff08;或其他语言&#xff09;&#xff0c;并解决文件编码导致的中文乱码和无法翻译的问题。 主要功能&#xff1a; 文件编码转换&…

pycharm调试知识, 线程进程与深度学习

pycharm调试&#xff0c; 在调试&#xff08;debugging&#xff09;过程中&#xff0c;Step Over 是一个常见的操作&#xff0c;用来控制代码的执行方式。它的意思是让调试器执行当前行的代码&#xff0c;但是如果这行代码中有函数调用&#xff0c;调试器不会进入该函数的内部…

【自动化测试】自动化测试的价值和误区以及如何高效实用地落地自动化测试

引言 自动化并不仅仅是机器“点点点”的操作。虽然从表面上看&#xff0c;自动化测试确实涉及到了机器按照预设的脚本进行一系列的操作&#xff0c;但它的内涵远比这要丰富得多 高效实用地落地自动化测试&#xff0c;需要从策略规划、工具选择、团队协作、持续改进等多个方面进…

如何将镜像推送到docker hub

前言 这一篇应该是最近最后一篇关于docker的博客了&#xff0c;咱来个有始有终&#xff0c;将最后一步——上传镜像给他写完&#xff0c;废话不多说&#xff0c;直接进入正题。 登录 首先需要确保登录才能推送到你的仓库中去&#xff0c;在终端输入docker login,输入用户名和…

idea激活页面怎么打开

打开Help------选择Register 然后就可以选择激活方式了

【干货分享】Ftrans安全数据交换系统 搭建跨网数据传输通道

安全数据交换系统是一种专门设计用于在不同的网络、系统或组织之间安全地传输数据的软件或硬件解决方案。这种系统通常包含多种安全特性&#xff0c;以确保数据在传输过程中的保密性、完整性和可用性。 安全数据交换系统可以解决哪些问题&#xff1f; 安全数据交换系统主要解…

术语“in law”(在分布上)

在概率论和统计学中&#xff0c;术语“in law”&#xff08;在分布上&#xff09;指的是随机变量的分布收敛到某个目标分布的情况。下面是对这个概念及其在定理中的应用的详细解释 “In Law”&#xff08;在分布上&#xff09;的含义 定义&#xff1a; 如果 { Y n } \{Y_n\} …