zoukankan      html  css  js  c++  java
  • MyBatis 中一对一和一对多的映射关系

    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 对应列的值。

     View Code

    方法二:使用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>
    复制代码
  • 相关阅读:
    [SAP ABAP开发技术总结]OLE
    [SAP ABAP开发技术总结]ABAP读写、解析XML文件
    [SAP ABAP开发技术总结]FTP到文件服务器,服务器上文件读写
    [SAP ABAP开发技术总结]Function远程、同步、异步调用
    [SAP ABAP开发技术总结]采购、销售、生产简单业务流程
    [SAP ABAP开发技术总结]IDoc
    [SAP ABAP开发技术总结]BAPI调用
    [SAP ABAP开发技术总结]业务对象和BAPI
    [SAP ABAP开发技术总结]增强Enhancement
    [SAP ABAP开发技术总结]SD销售订单定价过程
  • 原文地址:https://www.cnblogs.com/www123----/p/7857354.html
Copyright © 2011-2022 走看看