第一部分 示例
一、为Teacher实体增加相关属性
为教师实体增加指导学生集合的属性如下:
- private List<Student> supStudents;
并为其增加setter和getter方法,这里略过。
二、TeacherMapper接口
为实现教师实体映射,应先创建映射器接口如下:
- package com.abc.mapper;
-
- import com.abc.domain.Teacher;
-
- public interface TeacherMapper {
-
- public Teacher getById(int id);
-
- }
三、映射文件
为教师实体创建的映射文件如下:
- <?xml version="1.0" encoding="utf8"?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-
- <mapper namespace="com.abc.mapper.TeacherMapper">
-
- <!--TeacherMapper接口中getById方法对应的SQL语句。
- 查询教师及其指导的学生的信息。由于教师、学生都有
- id、name、gender等属性,因此给教师的字段都起了别名-->
- <select id="getById" parameterType="int" resultMap="supervisorResultMap">
- select t.id t_id, t.name t_name, t.gender t_gender,
- t.research_area t_research_area, t.title t_title,
- s.id,s.name, s.gender,s.major,s.grade
- from teacher t,student s where t.id=#{id}
- and s.supervisor_id = t.id
- </select>
-
-
- <resultMap id="supervisorResultMap" type="Teacher">
- <id property="id" column="t_id"/>
- <result property="name" column="t_name"/>
- <result property="gender" column="t_gender"/>
- <result property="researchArea" column="t_research_area"/>
- <result property="title" column="t_title"/>
- <!--collection元素映射教师的指导学生集合的属性。resultMap
- 以命名空间名.resultMap的id的形式,引用studentResultMap。
- 需要注意的是,上面的select语句中学生的字段名/别名应与
- studentResultMap中的column属性一致-->
- <collection property="supStudents"
- resultMap="com.abc.mapper.StudentMapper.studentResultMap"/>
- </resultMap>
-
- </mapper>
相应地,学生实体的映射文件如下:
- <?xml version="1.0" encoding="utf8"?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.abc.mapper.StudentMapper">
-
- <resultMap id="studentResultMap" type="Student">
- <id property="id" column="id"/>
- <result property="name" column="name"/>
- <result property="gender" column="gender"/>
- <result property="major" column="major"/>
- <result property="grade" column="grade"/>
- <!--相应地,在此引用supervisorResultMap,亦采用
- 命名空间名.resultMap的id的形式。-->
- <association property="supervisor"
- resultMap="com.abc.mapper.TeacherMapper.supervisorResultMap"/>
- </resultMap>
-
- </mapper>
在工程的src\resources目录下新建子目录mappers,用来统一存放映射文件。为了能让MyBatis找到这些映射文件,修改其核心配置文件configuration.xml中的mappers元素如下:
-
- <mappers>
- <mapper resource="resources/mappers/StudentMapper.xml"/>
- <mapper resource="resources/mappers/TeacherMapper.xml"/>
- </mappers>
注意:resources目录在工程编译前会被复制到classes目录下(详见工程生成文件build.xml中的copy-resources和compile这两个target),而classes目录会被ant添加到classpath中。
四、执行类
执行类为CollectionDemo,其内容如下:
- package com.demo;
-
- import org.springframework.context.ApplicationContext;
- import com.abc.mapper.StudentMapper;
- import com.abc.mapper.TeacherMapper;
- import com.abc.domain.Teacher;
- import com.abc.domain.Student;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import java.util.List;
-
- public class CollectionDemo
- {
- private static ApplicationContext ctx;
-
- static
- {
-
- ctx = new ClassPathXmlApplicationContext("resources/beans.xml");
- }
-
-
- public static void main(String[] args)
- {
-
- TeacherMapper mapper =
- (TeacherMapper)ctx.getBean("teacherMapper");
-
-
- Teacher teacher = mapper.getById(1);
-
- if(teacher == null)
- {
- System.out.println("未找到相关教师信息。");
- }
- else
- {
-
- System.out.println("**********************************************");
- System.out.println("教师姓名:" + " " + teacher.getName());
- System.out.println("教师职称:" + " " + teacher.getTitle());
- System.out.println("**********************************************");
-
- System.out.println("指导学生信息:");
-
- for(Student s : teacher.getSupStudents())
- {
- System.out.println("**********************************************");
- System.out.println( s.getName() + " " + s.getGender() + " " + s.getGrade()
- + " " + s.getMajor());
-
- System.out.println("指导教师研究方向:" + s.getSupervisor().getResearchArea());
-
- }
-
- System.out.println("**********************************************");
- }
-
- }
-
- }
从中可看出,可以从任意一端访问另一端的对象。
第二部分 :上面是比较典型的使用方式,还有其他方式即:嵌套的Select,但是会产生 N+1的问题
(1) association 中嵌套select
- <?xml version="1.0" encoding="utf8"?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.abc.mapper.StudentMapper">
-
- <select id="getById" parameterType="int" resultMap="studentResultMap">
- select id,name,gender,major,grade,supervisor_id from student where id=#{id}
- </select>
-
- <select id="selectSupervisor" parameterType="int" resultMap="supervisorResultMap">
- select id,name,gender,title,research_area
- from teacher where id=#{id}
- </select>
-
- <resultMap id="studentResultMap" type="Student">
- <id property="id" column="id"/>
- <result property="name" column="name"/>
- <result property="gender" column="gender"/>
- <result property="major" column="major"/>
- <result property="grade" column="grade"/>
-
- <!--column="supervisor_id"不能少。此列的值作为参数
- 传递给要引用的select语句,用来查询相应学生的指导教师
- 的信息。select属性指定要引用的select语句-->
- <association property="supervisor" javaType="Teacher"
- column="supervisor_id" select="selectSupervisor"/>
- </resultMap>
-
-
- <resultMap id="supervisorResultMap" type="Teacher">
- <id property="id" column="id"/>
- <result property="name" column="name"/>
- <result property="gender" column="gender"/>
- <result property="researchArea" column="research_area"/>
- <result property="title" column="title"/>
- </resultMap>
-
- </mapper>
(2)collection 中嵌套select
二、嵌套的select语句
- <?xml version="1.0" encoding="utf8"?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-
- <mapper namespace="com.abc.mapper.TeacherMapper">
-
- <!--TeacherMapper接口中getById方法对应的SQL语句。
- 查询教师的信息。-->
- <select id="getById" parameterType="int" resultMap="supervisorResultMap">
- select * from teacher where id=#{id}
- </select>
-
-
- <resultMap id="supervisorResultMap" type="Teacher">
- <id property="id" column="id"/>
- <result property="name" column="name"/>
- <result property="gender" column="gender"/>
- <result property="researchArea" column="research_area"/>
- <result property="title" column="title"/>
- <!--ofType指collection包含的元素的类型,此属性不可少。
- column属性指把上述的getById的select语句中的教师id列的值作为参数
- 传递给将要引用到的下述的getStudents的select语句,此属性不可少。
- 引用的形式为:命名空间.select语句id-->
- <collection property="supStudents" column="id" ofType="Student"
- select="com.abc.mapper.StudentMapper.getStudents"/>
- </resultMap>
-
- </mapper>
在这里把根据指导教师id查询学生信息的SQL语句写在StudentMapper.xml中,并引用其中的学生实体映射studentResultMap。修改StudentMapper.xml如下:
- <?xml version="1.0" encoding="utf8"?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.abc.mapper.StudentMapper">
-
- <resultMap id="studentResultMap" type="Student">
- <id property="id" column="id"/>
- <result property="name" column="name"/>
- <result property="gender" column="gender"/>
- <result property="major" column="major"/>
- <result property="grade" column="grade"/>
- <!--在这里引用supervisorResultMap和getById,亦采用
- 命名空间名.相关元素id的形式。column="supervisor_id"
- 属性不可少-->
- <association property="supervisor"
- resultMap="com.abc.mapper.TeacherMapper.supervisorResultMap"
- select="com.abc.mapper.TeacherMapper.getById" column="supervisor_id"/>
- </resultMap>
-
-
- <select id="getStudents" parameterType="int"
- resultMap="studentResultMap">
- select * from student where supervisor_id = #{id}
- </select>
-
- </mapper>
第三部分 :延迟加载
1、MyBatis的延迟加载机制需要使用cglib包,因此应向工程中添加此包,本文使用的是cglib-nodep-2.2.3.jar。
2、在MyBatis的核心配置文件(在本示例中是configuration.xml)里的settings元素中添加以下配置:
- <setting name="lazyLoadingEnabled" value="true" />
- <setting name="aggressiveLazyLoading" value="false" />
现在来对延迟加载进行测试。首先改造类CollectionDemo.java,让它先遍历查找到的教师,然后遍历某个教师指导的学生的信息
-
- List<Teacher> teachers = mapper.findTeacherByPage(params);
-
- if(teachers == null)
- {
- System.out.println("未找到相关教师信息。");
- }
- else
- {
- Object[] t = teachers.toArray();
- System.out.println("**********************************************");
-
- for(int i = 0; i < t.length; i++)
- {
- teacher = (Teacher)t[i];
- System.out.println("教师姓名:" + " " + teacher.getName());
- System.out.println("教师职称:" + " " + teacher.getTitle());
- System.out.println("**********************************************");
- }
-
-
- for(Student s : teacher.getSupStudents())
- {
- System.out.println( s.getName() + " " + s.getGender () + " " + s.getGrade()
- + " " + s.getMajor());
- }
- }
原文 http://legend2011.blog.51cto.com/3018495/973579