MyBatis一對多collection用法
阿新 • • 發佈:2019-02-12
比如一個班級有多個學生
班級表
CREATE TABLE `class` (
`CLASS_ID` int(11) NOT NULL AUTO_INCREMENT,
`CLASS_NAME` varchar(255) DEFAULT NULL,
`CLASS_YEAR` varchar(255) DEFAULT NULL,
`TEACHER_ID` int(255) DEFAULT NULL,
PRIMARY KEY (`CLASS_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
學生表
CREATE TABLE `student` ( `student_id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `sex` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, `class_id` int(11) DEFAULT NULL, PRIMARY KEY (`student_id`), KEY `class_id` (`class_id`), CONSTRAINT `class_id` FOREIGN KEY (`class_id`) REFERENCES `class` (`CLASS_ID`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
對應的實體類
package com.guahao.entity; import java.util.List; public class ClassEntity { private int classid; private String className; private String classYear; private Teacher teacher; private List<Student> students; public int getClassid() { return classid; } public void setClassid(int classid) { this.classid = classid; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public String getClassYear() { return classYear; } public void setClassYear(String classYear) { this.classYear = classYear; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } @Override public String toString() { return "ClassEntity [classid=" + classid + ", className=" + className + ", classYear=" + classYear + ", teacher=" + teacher + ", students=" + students + "]"; } }
package com.guahao.entity; public class Student { private int studentId; private String name; private String sex; private int age; public int getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [studentId=" + studentId + ", name=" + name + ", sex=" + sex + ", age=" + age + "]"; } }
classDao.xml
聚集元素用來處理“一對多”的關係。需要指定對映的Java實體類的屬性,屬性的javaType(一般為ArrayList);列表中物件的型別ofType(Java實體類);對應的資料庫表的列名稱;(需要注意的是,這裡的查詢student的select語句條件必須是外來鍵class_id)。
<?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.guahao.dao.ClassDao">
<resultMap type="com.guahao.entity.ClassEntity" id="classResultMap">
<id property="classid" column="CLASS_ID" />
<result property="className" column="CLASS_NAME" />
<result property="classYear" column="CLASS_YEAR" />
<association property="teacher" column="TEACHER_ID" select="com.guahao.dao.TeacherDao.getTeacher" />
<collection property="students" column="class_id"
javaType="ArrayList" ofType="com.guahao.entity.Student" select="com.guahao.dao.StudentDao.getStudentByClassId">
</collection>
</resultMap>
<select id="getClass" parameterType="java.lang.Integer" resultMap="classResultMap">
SELECT
*
FROM CLASS CT
WHERE CT.CLASS_ID = #{id};
</select>
</mapper>
studentDao.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.guahao.dao.StudentDao">
<resultMap type="com.guahao.entity.Student" id="ResultMap">
<id property="studentId" column="STUDENT_ID" />
<result property="name" column="NAME" />
<result property="sex" column="sex" />
<result property="age" column="age" />
</resultMap>
<select id="getStudentByClassId" parameterType="java.lang.Integer" resultMap="ResultMap">
SELECT
*
FROM Student CT
WHERE CT.CLASS_ID = #{id};
</select>
</mapper>
測試
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml","classpath:spring-servlet.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class Tt {
@Resource
private ClassDao classDao;
@Resource
private TeacherDao teacherDao;
@Test
public void getClassName(){
ClassEntity classs = classDao.getClass(1);
System.out.println(classs);
}
}
結果
ClassEntity [classid=1, className=一班, classYear=三年級, teacher=Teacher [teacherId=1, teacherName=打發, teacherSex=男], students=[Student [studentId=1, name=張三, sex=男, age=12], Student [studentId=2, name=李四, sex=男, age=13]]]