mybatis_映射查询
一、一对一映射查询:
第一种方式(手动映射):借助resultType属性,定义专门的pojo类作为输出类型,其中该po类中封装了查询结果集中所有的字段。此方法较为简单,企业中使用普遍。
1 <!-- 2 【手动映射:】 3 查询用户和用户所属的订单信息: 4 定义一个包含用户和订单实体的所有属性的全pojo类,将查询结果中的所有字段和全pojo类中的属性相对应。 5 --> 6 <select id="findUserAndOrders1" resultType="com.itheima.mybatis.pojo.OrdersAnduser"> 7 select o.*,u.id uid,u.username,u.address,u.birthday 8 from user u,orders o 9 where u.id=o.user_id 10 </select>
第二种方式(自动映射):借助resultMap属性,定义专门的resultMap用于映射一对一查询结果。
以用户订单关联关系为例,一个订单只能所属一个用户(一对一查询)
1 <!-- 2 type:表示返回的数据类型 3 id:表示resultMap的唯一标识 4 --> 5 <resultMap type="com.itheima.mybatis.pojo.Orders" id="OrdersUserResultMap"> 6 <!-- 主键列对应的实体类中的唯一属性 --> 7 <id column="id" property="id"/> 8 <!-- 普通列对应实体类中的普通属性 --> 9 <result column="user_id" property="userId"/> 10 <result column="number" property="number"/> 11 <result column="createtime" property="createtime"/> 12 <result column="note" property="note"/> 13 <!-- association表示进行关联查询的实体映射 14 property:表示被关联对象在查询对象中的属性民称 15 javaType:表示被关联对象的全路径名称 16 --> 17 <association property="user" javaType="com.itheima.mybatis.pojo.User"> 18 <id column="uid" property="id"/> 19 <result column="username" property="username"/> 20 <result column="gender" property="gender"/> 21 <result column="birthday" property="birthday"/> 22 <result column="address" property="address"/> 23 </association> 24 </resultMap> 25 <!-- 26 【自动映射:】 27 使用resultMap,封装一对一映射关系: 28 在orders订单类配置User对象,一个订单只能所属一个用户。 29 --> 30 <select id="findUserAndOrders2" resultMap="OrdersUserResultMap"> 31 select o.*,u.id uid,u.username,u.address,u.birthday 32 from user u,orders o 33 where u.id=o.user_id 34 </select>
二、一对多映射查询:
只能使用手动映射, 在resultMap中可以使用collection标签来标记对集合对象的关系映射。
以用户订单关联关系为例,一个用户可以有多个订单(一对多查询)
1 <!-- 一对多映射: --> 2 <select id="findUserAndOrders3" resultMap="UserOrdersResultMap"> 3 select u.*,o.id oid,o.createtime,o.number 4 from user u,orders o 5 where u.id=o.user_id 6 </select> 7 <!-- 8 type:表示返回的数据类型 9 id:表示resultMap的唯一标识 10 --> 11 <resultMap type="com.itheima.mybatis.pojo.User" id="UserOrdersResultMap"> 12 <id column="oid" property="id"/> 13 <result column="username" property="username"/> 14 <result column="gender" property="gender"/> 15 <result column="birthday" property="birthday"/> 16 <result column="address" property="address"/> 17 <!-- 18 collection:表示关联查询的结果集 19 property:关联查询的结果集存储在User对象的上的哪个属性 20 ofType:表示返回集合中的数据类型 21 --> 22 <collection property="ordersList" ofType="com.itheima.mybatis.pojo.Orders"> 23 <id column="id" property="id"/> 24 <result column="user_id" property="userId"/> 25 <result column="number" property="number"/> 26 <result column="createtime" property="createtime"/> 27 <result column="note" property="note"/> 28 </collection> 29 </resultMap>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?