resultMap、懒加载、

本文详细介绍了 MyBatis 中 resultMap 的使用方法,包括如何实现查询结果的去重及字段映射,以及如何配置懒加载进行一对多和一对一的数据关联。通过具体的 XML 配置示例,读者可以了解到如何设置 association 和 collection 属性来优化数据加载过程。

resultMap、

<resultMap id=" " type="映射的类,可别名">

<id /> 查询结果按id 的字段去重,使字段唯一

column=" ";数据库的字段; property=" "; 映射类型的属性; jdbcType 数据库的数据类型

<association property="映射的类类型,一对一" javaType="">

<id /> <result />

懒加载、

association添加属性:

1)fetchType="lazy" 开启懒加载

2)select=" ";用到再调用该方法

3)column=" ";调用该方法用到的字段

  <!-- ---------------------懒加载 一对一 ---------------------- -->
  <resultMap id="StudentResult" type="mapper.Student" extends="BaseResultMap">
  <association property="teacher" javaType="model.Teacher" select="queryBytid" column="tid" fetchType="lazy">
    <id column="tid" property="tid"/>
    <result column="tname" property="tname"/>
  </association>  
  </resultMap>
  
  <select id="queryAll" resultMap="StudentResult">
        select * from student
  </select>
  
  <select id="queryBytid" resultType="model.Teacher">
        select * from teacher where tid = #{tid}
  </select>
  
  <!-- --------------------一对多 ---------------- -->
  
  <resultMap id="TeacherResult" type="mapper.Teacher"  extends="BaseResultMap">
  <collection property="students" ofType="model.Student" >
    <id column="id" property="id"/>
    <result column="sname" property="sname"/>
  </collection>
  </resultMap>
  
  <select id="queryAll" resultMap="TeacherResult">
        select teacher.*, student.sname from student right join teacher where student.tid=teacher.tid
  </select>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值