[刘阳Java]_MyBatis_实体关系映射_第8讲
MyBatis既然是一个ORM框架,则它也有像Hibernate那样的一对多,多对多,多对一的实体关系映射功能。下面我们就来介绍一下如何使用MyBatis的实体关系映射
1.MyBatis实体关系映射,对于我个人来讲常用的有下面两种
- 多对一:在子表的映射文件中添加association
- 一对多:在父表的映射文件中添加collection
2.MyBatis中多对一的案例
- 先创建两张表
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`) ) ;
- 创建实体类
package com.gxa.pojo; public class Teacher { private intt_id; private String t_name; 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; } } package com.gxa.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; } }
- 创建StudentMapper.xml文件,在此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.gxa.mapper.StudentMapper"> <resultMap type="com.gxa.pojo.Student" id="Student"> <id property="sid" column="sid"/> <result property="sname" column="sname"/> <association property="teacher" javaType="com.gxa.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>
- 完成多对一关系的代码测试
package com.gxa.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.gxa.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.gxa.mapper.StudentMapper.getStudent"; Student student = sqlSession.selectOne(sql); System.out.println(student.getSname() + " === " + student.getTeacher().getT_name()); sqlSession.close(); } }
3.MyBatis一对多的案例
- 修改Student和Teacher这两个实体类
package com.gxa.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.gxa.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; } }
- 创建TeacherMapper的映射文件,在此文件的<resultMap>标签中加入<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.gxa.mapper.TeacherMapper">
<resultMap type="com.gxa.pojo.Teacher" id="Teacher">
<id property="t_id" column="t_id"/>
<result property="t_name" column="t_name"/>
<collection property="student" ofType="com.gxa.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>
- 测试
package com.gxa.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.gxa.pojo.Student; import com.gxa.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.gxa.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.gxa.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(); } }