Mybatis高级映射及其延迟加载

多对一

  1. 级联属性映射
    public class Student {
    	private Integer sid;
    	private String sname;
    	private Clazz clazz; //班级
    }
    public class Clazz {
    	private Integer cid;
    	private String cname;
    }
    
    <resultMap id="studentResultMap" type="Student">
    	<id property="sid" column="sid"/>
    	<result property="sname" column="sname"/>
    	<result property="clazz.cid" column="cid"/>
    	<result property="clazz.cname" column="cname"/>
    </resultMap>
    <select id="selectBySid" resultMap="studentResultMap">
    	select s.*, c.* from t_student s join t_clazz c on s.cid = c.cid w
    	here sid = #{sid}
    </select>
    
  2. association
    <resultMap id="studentResultMap" type="Student">
    	<id property="sid" column="sid"/>
    	<result property="sname" column="sname"/>
    	<association property="clazz" javaType="Clazz">
    		<id property="cid" column="cid"/>
    		<result property="cname" column="cname"/>
    	</association>
    </resultMap>
    <select id="selectBySid" resultMap="studentResultMap">
    	select s.*, c.* from t_student s join t_clazz c on s.cid = c.cid where sid = #{sid}
    </select>
    
  3. 分步查询
    studentMapper.xml
    <resultMap id="studentResultMap" type="Student">
    	<id property="sid" column="sid"/>
    	<result property="sname" column="sname"/>
    	<association property="clazz" select="com.powernode.mybatis.mapper.ClazzMapper.selectByCid" column="cid"/>
    </resultMap>
    <select id="selectBySid" resultMap="studentResultMap">
    	select s.* from t_student s where sid = #{sid}
    </select>
    
    clazzMapper.xml
    <select id="selectByCid" resultType="Clazz">
    	select * from t_clazz where cid = #{cid}
    </select>
    
    分步优点:代码复用性强,支持延迟加载
  4. 延迟加载
    懒加载即使用到的时候才回去查询,例如Student类有Clazz类,只有使用到了Clazz班级里的属性才会去查询,只使用Student里的变量不会去查询Clazz表
    • 设置fetchType实现懒加载,在分步查询的基础上修改
      <association property="clazz" select="com.powernode.mybatis.mapper.ClazzMapper.selectByCid" column="cid" fetchType = "lazy"/>
      
    • 全局开启懒加载
      mybatis:
        configuration:
          lazy-loading-enabled: true #开启全局懒加载
      
    • 如何针对性的关闭懒加载
      <association property="clazz" select="com.powernode.mybatis.mapper.ClazzMapper.selectByCid" column="cid" fetchType = "eager"/>
      

一对多

  1. collection
    public class Clazz {
    	private Integer cid;
    	private String cname;
    	private List<Student> stus; // 一个班级对应多名学生
    }
    
    <resultMap id="clazzResultMap" type="Clazz">
    	<id property="cid" column="cid"/>
    	<result property="cname" column="cname"/>
    	<collection property="stus" ofType="Student">
    		<id property="sid" column="sid"/>
    		<result property="sname" column="sname"/>
    	</collection>
    </resultMap>
    <select id="selectClazzAndStusByCid" resultMap="clazzResultMap">
    	select * from t_clazz c join t_student s on c.cid = s.cid where c.cid = #{cid}
    </select>
    
  2. 分步查询
    <resultMap id="clazzResultMap" type="Clazz">
    	<id property="cid" column="cid"/>
    	<result property="cname" column="cname"/>
    	<!--主要看这里-->
    	<collection property="stus" select="com.powernode.mybatis.mapper.StudentMapper.selectByCid" column="cid"/>
    </resultMap>
    <!--sql语句也变化了-->
    <select id="selectClazzAndStusByCid" resultMap="clazzResultMap">
    	select * from t_clazz c where c.cid = #{cid}
    </select>
    
    List<Student> selectByCid(Integer cid);
    
    <select id="selectByCid" resultType="Student">
    	select * from t_student where cid = #{cid}
    </select>
    
    延迟加载的机制都是一样的
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值