MyBatis實(shí)體關(guān)系映射
MyBatis既然是一個(gè)ORM框架,則它也有像Hibernate那樣的一對(duì)多,多對(duì)多,多對(duì)一的實(shí)體關(guān)系映射功能。下面我們就來介紹一下如何使用MyBatis的實(shí)體關(guān)系映射
1.MyBatis實(shí)體關(guān)系映射,對(duì)于我個(gè)人來講常用的有下面兩種
多對(duì)一:在子表的映射文件中添加association
一對(duì)多:在父表的映射文件中添加collection
2.MyBatis中多對(duì)一的案例
先創(chuàng)建兩張表
CREATE TABLE `student` (
? `sid` int(11) default NULL,
? `sname` varchar(10) default NULL,
? `t_id` int(11) default NULL
) ;
CREATE TABLE `teacher` (
? `t_id` int(11) NOT NULL,
? `t_name` varchar(20) default NULL,
? PRIMARY KEY? (`t_id`)
) ;
創(chuàng)建實(shí)體類
package com.zlt.pojo;
public class Teacher {
? ? private intt_id;
? ? private String t_name;
? ? public intgetT_id() {
? ? ? ? return t_id;
? ? }
? ? public void setT_id(int t_id) {
? ? ? ? this.t_id = t_id;
? ? }
? ? public String getT_name() {
? ? ? ? return t_name;
? ? }
? ? public void setT_name(String t_name) {
? ? ? ? this.t_name = t_name;
? ? }
}
package com.zlt.pojo;
public class Student {
? ? private int sid;
? ? private String sname;
? ? private Teacher teacher;
? ? public int getSid() {
? ? ? ? return sid;
? ? }
? ? public void setSid(intsid) {
? ? ? ? this.sid = sid;
? ? }
? ? public String getSname() {
? ? ? ? return sname;
? ? }
? ? public void setSname(String sname) {
? ? ? ? this.sname = sname;
? ? }
? ? public Teacher getTeacher() {
? ? ? ? return teacher;
? ? }
? ? public void setTeacher(Teacher teacher) {
? ? ? ? this.teacher = teacher;
? ? }
}
創(chuàng)建StudentMapper.xml文件,在此XML文件中配置多對(duì)一的關(guān)系
<?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.zlt.mapper.StudentMapper">
? ? <resultMap type="com.zlt.pojo.Student" id="Student">
? ? ? ? <id property="sid" column="sid"/>
? ? ? ? <result property="sname" column="sname"/>
? ? ? ? <association property="teacher" javaType="com.zlt.pojo.Teacher">
? ? ? ? ? ? <id property="t_id" column="t_id"/>
? ? ? ? ? ? <result property="t_name" column="t_name"/>
? ? ? ? </association>
? ? </resultMap>
? ? <select id="getStudent" resultMap="Student">
? ? ? ? select * from student a, teacher b where a.t_id = b.t_id and sid = 123
? ? </select>
</mapper>
完成多對(duì)一關(guān)系的代碼測(cè)試
package com.zlt.test;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import com.zlt.pojo.Student;
public class Test03 {
? ? private static SqlSessionFactorysqlSessionFactory;
? ? private static Reader reader;
? ? static {
? ? ? ? try {
? ? ? ? ? ? reader = Resources.getResourceAsReader("config.xml");
? ? ? ? ? ? sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? @Test
? ? public void m01() {
? ? ? ? SqlSessionsqlSession = sqlSessionFactory.openSession();
? ? ? ? String sql = "com.zlt.mapper.StudentMapper.getStudent";
? ? ? ? Student student = sqlSession.selectOne(sql);
? ? ? ? System.out.println(student.getSname() + " ===? " + student.getTeacher().getT_name());
? ? ? ? sqlSession.close();
? ? }
}
3.MyBatis一對(duì)多的案例
修改Student和Teacher這兩個(gè)實(shí)體類
package com.zlt.pojo;
public class Student {
? ? private intsid;
? ? private String sname;
? ? private Teacher teacher;
? ? public intgetSid() {
? ? ? ? return sid;
? ? }
? ? public void setSid(intsid) {
? ? ? ? this.sid = sid;
? ? }
? ? public String getSname() {
? ? ? ? return sname;
? ? }
? ? public void setSname(String sname) {
? ? ? ? this.sname = sname;
? ? }
? ? public Teacher getTeacher() {
? ? ? ? return teacher;
? ? }
? ? public void setTeacher(Teacher teacher) {
? ? ? ? this.teacher = teacher;
? ? }
}
package com.zlt.pojo;
import java.util.List;
public class Teacher {
? ? private intt_id;
? ? private String t_name;
? ? private List<Student> student;
? ? public List<Student>getStudent() {
? ? ? ? return student;
? ? }
? ? public void setStudent(List<Student> student) {
? ? ? ? this.student = student;
? ? }
? ? public intgetT_id() {
? ? ? ? return t_id;
? ? }
? ? public void setT_id(intt_id) {
? ? ? ? this.t_id = t_id;
? ? }
? ? public String getT_name() {
? ? ? ? return t_name;
? ? }
? ? public void setT_name(String t_name) {
? ? ? ? this.t_name = t_name;
? ? }
}
創(chuàng)建TeacherMapper的映射文件,在此文件的<resultMap>標(biāo)簽中加入<collection>
<?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.zlt.mapper.TeacherMapper">
? ? <resultMap type="com.zlt.pojo.Teacher" id="Teacher">
? ? ? ? <id property="t_id" column="t_id"/>
? ? ? ? <result property="t_name" column="t_name"/>
? ? ? ? <collection property="student" ofType="com.zlt.pojo.Student">
? ? ? ? ? ? <id property="sid" column="sid"/>
? ? ? ? ? ? <result property="sname" column="sname"/>
? ? ? ? </collection>
? ? </resultMap>
? ? <select id="getTeacher" resultMap="Teacher">
? ? ? ? select * from teacher a,student b where a.t_id = b.t_id and a.t_id = 1
? ? </select>
</mapper>
測(cè)試
package com.zlt.test;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import com.zlt.pojo.Student;
import com.zlt.pojo.Teacher;
public class Test03 {
? ? private static SqlSessionFactorysqlSessionFactory;
? ? private static Reader reader;
? ? static {
? ? ? ? try {
? ? ? ? ? ? reader = Resources.getResourceAsReader("config.xml");
? ? ? ? ? ? sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? @Test
? ? public void m01() {
? ? ? ? SqlSessionsqlSession = sqlSessionFactory.openSession();
? ? ? ? String sql = "com.zlt.mapper.StudentMapper.getStudent";
? ? ? ? Student student = sqlSession.selectOne(sql);
? ? ? ? System.out.println(student.getSname() + " ===? " + student.getTeacher().getT_name());
? ? ? ? sqlSession.close();
? ? }
? ? @Test
? ? public void m02() {
? ? ? ? SqlSessionsqlSession = sqlSessionFactory.openSession();
? ? ? ? String sql = "com.zlt.mapper.TeacherMapper.getTeacher";
? ? ? ? Teacher teacher = sqlSession.selectOne(sql);
? ? ? ? List<Student> student = teacher.getStudent();
? ? ? ? for (Student s : student) {
? ? ? ? ? ? System.out.println(teacher.getT_name() + "====" + s.getSname());
? ? ? ? }
? ? ? ? sqlSession.close();
? ? }
}
原創(chuàng):知了堂Java培訓(xùn)講師——子墨