1 一对一映射
比如每位学生有一个地址。
public class Address { private Integer addrId; private String street; private String city; private String state; private String zip; private String country; // setters & getters } public class Student { private Integer studId; private String name; private String email; private PhoneNumber phone; private Address address; //setters & getters }
我们根据学生 ID 选择学生信息
方法一:使用句点符号表示嵌套对象的引用,
Student 的 address 属性使用了圆点记法被赋上了 address 对应列的值。
方法二:使用mybatis提供的一对一映射,关键字:association
1)
<resultMap type="Address" id="AddressResult"> <id property="addrId" column="addr_id" /> <result property="street" column="street" /> <result property="city" column="city" /> <result property="state" column="state" /> <result property="zip" column="zip" /> <result property="country" column="country" /> </resultMap> <resultMap type="Student" id="StudentWithAddressResult"> <id property="studId" column="stud_id" /> <result property="name" column="name" /> <result property="email" column="email" /> <association property="address" resultMap="AddressResult" /> </resultMap> <select id="findStudentWithAddress" parameterType="int" resultMap="StudentWithAddressResult"> SELECT STUD_ID, NAME, EMAIL, A.ADDR_ID, STREET, CITY, STATE, ZIP, COUNTRY FROM STUDENTS S LEFT OUTER JOIN ADDRESSES A ON S.ADDR_ID=A.ADDR_ID WHERE STUD_ID=#{studId} </select>
元素<association>被用来导入“有一个”(has-one)类型的关联。
如果内嵌的对象有对应的 resultMap 那么使用 <association property="address" resultMap="AddressResult" /> 比较方便。如果内嵌对象需要其它的查询来生成对象,使用方法 3)比较好。
2)也可以使用<association 定义内联的 resultMap,代码如下所示:
<resultMap type="Student" id="StudentWithAddressResult"> <id property="studId" column="stud_id" /> <result property="name" column="name" /> <result property="email" column="email" /> <association property="address" javaType="Address"> <id property="addrId" column="addr_id" /> <result property="street" column="street" /> <result property="city" column="city" /> <result property="state" column="state" /> <result property="zip" column="zip" /> <result property="country" column="country" /> </association> </resultMap>
3)通过使用嵌套 select 查询来获取 Student 及其 Address 信息,代码如下:
<resultMap type="Address" id="AddressResult"> <id property="addrId" column="addr_id" /> <result property="street" column="street" /> <result property="city" column="city" /> <result property="state" column="state" /> <result property="zip" column="zip" /> <result property="country" column="country" /> </resultMap> <select id="findAddressById" parameterType="int" resultMap="AddressResult"> SELECT * FROM ADDRESSES WHERE ADDR_ID=#{id} </select> <resultMap type="Student" id="StudentWithAddressResult"> <id property="studId" column="stud_id" /> <result property="name" column="name" /> <result property="email" column="email" /> <association property="address" column="addr_id" select="findAddressById" /> </resultMap> <select id="findStudentWithAddress" parameterType="int" resultMap="StudentWithAddressResult"> SELECT * FROM STUDENTS WHERE STUD_ID=#{Id} </select>
在此方式中,<association>元素的 select 属性被设置成了 id 为 findAddressById 的语句。这里,两个分开的SQL 语句将会在数据库中执行,第一个调用 findStudentWithAddress 加载 student 信息,而第二个调用 findAddressById 来加载 address 信息。
Addr_id 列的值将会被作为输入参数传递给 selectAddressById 语句;如果有多个参数可以通过逗号分隔。
2 一对多映射,关键词使用 <collection>
比如老师和课程的关系,老师可以讲多门课程,每门课程有一个老师
public class Course { private Integer courseId; private String name; private String description; private Date startDate; private Date endDate; private Integer tutorId; //setters & getters } public class Tutor { private Integer tutorId; private String name; private String email; private Address address; private List<Course> courses; // setters & getters }
1)使用内嵌结果 ResultMap 实现一对多映射。下面是配置文件
<resultMap type="Course" id="CourseResult"> <id column="course_id" property="courseId" /> <result column="name" property="name" /> <result column="description" property="description" /> <result column="start_date" property="startDate" /> <result column="end_date" property="endDate" /> </resultMap> <resultMap type="Tutor" id="TutorResult"> <id column="tutor_id" property="tutorId" /> <result column="tutor_name" property="name" /> <result column="email" property="email" /> <collection property="courses" resultMap="CourseResult" /> </resultMap> <select id="findTutorById" parameterType="int" resultMap="TutorResult"> SELECT T.TUTOR_ID, T.NAME AS TUTOR_NAME, EMAIL, C.COURSE_ID, C.NAME, DESCRIPTION, START_DATE, END_DATE FROM TUTORS T LEFT OUTER JOIN ADDRESSES A ON T.ADDR_ID=A.ADDR_ID LEFT OUTER JOIN COURSES C ON T.TUTOR_ID=C.TUTOR_ID WHERE T.TUTOR_ID=#{tutorId} </select>
<collection>元素的 resultMap 属性设置成了 CourseResult,CourseResult 包含了 Course 对象属性与表列名之间的映射。
2)使用嵌套 Select 语句实现一对多映射,配置文件如下
<resultMap type="Course" id="CourseResult"> <id column="course_id" property="courseId" /> <result column="name" property="name" /> <result column="description" property="description" /> <result column="start_date" property="startDate" /> <result column="end_date" property="endDate" /> </resultMap>