discriminator鉴别器的使用

来源:互联网 发布:淘宝皓月岛 编辑:程序博客网 时间:2024/06/05 06:05
myBatis可以使用discriminator判断某一列的值,然后根据某一列的值改变封装行为

ex:

根据性别字段进行判断,如果是女生,则查询对应的部门编号,如果是男生,则不用查

<resultMap id="MyEmpDis" type="com.test.beans.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:指定封装的结果类型;不能缺少。/resultMap-->            <case value="0" resultType="com.test.beans.Employee">                <association property="dept"                             select="com.test.dao.DepartMentMapper.getDeptById"                             column="d_id">                </association>            </case>            <!--男生;如果是男生,把last_name这一列的值赋值给email-->            <case value="1" resultType="com.test.beans.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>



原创粉丝点击