MyBatis的关联映射

关联关系概述

在关系型数据库中,多表之间存在着三种关联关系,分别为一对一、一对多和多对多。

  • 一对一:在任意一方引入对方主键作为外键。
  • 一对多:在“多”的一方,,添加“一”的一方的主键作为外键。
  • 多对多:产生中间关系表,引入两张表的主键作为外键,两个主键成为联合主键或使用新的字段作为主键。

对象之间也存在着三种关联关系。

  • 一对一的关系:就是在本类中定义对方类型的对象,如A类中定义B类类型的属性b,B类中定义A类类型的属性a。
  • 一对多的关系:就是一个A类类型对应多个B类类型的情况,需要在A类中以集合的方式引入B类类型的对象,在B类中定义A类类型的属性a。
  • 多对多的关系:在A类中定义B类类型的集合,在B类中定义A类类型的集合。

关联关系

一对一

一个学生只有一本学生证
元素中,包含了一个< association>子元素,MyBatis就是通过该元素来处理一对关联关系的。
< association>元素有如下两种配置方式:

<!--方式一:嵌套查询-->
<association property="card" column="card_id" javaType="com.ssm.po.StudentIdCard"
select="com.ssm.mapper. StudentIdCardMapper.findCodeById"/>
<!--方式二:嵌套结果-->
<association property="card" javaType="com.ssm.po.StudentIdCard">
<id property="id" column=""card_id"/>
<result property="code" column="code"/>
</association>

** StudentIdCardMapper.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.ssm.mapper.StudentIdCardMapper">
	<!--根据id获取学生证信息 -->
	<select id="findStudentIdCardById" parameterType="Integer" resultType="StudentIdCard">
		select * from tb_studentidcard where id=#{id}
	</select>
</mapper>

StudentMapper.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.ssm.mapper.StudentMapper">
	<!--嵌套查询,通过执行另外一条SQL映射语句来返回预期的特殊类型 -->
	<select id="findStudentById" parameterType="Integer"
        resultMap="StudentIdCardWithStudentResult">
		select * from tb_student where id=#{id}
	</select>
	<resultMap type="Student" id="StudentIdCardWithStudentResult">
 	<id property="id" column="id"/>
	    <result property="name" column="name"/>
	    <result property="sex" column="sex"/>
	<!-- 一对一,association使用select属性引入另外一条SQL语句 -->
	<association property="studentIdCard" column="card_id" javaType="StudentIdCard" 
	     select="com.ssm.mapper.StudentIdCardMapper.findStudentIdCardById"/>
	</resultMap>
</mapper>

一对多

一个班级有多个学生
元素中,包含了一个子元素,MyBatis就是通过该元素来处理一对多关联关系的

< collection>元素可以参考如下两种示例进行配置,具体代码如下。

<!--方式一:嵌套查询-->
<collection property="studentList" column="id" ofType="com. ssm.po.Student"
select= "com.ssm.mapper. StudentMapper.selectStudent"/>
<!--方式二:嵌套结果-->
<collection property="studentList" ofType="com. ssm.po.Student">
<id property="id" column="student_id"/>
<result property="username" column="username"/>
</collection>

BanjiMapper.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.ssm.mapper.BanjiMapper">
	<!--一对多:查看某一班级及其关联的学生信息
			注意:当关联查询出的列名相同,则需要使用别名区分  -->
	<select id="findBanjiWithStudent" parameterType="Integer"
		resultMap="BanjiWithStudentResult">
		select b.*,s.id as student_id,s.name 
		from tb_banij b,tb_student s
		 where b.id=s.banji_id and b.id=#{id}
	</select>
	<resultMap type="Banji" id="BanjiWithStudentResult">
		<id property="id" column="id" />
		<result property="name" column="name" />
		<!--一对多关联映射:collection
			ofType表示属性集合中元素的类型List<Student>属性即Student类  -->
		<collection property="studentList" ofType="Student">
			<id property="id" column="student_id" />
			<result property="name" column="name" />
<result property="sex" column="sex" />
		</collection>
	</resultMap>
</mapper>

多对多

学生和课程

CourseMapper.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.ssm.mapper.CourseMapper">
	<!--多对多嵌套查询:通过执行另外一条SQL映射语句来返回预期的特殊类型  -->
	<select id="findCourseWithStudent" parameterType="Integer"
		resultMap="CourseWithStudentResult">
		select * from tb_course where id=#{id}
	</select>
	<resultMap type="Course" id="CourseWithStudentResult">
		<id property="id" column="id" />
		<result property="name" column="name" />
		<result property="code" column="code" />
		<collection property="studentList" column="id" ofType="Student"
 select="com.ssm.mapper.StudentMapper.findStudentById" >
		</collection>
	</resultMap>
</mapper>

StudentMapper.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.ssm.mapper.StudentMapper">
	<select id="findStudentById" parameterType="Integer" resultType="Student">
		select * from tb_student where id in(
		select student_id from tb_electivecourse where course_id=#{id}
		)
	</select>
</mapper>

版权声明:本文为博主原创文章,转载请附上博文链接!
原文地址:https://www.cnblogs.com/zq98/p/13184542.html