Mybatis学习——一对多关联表查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis学习——一对多关联表查询相关的知识,希望对你有一定的参考价值。

1.实体类

 1 public class Student { 
 2     private int id;
 3     private String name; 
 4 }
 5 public class Classes { 
 6     private int id;
 7     private String name;
 8     private Teacher teacher; 
 9     private List<Student> students;
10 }

2.映射文件

技术分享
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 <mapper namespace="com.zhengbin.entity.classMapper">
 4     <!-- 方式一: 嵌套结果: 使用嵌套结果映射来处理重复的联合结果的子集 
 5         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 
 6     -->
 7     <select id="getClass3" parameterType="int" resultMap="ClassResultMap3">
 8         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}
 9     </select>
10     <resultMap type="Classes" id="ClassResultMap3">
11         <id property="id" column="c_id" />
12         <result property="name" column="c_name" />
13         <association property="teacher" javaType="Teacher">
14             <id property="id" column="t_id"/>
15             <result property="name" column="t_name"/>
16         </association>
17         <!-- ofType 指定 students 集合中的对象类型 -->
18         <collection property="list" ofType="Student">
19             <id property="id" column="s_id" />
20             <result property="name" column="s_name" />
21         </collection>
22     </resultMap>
23     
24     <!-- 方式二:嵌套查询:通过执行另外一个 SQL 映射语句来返回预期的复杂类型
25         SELECT * FROM class WHERE c_id=1; 
26         SELECT * FROM teacher WHERE t_id=1 
27         //1 是上一个查询得到的 teacher_id 的值 SELECT*FROMstudentWHEREclass_id=1 
28         //1是第一个查询得到的c_id字段的值 
29     -->
30     <select id="getClass4" parameterType="int" resultMap="ClassResultMap4"> 
31         select * from class where c_id=#{id}
32     </select>
33     <resultMap type="Classes" id="ClassResultMap4">
34         <id property="id" column="c_id" />
35         <result property="name" column="c_name" />
36         <association property="teacher" column="teacher_id" javaType="Teacher" select="getTeacher2"></association>
37         <collection property="list" ofType="Student" column="c_id" select="getStudent"></collection>
38     </resultMap>
39     <select id="getTeacher2" parameterType="int" resultType="Teacher"> 
40         SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id}
41     </select>
42     <select id="getStudent" parameterType="int" resultType="Student"> 
43         SELECT s_id id, s_name name FROM student WHERE class_id=#{id}
44     </select>
45 </mapper>
classMapper.xml

3.测试类

技术分享
 1 package com.zhengbin.test;
 2 
 3 import java.util.List;
 4 
 5 import org.apache.ibatis.session.SqlSession;
 6 import org.apache.ibatis.session.SqlSessionFactory;
 7 
 8 import com.zhengbin.entity.Classes;
 9 import com.zhengbin.entity.Student;
10 import com.zhengbin.util.MyBatisUtils;
11 
12 public class Test5 {
13     @org.junit.Test
14     public void getClasses(){
15         SqlSessionFactory sessionFactory = MyBatisUtils.getFactory();
16         // 参数为TRUE,相当于session.commit();
17         SqlSession session = sessionFactory.openSession(true);
18         // 读取映射文件
19         String statement = "com.zhengbin.entity.classMapper" + ".getClass3";
20         /*
21          * 查看本班所有学生的信息,与任课老师的信息
22          * 一个班级对多个学生,一个班级对一个老师
23          * 即 一对多 与 一对一
24          */
25         Classes c = session.selectOne(statement,1);
26         List<Student>list = c.getList();
27         System.out.println(list);
28         System.out.println(c);
29         session.close();
30     }
31     
32     @org.junit.Test
33     public void getClasses1(){
34         SqlSessionFactory sessionFactory = MyBatisUtils.getFactory();
35         // 参数为TRUE,相当于session.commit();
36         SqlSession session = sessionFactory.openSession(true);
37         // 读取映射文件
38         String statement = "com.zhengbin.entity.classMapper" + ".getClass4";
39         Classes c = session.selectOne(statement,1);
40         List<Student> list = c.getList();
41         System.out.println(list);
42         System.out.println(c);
43         session.close();
44     }
45 }
Test.java

 

以上是关于Mybatis学习——一对多关联表查询的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis学习系列关联查询

MyBatis高级篇 - 关联查询(一对多)

mybatis学习笔记(11)-一对多查询

SpringBoot与Mybatis整合(包含generate自动生成代码工具,数据库表一对一,一对多,关联关系中间表的查询)

mybatis学习——多表查询

mybatis学习——多表查询