mybatis的resultMap屬性----discriminator

<!-- =======================鑑別器============================ -->
    <!-- <discriminator javaType=""></discriminator>
        鑑別器:mybatis可以使用discriminator判斷某列的值,然後根據某列的值改變封裝行爲
        封裝Employee:
            如果查出的是女生:就把部門信息查詢出來,否則不查詢;
            如果是男生,把last_name這一列的值賦值給email;
     -->
     <resultMap type="com.mybatis.bean.Employee" id="MyEmpDis">
        <id column="id" property="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">
            <!--女生  resultType:指定封裝的結果類型;不能缺少。/resultMap-->
            <case value="0" resultType="com.atguigu.mybatis.bean.Employee">
                <association property="dept" 
                    select="com.mybatis.dao.DepartmentMapper.getDeptById"
                    column="d_id">
                </association>
            </case>
            <!--男生 ;如果是男生,把last_name這一列的值賦值給email; -->
            <case value="1" resultType="com.mybatis.bean.Employee">
                <id column="id" property="id"/>
                <result column="last_name" property="lastName"/>
                <result column="last_name" property="email"/>
                <result column="gender" property="gender"/>
            </case>
        </discriminator>
     </resultMap>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章