1. 程式人生 > 其它 >05 對映檔案-select

05 對映檔案-select

select

返回 List

<!--    public List<Employee> getEmpByLastNameLike(String lastName);-->
<!--    resultType:如果返回的是一個集合,要寫集合中元素的型別-->
    <select id="getEmpByLastNameLike" resultType="com.atguigu.mybatis.bean.Employee">
        select * from tbl_employee where last_name like #{lastname}
    </select>

記錄封裝map

    //返回一條記錄的map,key就是列名,值就是對應的值
    public Map<String,Object> getEmpByIdReturnMap(Integer id);

    //返回多條記錄封裝一個map,Map<Integer,Employee>:鍵是這條記錄的主鍵,值是記錄封裝後的java物件
    @MapKey("id")//封裝這個map的時候使用哪個屬性作為map的key
    public Map<Integer,Employee> getEmpByLastNameLikeReturnMap(String lastName);

<!--    public Map<String,Object> getEmpByIdReturnMap(Integer id);-->
    <select id="getEmpByIdReturnMap" resultType="map">
        select * from tbl_employee where id=#{id}
    </select>

<!--    public Map<Integer,Employee> getEmpByLastNameLikeReturnMap(String lastName);-->
    <select id="getEmpByLastNameLikeReturnMap" resultType="com.atguigu.mybatis.bean.Employee">
        select * from tbl_employee where last_name like #{lastName}
    </select>

自定義resultMap

    <!-- 自定義javaBean的封裝規則
    type:自定義規則的java型別
    id:唯一id  方便引用
    -->

    <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyEmp">
        <!--指定主鍵列的封裝規則
        column:指定哪一列
        property:指定對應的javaBean屬性
        -->
        <id column="id" property="id"/>
        <!--定義普通列封裝會做-->
        <result column="last_name" property="lastName"/>
        <!--其他不指定的列會自動封裝,我們要寫resulMao就把全部的對映規則都寫上-->
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
    </resultMap>

<!--    public Employee getEmpById(Integer id);-->

    <select id="getEmpById" resultMap="MyEmp">
        select * from tbl_employee where id = #{id}
    </select>

場景一

    <!--
        場景1:查詢emplpyee的同時查詢員工對應的部門
        一個員工有與之對應的部門資訊
    -->

    <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDiEmp">
        <id column="id" property="id"></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.departmentName"/>
    </resultMap>

		<!--使用assocation定義單個物件的封裝規則-->
   		<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDiEmp2">
        <id column="id" property="id"></id>
        <result column="last_name" property="lastName"/>
        <result column="gender" property="gender"/>
        <!--association 可以指定聯合的javaBean物件
            property:指定哪個屬性是聯合物件
            javaType:指定這個屬性物件的型別
        -->
        <association property="dept" javaType="com.atguigu.mybatis.bean.Department">
            <id column="did" property="id"/>
            <result column="dept_name" property="dept.departmentName"/>
        </association>


<!--    public Employee getEmpAndDept(Integer id);-->
    <select id="getEmpAndDept" resultMap="">
        SELECT e.id id,e.last_name last_name,e.gender gender,e.d_id d_id,
               d.id did,d.dept_name dept_name
        FROM tbl_employee e,tbl_dept d
        WHERE e.d_id = d.id AND e.id = #{id}
    </select>

association 分步查詢

    <!-- 使用association進行分佈查詢
           1、先按照員工id查詢員工資訊
           2、根據查詢員工資訊中的d_id值去部門表查出部門資訊
           3、部門設定到員工中
    -->

    <resultMap id="com.atguigu.mybatis.bean.Employee" type="MyEmpByStep">
        <id column="id" property="id"></id>
        <result column="last_name" property="lastName"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
        <!--關聯物件的封裝規則
            select:表明當前屬性是呼叫select指定的方法查出的結果
            column:將那一列的值傳給這個方法

            流程:使用select指定的方法(闖入column指定的這列引數的值)查出物件,並封裝給property指定的屬性
        -->
        <association property="dept"
                     select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById"
                      column="d_id"></association>

    </resultMap>
    

