• 06_一对多联表查询


    1 需求

    • 一个班级中有多个学生
    • 根据classId查询对应的班级信息,包括学生信息和老师信息

    2 创建表和数据

    1. CREATE TABLE student(
    2. s_id INT PRIMARY KEY AUTO_INCREMENT,
    3. s_name VARCHAR(20),
    4. class_id INT
    5. );
    6. INSERT INTO student(s_name, class_id) VALUES('xs_A', 1);
    7. INSERT INTO student(s_name, class_id) VALUES('xs_B', 1);
    8. INSERT INTO student(s_name, class_id) VALUES('xs_C', 1);
    9. INSERT INTO student(s_name, class_id) VALUES('xs_D', 2);
    10. INSERT INTO student(s_name, class_id) VALUES('xs_E', 2);
    11. INSERT INTO student(s_name, class_id) VALUES('xs_F', 2);

    3 定义实体类

    • Student
    1. public class Student {
    2. private int id;
    3. private String name;
    4. }
    • 修改Classes类,添加一个学生集合属性
    1. public class Classes {
    2. private int id;
    3. private String name;
    4. private Teacher teacher;
    5. private List<Student> students;
    6. }

    4 定义SQL映射文件classMapper.xml

    4.1 方式一:嵌套结果,也就是连表查询

    • xml
    1. <!-- 方式一:嵌套结果 ,连表查询
    2. SELECT * FROM class c, teacher t,student s WHERE c.teacher_id=t.t_id AND c.C_id=s.class_id AND c.c_id=1
    3. -->
    4. <resultMap type="Classes" id="ClassResultMap3">
    5. <id property="id" column="c_id"/>
    6. <result property="name" column="c_name"/>
    7. <association property="teacher" column="teacher_id" javaType="Teacher">
    8. <id property="id" column="t_id"/>
    9. <result property="name" column="t_name"/>
    10. </association>
    11. <!-- ofType指定students集合中的对象类型 -->
    12. <collection property="students" ofType="Student">
    13. <id property="id" column="s_id"/>
    14. <result property="name" column="s_name"/>
    15. </collection>
    16. </resultMap>
    17. <select id="getClass3" parameterType="int" resultMap="ClassResultMap3">
    18. select * from class c, teacher t,student s where c.teacher_id=t.t_id and c.C_id=s.class_id and c.c_id=#{id}
    19. </select>
    • 测试
    1. @Test
    2. public void testOM() {
    3. SqlSessionFactory factory = MybatisUtils.getFactory();
    4. SqlSession sqlSession = factory.openSession();
    5. Classes c = sqlSession.selectOne("cn.imentors.mybatis.test6.classMapper.getClass3", 1);
    6. System.out.println(c);
    7. }

    4.2 方式二,嵌套查询:通过执行另外一个SQL映射语句来返回预期的复杂类型

    • xml
    1. <!--
    2. 方式二:嵌套查询:通过执行另外一个SQL映射语句来返回预期的复杂类型
    3. SELECT * FROM class WHERE c_id=1;
    4. SELECT * FROM teacher WHERE t_id=1 //1 是上一个查询得到的teacher_id的值
    5. SELECT * FROM student WHERE class_id=1 //1是第一个查询得到的c_id字段的值
    6. -->
    7. <select id="getClass4" parameterType="int" resultMap="ClassResultMap4">
    8. select * from class where c_id=#{id}
    9. </select>
    10. <resultMap type="Classes" id="ClassResultMap4">
    11. <id property="id" column="c_id"/>
    12. <result property="name" column="c_name"/>
    13. <association property="teacher" column="teacher_id" javaType="Teacher" select="getTeacher2"></association>
    14. <collection property="students" ofType="Student" column="c_id" select="getStudent"></collection>
    15. </resultMap>
    16. <select id="getTeacher2" parameterType="int" resultType="Teacher">
    17. SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id}
    18. </select>
    19. <select id="getStudent" parameterType="int" resultType="Student">
    20. SELECT s_id id, s_name name FROM student WHERE class_id=#{id}
    21. </select>
    测试
    1. @Test
    2. public void testOM2() {
    3. SqlSessionFactory factory = MybatisUtils.getFactory();
    4. SqlSession sqlSession = factory.openSession();
    5. Classes c = sqlSession.selectOne("cn.imentors.mybatis.test6.classMapper.getClass4", 1);
    6. System.out.println(c);
    7. }

    5 属性说明

    • collection:做一对多关联查询
    • ofType:指定集合中元素对象的类型

    捐赠我们
        良师益友工作室一直在致力于帮助编程爱好更加快速方便地学习编程,如果您对我们的成果表示认同并且觉得对你有所帮助,欢迎您对我们捐赠^_^。
        
  • 相关阅读:
    K8S学习笔记之k8s使用ceph实现动态持久化存储
    Linux学习笔记之Centos7 自定义systemctl服务脚本
    ELK学习笔记之Kibana权限控制和集群监控
    ELK学习笔记之ELK6.0 X-pack设置用户名和密码
    Linux学习笔记之rsync配置
    K8S学习笔记之Pod的Volume emptyDir和hostPath
    不小心删除了某个包的一部分,重新安装方法
    串口数据解析总结
    CMakeLists 链接库相关指令
    google 开源项目阅读计划
  • 原文地址:https://www.cnblogs.com/imentors/p/4809795.html
Copyright © 2020-2023  润新知