(1)根据学号查询学生,(2)根据班级编号查询班级及该班级所有学生,使用查询嵌套方式重新实现实验任务(1)和(2),写出映射代码
时间: 2023-12-29 22:03:12 浏览: 37
查询嵌套方式重新实现实验任务(1)和(2)的映射代码如下:
```xml
<!-- 实验任务1:根据学号查询学生 -->
<select id="selectStudentById" resultMap="studentResultMap">
SELECT * FROM student WHERE id = #{id}
</select>
<!-- 实验任务2:根据班级编号查询班级及该班级所有学生 -->
<select id="selectClassById" resultMap="classResultMap">
SELECT * FROM class WHERE id = #{id}
</select>
<resultMap id="studentResultMap" type="Student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="gender" column="gender"/>
<result property="classId" column="class_id"/>
</resultMap>
<resultMap id="classResultMap" type="Class">
<id property="id" column="id"/>
<result property="name" column="name"/>
<collection property="students" ofType="Student">
<select id="selectStudentsByClassId" resultMap="studentResultMap">
SELECT * FROM student WHERE class_id = #{id}
</select>
</collection>
</resultMap>
```
在第二个映射文件中,我们使用了嵌套查询方式,通过在 Class 对象中添加一个 List<Student> 属性,来实现查询班级及该班级所有学生的需求。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)