码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis分步查询与延迟加载

时间:2018-01-02 21:26:02      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:图片   ima   基础上   log   字段   lazyload   inf   loading   查询   

1.分步查询

先查询用户的部门

部门Mapper.xml

<resultMap id="rMap" type="com.yunqing.mybatis.bean.Department">
        <id column="id" property="id"/>
        <result column="department" property="departmentName"/>
    </resultMap>
    <select id="getDepByIdStep2" resultMap="rMap">
        SELECT id,department FROM t_dep WHERE id=#{id}
    </select>

再通过查用户查出部门

用户Mapper.xml

<resultMap id="rMap" type="com.yunqing.mybatis.bean.User">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="age" property="age"/>
<!--depUser实体类中的Department dep;select中的内容是查询DepartmentMapper接口的getDepByStep2方法;dep_id是User数据库表字段名-->
<association property="dep" select="com.yunqing.mybatis.dao.DepartmentMapper.getDepByIdStep2" column="dep_id"> </association> </resultMap> <select id="getUserByIdStep1" resultMap="rMap"> SELECT * FROM t_user WHERE id=#{id} </select>

2.延迟加载

分部查询的好处是可以延迟加载,只有在需要时才查询部门,不需要时可以不用查询部门。

需要在分部查询的基础上加上两个设置。

技术分享图片

    <settings>
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

 

mybatis分步查询与延迟加载

标签:图片   ima   基础上   log   字段   lazyload   inf   loading   查询   

原文地址:https://www.cnblogs.com/yunqing/p/8178874.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!