延遲載入

    <!-- 可以延遲載入(懶載入) 按需載入
        我們每次查詢Employee物件的時候,都將一起查詢出來
        部門資訊在我們使用的時候再去查詢
        分步查詢的基礎上加上 兩個配置
        在全部配置檔案中加上
       <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
    -->

collection定義關聯集合封裝規則

        <!--
        場景二:
            查詢部門的時候將部門對應的所有員工資訊也查詢出來
    -->

	<resultMap id="MyDept" type="com.atguigu.mybatis.bean.Department">
        <id column="did" property="id"></id>
        <result column="dept_name " property="departmentName"/>
        <!-- collection定義集合型別的屬性的封裝規則
            ofType:指定集合中元素的封裝規則
        -->
        <collection property="emps" ofType="com.atguigu.mybatis.bean.Employee">
            <!-- 定義這個集合中元素的封裝規則-->
            <id column="eid" property="id"></id>
            <result column="last_name" property="lastName"/>
            <result column="email" property="email"/>
            <result column="gender" property="gender"/>
        </collection>
    </resultMap>
<!--    public Department getDeptByIdPlus(Integer id);-->
    <select id="getDeptByIdPlus" resultMap="">
        select d.id did,dept_name dept_name,
               e.id eid,e.last_name last_name,e.mail main,e.gender gender
        from tbl_dept d
        left join tbl_employee e
        on d.id=e.id_id
        where d,id=#{id}

    </select>

collection分佈查詢

    <resultMap id="MyDeptStep" type="com.atguigu.mybatis.bean.Department">
        <id column="id" property="id"/>
        <id column="dept_name" property="department"/>
        <collection property="emps"
        select="com.atgui.mybatis.dao.EmployeeMapperPlus.getEmpsbyDeptId"
        column="id"></collection>
    </resultMap>
<!--    public Department getDeptByIdStep(Integer id);-->
    <select id="getDeptByIdStep" resultMap="">
        select id,dept_name departmentName from  tbl_dept where id = #{id}
    </select>


擴充套件:多列的值傳遞過去

將多列的值封裝map傳遞

colum="{key1=column1,key2=column2}"

fetchType="lazy" 表示使用延遲載入

eager:立即

lazy:延遲

    <resultMap id="MyDeptStep" type="com.atguigu.mybatis.bean.Department">
        <id column="id" property="id"/>
        <id column="dept_name" property="department"/>
        <collection property="emps"
        select="com.atgui.mybatis.dao.EmployeeMapperPlus.getEmpsbyDeptId"
        column="{deptId=id}" fetchType="lazy"></collection>
    </resultMap>

discriminator鑑別器

    <!--  <discriminator javaType=""></discriminator>

        鑑別器:mybatis可以使用discriminator判斷某列的值,然後根據某列的值改變封裝行為封裝Employee
        例項:如果查出的女生,就把部門資訊 查詢出來,否則不查詢
        如果是男生,把lst_name這一列的值賦值給email
    -->
    <resultMap type="com.atguigu.mybatis.bean.Employee" id="myEmp">
        <id column="id" property="id"></id>
        <result column="last_name" property="lastName"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
        <!--
            column:指定判定的列名
            javaType:列值對應的Java型別-->
        <discriminator javaType="string" column="gender">
            <!-- 女生 javaType:指定封裝的結果型別-->
            <case value="0" resultType="com.atguigu.mybatis.bean.Employee">
                <association property="dept"
                             select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById"
                             column="d_id">
                </association>
            </case>
            <!--男生-->
            <case value="1" resultType="com.atguigu.mybatis.bean.Employee">
                <id column="id" property="id"></id>
                <result column="last_name" property="lastName"/>
                <result column="last_name" property="email"/>
                <result column="gender" property="gender"/>
            </case>
        </discriminator>

    </resultMap>