mybatis 嵌套

一张栏目表t_column_video

一张栏目下的视频列表t_attachment_video 

xml:

<resultMap type="java.util.HashMap" id="definedMap">
  <id column="code" property="code" jdbcType="VARCHAR" />
  <result column="columnname" property="columnname" jdbcType="VARCHAR" />
  <result column="columnname" property="code" jdbcType="VARCHAR" />
 <collection column="code" property="children" javaType="java.util.ArrayList" select="selectvideo"  /> 

  </resultMap>

 <select id="find" resultMap ="definedMap" >
  select code ,columnname from t_column_video

  </select>

  <select id="selectvideo" resultMap ="definedMap" >
  select id, oldName, newName, columnCode, src, code, fileSuffix, fileSize, uploadTime, fileFullName, 
    fkTopicCode from t_attachment_video where columnCode = #{code}

  </select>

mapper

List<Map<String,Object>> find();


MyBatis嵌套映射(Nested Mapping)是MyBatis框架中用于处理复杂数据结构和关联查询的一种高级映射技术。在传统的一对一、一对多和多对多的关系映射中,当数据模型包含嵌套的对象或者集合时,MyBatis嵌套映射就显得尤为重要。 嵌套映射允许你在SQL查询结果集中直接获取嵌套的对象或集合,而不需要显式地进行多次对象操作。它通常通过`<select>`标签中的`resultType`或`resultMap`元素来配置,同时可能涉及到`association`、`collection`、`discriminator`等元素来指定关联关系的处理方式。 例如,如果你有一个`User`对象,它有一个`Address`对象作为嵌套,你可以这样配置: ```xml <select id="selectUserWithAddress" resultType="com.example.User"> SELECT * FROM user LEFT JOIN address ON user.id = address.user_id </select> <resultMap id="userResultMap" type="com.example.User"> <id property="id" column="user_id"/> <result property="name" column="user_name"/> <!-- 使用association来映射嵌套的Address --> <association property="address" javaType="com.example.Address" select="selectAddressById"> <id property="id" column="address_id"/> <result property="street" column="street"/> </association> </resultMap> <select id="selectAddressById" parameterType="int" resultType="com.example.Address"> SELECT * FROM address WHERE id = #{id} </select> ``` 在这个例子中,`selectUserWithAddress`方法会返回一个包含`User`对象的结果集,其中的`address`属性会被自动解析为从`selectAddressById`查询得到的`Address`对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值