MyBatis(四)映射文件 之 resultMap:鉴别器

一、鉴别器

  鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为
<discriminator javaType="">
    <case value=""></case>
</discriminator>
  案例:
    封装 Employee:
      如果查出的是女生:就把部门信息查询出来,否则不查询;
      如果是男生,把last_name这一列的值赋值给email;
    <resultMap id="MyEmpByDis" type="com.njf.mybatis.bean.Employee">
        <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:指定封装的结果类型,不能缺少-->
            <case value="0" resultType="com.njf.mybatis.bean.Employee">
                <association property="department"
                             select="com.njf.mybatis.dao.DepartmentMapper.getDeptById"
                             column="dept_id">
                </association>
            </case>
            <!--男生-->
            <case value="1" resultType="com.njf.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>
 
 
 
 
原文地址:https://www.cnblogs.com/niujifei/p/15238911.html