MyBatis 鑑別器(discriminator)的應用

MyBatis 多表操作(一對多):https://yuanyu.blog.csdn.net/article/details/89603924

分步查詢,但是男生執行的操作和女生不同

  • 如果查出的是女生:就把部門信息查詢出來,否則不查詢
  • 如果是男生,把username這一列的值賦值給email

刪除數據庫中所有數據,從新插入 數據

-- 插入
INSERT INTO t_dept(`id`, `dept_name`)
VALUES ('8848', '開發部'),
       ('6699', '測試部');
INSERT INTO t_employee(`id`, `d_id`, `username`, `gender`, `email`)
VALUES ('1', '8848', '張三', '男', '[email protected]'),
       ('2', '8848', '李四', '女', '[email protected]'),
       ('3', '8848', 'tom', '男', '[email protected]');
-- 查詢
SELECT * FROM t_employee;
SELECT * FROM t_dept;

<!--
    EmployeeMapper.xml
-->
<!--
    <discriminator javaType=""></discriminator>
    鑑別器:mybatis可以使用discriminator判斷某列的值,然後根據某列的值改變封裝行爲
    封裝Employee
        如果查出的是女生 : 就把部門信息查詢出來,否則不查詢
        如果是男生      : 把username這一列的值賦值給email
-->
<resultMap type="cn.yuanyu.mybatis.entity.Employee" id="discriminatorResultMap">
    <id column="id" property="id"/>
    <result column="username" property="username"/>
    <result column="email" property="email"/>
    <result column="gender" property="gender"/>
    <!--
        column      : 指定判定的列名
        javaType    : 列值對應的java類型
    -->
    <discriminator javaType="string" column="gender">
        <!--
        女生
        resultType : 指定封裝的結果類型,不能缺少-->
        <case value="女" resultType="cn.yuanyu.mybatis.entity.Employee">
            <association property="dept"
                         select="cn.yuanyu.mybatis.mapper.DepartmentMapper.getDeptById"
                         column="d_id">
            </association>
        </case>
        <!--
            男生
            如果是男生,把username這一列的值賦值給email
        -->
        <case value="男" resultType="cn.yuanyu.mybatis.entity.Employee">
            <id column="id" property="id"/>
            <result column="username" property="username"/>
            <result column="username" property="email"/>
            <result column="gender" property="gender"/>
        </case>
    </discriminator>
</resultMap>
<select id="discriminator" resultMap="discriminatorResultMap">
    select *
    from t_employee
    where id = #{id}
</select>
<!--
    DepartmentMapper.xml
-->
<select id="getDeptById" resultType="cn.yuanyu.mybatis.entity.Department">
    select id, dept_name deptName
    from t_dept
    where id = #{id}
</select>
public interface EmployeeMapper {
    /**
     * 根據id查詢員工信息,如果是女生部門信息也返回,男生只返回用戶信息
     *
     * @param id 用戶id
     * @return Employee + Department
     */
    Employee discriminator(Integer id);
}
@Test
public void discriminator() {
    Employee zhangsan = employeeMapper.discriminator(1); //男
    System.out.println(zhangsan);
    Employee lisi = employeeMapper.discriminator(2); //女
    System.out.println(lisi);

男生輸出示例:

==>  Preparing: select * from t_employee where id = ? 
==> Parameters: 1(Integer)
<==    Columns: id, d_id, username, gender, email
<==        Row: 1, 8848, 張三, 男, [email protected]
<==      Total: 1
Employee(id=1, username=張三, email=張三, gender=男, dept=null)

 女生輸出示例:

==>  Preparing: select * from t_employee where id = ? 
==> Parameters: 2(Integer)
<==    Columns: id, d_id, username, gender, email
<==        Row: 2, 8848, 李四, 女, [email protected]
====>  Preparing: select id, dept_name deptName from t_dept where id = ? 
====> Parameters: 8848(String)
<====    Columns: id, deptName
<====        Row: 8848, 開發部
<====      Total: 1
<==      Total: 1
Employee(id=2, username=李四, [email protected], gender=女, dept=Department(id=8848, deptName=開發部, emps=null))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章