MyBatis一对多双向关联以及collection的两种形式

一、一对多双向关联

处理has-one关系需要用到association元素,而处理has many关系则需要用到collection元素。例如本例中,假设一名教师可同时指导多名学生,下面就来介绍如何使用collection元素来实现这种映射,具体的任务是查询出教师及其指导的多个学生的信息

为教师实体增加指导学生集合的属性如下:

private List<Student> supStudents;//指导学生

并为其增加setter和getter方法,这里略过。

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">
<!--与以前一样,namespace的值是对应的映射器接口的完整名称-->
<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>

执行类为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
{
//在类路径下寻找resources/beans.xml文件
ctx = new ClassPathXmlApplicationContext("resources/beans.xml");
}
public static void main(String[] args)
{
//从Spring容器中请求映射器
TeacherMapper mapper =
(TeacherMapper)ctx.getBean("teacherMapper");
//查询id为1的教师
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("**********************************************");
}
}
}

二、collection的两种形式

1、嵌套的resultMap

实际上以前的示例使用的就是这种方法,今天介绍它的另一种写法。还是以教师映射为例,修改映射文件TeacherMapper.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">
<!--与以前一样,namespace的值是对应的映射器接口的完整名称-->
<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"/>
<!--需要注意的是,上面的select语句中学生的字段名/别名应与
下面的column属性一致。ofType指collection包含的元素的类型,
此属性不可少-->
<collection property="supStudents"ofType="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本身-->
<association property="supervisor"
resultMap="supervisorResultMap"/>
</collection>
</resultMap>
</mapper>

与以前的写法相比,这种写法的缺点是学生实体映射被嵌入到教师实体映射中,因此学生实体映射不能被重用。

2、嵌套的select语句

这种方式是使用一条单独的select语句来加载关联的实体(在本例中就是学生实体),然后在collection元素中引用此select语句。

首先修改TeacherMapper.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">
<!--与以前一样,namespace的值是对应的映射器接口的完整名称-->
<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>
<!--根据指导教师id查询学生信息-->
<select id="getStudents" parameterType="int"
resultMap="studentResultMap">
select * from student where supervisor_id = #{id}
</select>
</mapper>

从以上可看出,collection的这两种形式与association的两种形式非常相似。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值