MyBatis collection定义关联的集合类型的属性封装规则

实体类对象

@Data
public class Employee {

    private Integer id;

    private String lastName;

    private Integer gender;

    private String email;

    private Department department;
}
@Data
public class Department {

    private Integer id;

    private String deptName;
    /**
     * 一个部门下有很多个员工
     */
    private List<Employee> emps;
}

查询部门的时候将部门下的所有员工一并查出来

接口中的方法:

Department `selectDeptByIdPlus(Integer id);`

实现方式一:

<!--场景2:查询部门的时候将部门下的所有员工一并查出来-->
<!--collection定义关联的集合类型的属性封装规则,ofType:指定集合里边的元素类型-->
<!--和association一样,这样的话不管会不会用到部门下的员工信息都会把员工信息查询出来
也可以通过分步的方式,员工集合信息按需加载出来-->
<resultMap id="SimpleResultMap" type="com.gf.selfdemo.mybatis.bean.Department">
    <id column="d_id" property="id"/>
    <result column="dept_name" property="deptName"/>
    <collection property="emps" ofType="com.gf.selfdemo.mybatis.bean.Employee">
        <result column="e_id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
    </collection>
</resultMap>

<select id="selectDeptByIdPlus" resultMap="SimpleResultMap">
    select d.id d_id,d.dept_name,e.id e_id,e.last_name,e.email,e.gender
    from department d
    left join employee e
    on e.dept_id=d.id
    where d.id=#{id}
</select>

实现方式二:

 <!--分步查询部门信息和部门下的员工集合信息-->
  <resultMap id="SimpleResultMap2" type="com.gf.selfdemo.mybatis.bean.Department">
        <id column="id" property="id"/>
        <result column="dept_name" property="deptName"/>
        <collection property="emps"
                    select="com.gf.selfdemo.mybatis.mapper.EmployeeMapperPlus.getEmpByDeptId"
                    column="id" fetchType="lazy">
        </collection>
    </resultMap>
    <select id="selectDeptByIdStep" resultMap="SimpleResultMap2">
        select * from department where id=#{id}
    </select>

其中关联的分步sql为:

  <select id="getEmpByDeptId" resultType="com.gf.selfdemo.mybatis.bean.Employee">
        select * from employee where dept_id=#{deptId}
    </select>

扩展知识

 <!--扩展:分布查询的时候不管是association还是collection,
    都可以将步骤一的查询结果作为步骤二的条件,如果步骤二需要传入多个条件,则需要变一下column的写法,封装为map传过去
    如:column="{key1=value1,key2=value2,key3=value3....}"
    另外,虽然全局开启了延迟加载,也可以在每个sql映射中改变,如:使用fetchType属性 lazy:延迟加载,eager:立即加载-->

discriminator 鉴别器的简单使用

<!--discriminator:鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为
    封装Employee:
        如果查出来的是女生,就把部门信息查询出来,否则不查询
        如果是男生,把last_name这一列的值赋给email-->

<resultMap id="ResultMapDis" type="com.gf.selfdemo.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">
            <!--女生,部门信息查出来-->
            <case value="0" resultType="com.gf.selfdemo.mybatis.bean.Employee">
                <!--分步查询-->
                <association property="department"
                             javaType="com.gf.selfdemo.mybatis.bean.Department"
                             column="dept_id">
                </association>
            </case>
            <!--男生,步查部门信息,把last_name赋值给email-->
            <case value="1" resultType="com.gf.selfdemo.mybatis.bean.Employee">
                <result column="last_name" property="lastName"/>
                <result column="last_name" property="email"/>
                <result column="gender" property="gender"/>
            </case>
        </discriminator>
    </resultMap>

    <select id="selectEmpDis" resultMap="ResultMapDis">
        select id,last_name,email,gender dpet_id from employee where id=#{id}
    </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值