当前位置: 首页 > news >正文

Mybatis——一对多关联映射

一对多关联映射

一对多关联映射有两种方式,都用到了collection元素

以购物网站中用户和订单之间的一对多关系为例

collection集合的嵌套结果映射

创建两个实体类和映射接口

package org.example.demo;import lombok.Data;import java.util.List;@Data
public class User {private Integer userId;private String userName;private String password;private Cart cart;private List<Order> orderList;
}
package org.example.demo;import lombok.Data;@Data
public class Order {private Integer orderId;private double price;
}

实现根据用户id查询出所有用户信息,包括该用户的所有订单信息 

package org.example.mapper;import org.example.demo.User;import java.util.List;public interface UserMapper {User findUserAndOrderListByUserId(Integer userId);
}
方式一:与association类似,集合的嵌套结果映射就是指通过一次SQL查询得到所有的结果 
<resultMap type="com.mybatis.entity.User" id="userMap"><id property="id" column="id"/><result property="userName" column="user_name"/><result property="password" column="password"/>
</resultMap>
<resultMap type="com.mybatis.entity.User" id="userAndOrderListMap" extends="userMap"><collection property="orderList" ofType="com.mybatis.entity.Order"><id property="id" column="order_id"/><result property="price" column="price"/></collection>
</resultMap>
<select id="findUserAndOrderListById" resultMap="userAndOrderListMap">select u.id, u.user_name, u.password,o.order_id, o.pricefrom user uleft join orders o on u.id = o.user_idwhere u.id = #{id}
</select>

resultMap元素中的extends属性可以实现结果映射的继承

collection的ofType属性指定集合中元素的类型,必选项 

    <resultMap id="userAndOrderMap" type="org.example.demo.User"><id property="userId" column="user_id"/><result property="userName" column="user_name"/><result property="password" column="password"/><collection property="orderList" ofType="org.example.demo.Order"><id property="orderId" column="order_id"/><result property="price" column="price"/></collection></resultMap><select id="findUserAndOrderListByUserId" resultMap="userAndOrderMap">select*from t_user uleft join t_order o on u.user_id = o.user_idwhere u.user_id = #{userId};</select>

 

collection集合的嵌套查询

集合的嵌套查询同样会执行额外的SQL查询

<resultMap type="com.mybatis.entity.User"id="userAndOrderListMap" extends="userMap"><collection property="orderList" column="{uid=id}"ofType="com.mybatis.entity.Order"       select="com.mybatis.mapper.OrderMapper.findOrdersByUserId"></collection>
</resultMap>
<select id="findUserAndOrderListById" resultMap="userAndOrderListMap">select * from user where id = #{id}
</select>

OrderMapper.xml

<resultMap type="com.mybatis.entity.Order"id="orderMap"><id property="id" column="order_id"/><result property="price" column="price"/>
</resultMap>
<select id="findOrdersByUserId"resultMap="orderMap">select * from orders where user_id = #{uid}
</select>

对比两种方式

第一种方式属于“关联的嵌套结果映射“,即通过一次SQL查询根据表或指定的属性映射到不同的对象中

第二种方式属于“关联的嵌套查询”,利用简单的SQL语句,通过多次查询得到想要的结果,也可以实现延迟加载效果 

 

 

http://www.lryc.cn/news/332999.html

相关文章:

  • Pytorch实用教程:TensorDataset和DataLoader的介绍及用法示例
  • uni-app如何实现高性能
  • docker 应用部署
  • java.awt.FontFormatException: java.nio.BufferUnderflowException
  • C++ 枚举类型 ← 关键字 enum
  • MySQL故障排查与优化
  • 如何做一个知识博主? 善用互联网检索
  • 《QT实用小工具·十》本地存储空间大小控件
  • 作为一个初学者该如何学习kali linux?
  • 多线程学习-线程池
  • Linux第4课 Linux的基本操作
  • 堆排序解读
  • docker + miniconda + python 环境安装与迁移(详细版)
  • 蓝桥杯刷题第八天(dp专题)
  • 【WEEK6】 【DAY1】DQL查询数据-第一部分【中文版】
  • Linux:权限篇
  • Lua热更新(xlua)
  • 并查集(基础+带权以及可撤销并查集后期更新)
  • 基于 Java 的数据结构和算法 (不定期更新)
  • 考研回忆录【二本->211】
  • 【XCPC笔记】2023 (ICPC) Jiangxi Provincial Contest——ABCIJKL 做题记录
  • 猫头虎分享已解决Bug || **URLError (URL错误)** 全方位解析
  • 如何使用极狐GitLab 启用自动备份功能
  • HTML/XML转义字符对照
  • 设计模式:组合模式示例
  • 普通情况和高并发时,Redis缓存和数据库怎么保持一致?
  • Django -- 自动化测试
  • NodeJS 在Windows / Mac 上实现多版本控制
  • Web3 游戏周报(3.24-3.30)
  • 算法思想1. 分治法2. 动态规划法3. 贪心算法4. 回溯法