MyBatis之ResultMap玩法回顾

1、Select_ResultMap

自定义结果集映射规则

<!-- 指定主键列的封装规则
    id:定义主键据说会有底层优化
    colum:指定哪一列
    property: 指定对应java的bean属性
-->
<resultMap type="com.demo.pojo.Employee" id="MyEmp">
    <id column="id" property="id"/>
    <!-- 定义普通列封装属性 -->
    <result column="last_name" property="lastName"/>
</resultMap>
<select id="" resultMap="MyEmp">
    select * from employee where id=#{id}
</select>

关联查询_级联属性封装结果

<!--
    场景一:(一对一)
        查询Employee的同时查询员工对应的部门
 -->
<resultMap type="employee" id="">
    <id column="id" property="id"/>
    <result column="did" property="dept.id"/>
    <result column="deptName" property="dept.departmentName"/>
</resultMap>
<select id="" resultMap="">
    select * from employee e, department d where e.id = d.id
</select>

关联查询_association定义封装对象规则

<resultMap type="employee" id="">
    <id column="id" property="id"/>
    <!-- association可以指定联合的JavaBean对象
        property="dept":指定哪个属性是联合的对象
        javaType:指定这个属性对象的类型[不能省略]
     -->
    <association property="dept" javaType="dept">
        <id column="id" property="id"/>
        <result column="deptName" property="departmentName"/>
    </association>
</resultMap>
<select id="" resultMap="">
    select * from employee e, department d where e.id = d.id
</select>

关联查询_assocation分步查询

<!--
    使用association进行分步查询
    1、先按照员工id查询员工信息
    2、根据查询员工信息中的id值去部门表查询出部门信息
    3、部门设置到员工中
-->
<resultMap type="employee" id="">
    <id column="id" property="id"/>
    <result column="email" property="email"/>
    <result column="gender" property="gender"/>
    <!--
        select:表明当前属性是调用select指定的方法查处的结果
        column:指定将哪一列的值传给这个方法
    -->
    <assocation property="dept" select="com.demo.mapper.DeptMapper.getDeptById" column="d_id">    
    </assocation>
</resultMap>
<select id="" resultMap="">
    select * from employee where id=#{id}
</select>

关联查询__assocation_分步查询_延迟加载(懒加载/按需加载)

<!-- 在Setting中显式指定,防止版本更替 -->
<setting name="lazyLoadingEnable" value="true"></setting>
<setting name="aggressiveLazyLoading" value="false"></setting>

关联查询_collection定义关联集合封装规则

<!--
    场景二、(一对多)
        查询部门的时候将部门对应的员工信息查询出来
-->
<resutMap type="department" id="de">
    <id column = "did" property="id"/>
    <result column="deptName" property="departmentName"/>
    <!-- collection定义关联集合类型的属性的封装规则
        collection: 定义关联集合类型属性的封装规则
        ofType: 指定集合里面元素的类型
     -->
    <collection property="emps" ofType="employee">
        <!-- 定义这个集合中元素的封装规则 -->
        <id column="eid" property="id"/>
        <result column="email" property="email"/>
    </collection>
</resutMap>
<select id="" resultMap="de">
    select * from dept d left join employee e on d.id=e.id
</select>

关联查询_collection分步查询

同上assocation分步查询方式

分步查询拓展

<!-- 传递多列的值
    将多列的值封装Map传递
    key为需要接收参数的列,column为去传递参数的列
    column="{key1=column1,key2=column2}"
    fetchType="lazy":表示使用懒加载
            -lazy:懒加载
            -eager:立即查询
-->

discriminator鉴别器

<!--
    <discriminator javaType=""></discriminator>
    鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为

    封装Employee:
        如果是女,则查出部门信息
        如果是男,则将lastName赋值给email
-->
<resultMap type="employee" id="">
    <id column = "id" property="id"/>
    <result column="lastName" property="lastName"/>
    <result column="gender" property="gender"/>
    <!-- 
        column: 指定要判断的列名
        javaType: 列值对应的java类型
    -->
    <discriminator javaType="string" column="gender">
        <!-- 女生 -->
        <case value="0" resulyType="employee">
            <assocation property="dept" select="com.demo.mapper.DeptMapper.getDeptById" column="d_id"></assocation>
        </case>
        <case value="1" resulyType="employee">
            <id column = "id" property="id"/>
            <result column="lastName" property="email"/>
        </case>
    </discriminator>
</resultMap>

回顾:Select_记录封装Map

多条记录封装一个map:Map<Integer,Employee>: 键是这条记录的主键,值是记录封装后的Java对象

告诉MyBatis封装这个map时使用哪个属性作为主键,使用注解标明

@MapKey("id")

------------------------------------> - 原创不易,转载请备注来源

评论

暂无评论