假设一个班级有多名学生为例,通过班级号查询出该班级的信息,和班级里面的所有学生的信息,一般的做法就是通过班级号把班级的信息查询出来,再通过班级ID号把该班级里面的所有学生查询出来,我们不用这种通用的方法
1.班级实体类可以定义为这样:
import java.util.List;
public class ClazzEntity {
private int clazzID;
private String clazzName;
private List<StudentEntity> studentList;
public int getClassID() {
return clazzID;
}
public int getClazzID() {
return clazzID;
}
public void setClazzID(int clazzID) {
this.clazzID = clazzID;
}
public String getClazzName() {
return clazzName;
}
public void setClazzName(String clazzName) {
this.clazzName = clazzName;
}
public List<StudentEntity> getStudentList() {
return studentList;
}
public void setStudentList(List<StudentEntity> studentList) {
this.studentList = studentList;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
学生实体类定义:
package com.cn.hnust.pojo;
public class StudentEntity {
private int stuID;
private String stuName;
private int stuAge;
private String stuAddress;
public int getStuID() {
return stuID;
}
public void setStuID(int stuID) {
this.stuID = stuID;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public String getStuAddress() {
return stuAddress;
}
public void setStuAddress(String stuAddress) {
this.stuAddress = stuAddress;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
2.数据库建表语句:
CREATE TABLE student_t
(
stuno INT PRIMARY KEY,
stuname VARCHAR(20),
stuage INT,
stuaddress VARCHAR(20) ,
classid INT
);
CREATE TABLE class_t
(
classid INT PRIMARY KEY,
classname VARCHAR(20)
);
插入数据记录如图所示:
3.查询ClazzEntity中的学生信息列表StudentEntity,通过mybatis中的collection标签来配置,其中,ofType是查询返回的学生信息对应的实体类,select为要执行的查询学生列表的查询语句,mybatis的xml配置文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.cn.hnust.dao.InfoManageDao" >
<resultMap id="ClazzResultMap" type="com.cn.hnust.pojo.ClazzEntity" >
<id column="classID" property="clazzID" jdbcType="INTEGER" />
<result column="className" property="clazzName" jdbcType="VARCHAR" />
<collection property="studentList" column="classID" javaType="ArrayList"
ofType="com.cn.hnust.pojo.StudentEntity" select="getStudentByClassID"/>
</resultMap>
<resultMap id="StudentResultMap" type="com.cn.hnust.pojo.StudentEntity">
<id property="stuID" column="stuID" />
<result property="stuName" column="stuName" />
<result property="stuAge" column="stuAge" />
<result property="stuAddress" column="stuAddress" />
</resultMap>
<select id="getClassByID" resultMap="ClazzResultMap" parameterType="java.lang.Integer" >
select classID,className
from class_t
where classID = #{clazzID}
</select>
<select id="getStudentByClassID" resultMap="StudentResultMap" parameterType="java.lang.Integer" >
select stuID,stuName,stuAge,stuAddress,classID
from student_t
where classID = #{clazzID}
</select>
</mapper>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
这样就可以查到一个班级的信息,和班级里面的所有学生信息:
ClazzEntity [clazzID=1, clazzName=junior, studentList=[StudentEntity [stuID=1001, stuName=wanghai, stuAge=18, stuAddress=beijing], StudentEntity [stuID=1002, stuName=zhangdong, stuAge=20, stuAddress=shanghai]]]