mybatis-クエリー(resultMap、単一オブジェクトを関連付ける)-14

9909 ワード

1つ目の方法:ネストされた結果セット方式2つ目の方法:ステップクエリー方式、associationによって結合されたオブジェクトを定義する3つ目の方法:associationステップクエリーを使用する
シーン:employeeを検出すると同時に部門を検出し、employee->department
JAvaBeanとテーブル
public class Employee {
    private Integer id;
    private String lastName;
    private String email;
    private String gender;
    private Department dept;}

    //   :tbl_employee->id,last_name,gender,email,d_id
public class Department {
    private Integer id;
    private String name;}

   //    :tbl_dept->id,dept_name

1つ目の方法:結果セットをネストする方法
 <resultMap id="myEmpDiff" type="com.stayreal.mybatis.Employee">
            <id column="id" property="id"/>
            <result column="last_name" property="lastName"/>
            <result column="email" property="email"/>
            <result column="gender" property="gender"/>
            <result column="did" property="dept.id"/>
            <result column="dept_name" property="dept.name"/>
        </resultMap>

    <!--public Employee getEmpAndDept(Integer id);-->
        <select id="getEmpAndDept"  resultMap="myEmpDiff">
            select e.id,e.last_name,e.email,e.gender,e.d_id,
            d.id did,d.dept_name from tbl_employee e,tbl_dept d
            where e.d_id = d.id and e.id=#{id}
        </select>
//Junit:
            Employee employee = mapper.getEmpAndDept(1);
            System.out.println(employee.toString());
            System.out.println(employee.getDept());
    //Employee{id=1, lastName='Jerry', email='[email protected]', gender='1'}
    //Department{id=2, name='ceshi'}

2つ目の方法:associationによって結合されたオブジェクトを定義する
 <resultMap id="myEmpDiff2" type="com.stayreal.mybatis.Employee">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
        <!--association           property="dept":             javaType:     -->
        <association property="dept" javaType="com.stayreal.mybatis.Department">
            <id column="did" property="id"/>
            <result column="dept_name" property="name"/>
        </association>
    </resultMap>

3つ目の方法:associationステップクエリーを使用する
<!-- 1.        2.       3.            select     ,    ,  column      -->
    <resultMap id="myEmpByStep" type="com.stayreal.mybatis.Employee">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
        <association property="dept" select="com.stayreal.mybatis.DepartmentMapper.getDepartmentById" column="d_id">
        </association>
    </resultMap>
    <!--public Employee getEmpByIdStep(Integer id);-->
    <select id="getEmpByIdStep" resultMap="myEmpByStep">
        select * from tbl_employee where id=#{id}
    </select>
            :
    employee->dept        employee     ,      ,
                                    
                                   

          ,       mybatis-config.xml   
    <!--                     ,                      -->
            <setting name="lazyLoadingEnabled" value="true"/>
            <setting name="aggressiveLazyLoading" value="false"/>