mybatis中实现一对一,一对多查询

时间:2023-03-10 00:08:57
mybatis中实现一对一,一对多查询

在实际的开发中我们经常用到的是一对一查询和一对多查询。而多对多的实现是通过中间来实现,这里就没有给出来了mybatis中实现一对一,一对多查询

比如:

订单和用户是一对一的关系(一个订单只能对应一个用户)

订单和订单明细是一对多的关系(一个订单可以对应多个订单明细)

程序结构图:

mybatis中实现一对一,一对多查询

所采用的mybatis版本3.2.3

对应的jar包如下图所示

mybatis中实现一对一,一对多查询

对应库表的信息

用户表:

mybatis中实现一对一,一对多查询

订单表:

mybatis中实现一对一,一对多查询

订单明细表:

mybatis中实现一对一,一对多查询

对应的实体类的信息

用户实体类;

mybatis中实现一对一,一对多查询

用户扩展实体类:

mybatis中实现一对一,一对多查询

订单实体类:

mybatis中实现一对一,一对多查询

订单明细实体类:

mybatis中实现一对一,一对多查询

==============================【一对一(resultType方式) begin】==============================

OrderContent.xml

<!-- 一对一: 通过查询商品,查询出对应的用户的信息 -->
<!-- 方式1:resultType -->
<select id="findOrderAndRelateUserByResultType" resultType="com.ithema.mybatis.po.OrderVo">
SELECT
orders.id,
orders.order_number,
orders.user_id,
user.username,
user.address
FROM orders,
USER
WHERE orders.user_id = user.id
</select>

OrderContent.java

/**
* resultType的方式实现通过订单查询用户(一对一)
* @return
*/
List<OrderVo> findOrderAndRelateUserByResultType();

查询结果:

mybatis中实现一对一,一对多查询

数据库结果:

mybatis中实现一对一,一对多查询

==============================【一对一(resultType方式) end】==============================

==============================【一对一(resultMap方式) begin】==============================

OrderContent.xml

<!-- 方式2:resultMap -->
<!-- 定义一个resultMap,用于映射订单信息和用户信息 id:在同一个命名空间下唯一 
type:要映射到的java类型,在Orders实体中定义一个User属性,用于映射User的信息 -->
<resultMap type="com.ithema.mybatis.po.Orders" id="orderAndRelateUserMap">
<id column="id" property="id" />
<result column="order_number" property="order_number" />
<result column="user_id" property="user_id" />

<!-- association标签用于实现一对一映射 property:将关联的信息映射到Orders对象中的哪个属性中 javaType:映射属性的java类型 -->
<association property="user" javaType="com.ithema.mybatis.po.User">
<!-- id:关联的用户信息的唯一约束 property:id指定的映射关联的对象的那个属性 -->
<id column="user_id" property="id" />
<result column="username" property="username" />
<result column="address" property="address" />
</association>
</resultMap>

<select id="findOrderAndRelateUserByResultMap" resultMap="orderAndRelateUserMap">
SELECT
orders.id,
orders.order_number,
orders.user_id,
user.username,
user.address
FROM orders,
USER
WHERE orders.user_id = user.id
</select>

OrderContent.java

/**
* resultMap的方式实现通过订单查询用户(一对一)
* @return
*/
List<Orders> findOrderAndRelateUserByResultMap();

测试结果:

mybatis中实现一对一,一对多查询

==============================【一对一(resultMap方式) end】==============================

==============================【一对多(resultMap方式) begin】==============================

OrderContent.xml

<!-- 一对多: 通过查询订单查询对应的订单明细信息 
一对多的查询只能通过resultMap的方式实现
在Orders对象中加入一个list集合表示订单明细的信息
-->
<!-- 定义一个订单和订单明细的一对多的映射Map -->
<resultMap type="com.ithema.mybatis.po.Orders" id="orderAndOrderDetailMap">
<!-- 订单对象的唯一标识 -->
<id column="id" property="id"/>
<result column="order_number" property="order_number"/>

<!-- 一对多的映射要使用collection标签 
property:将明细信息映射到哪个集合对象中
ofType:所要映射的集合对象的类型
-->
<collection property="orderDetails" ofType="com.ithema.mybatis.po.OrderDetail">
<id column="orderdetail_id" property="id"/>
<result column="item_num" property="item_num"/>
<result column="item_price" property="item_price"/>
</collection>
</resultMap>

<select id="findOrderAndOrderDetail" resultMap="orderAndOrderDetailMap">
SELECT
orders.id,
orders.order_number,
orderdetail.id orderdetail_id,
orderdetail.item_num,
orderdetail.item_price
FROM orders,
orderdetail
WHERE orders.id = orderdetail.orders_id
</select>

OrderContent.java

/**
* resultMap的方式实现通过订单查询对应的订单明细(一对多)
* @return
*/
List<Orders> findOrderAndOrderDetail();

测试结果:

mybatis中实现一对一,一对多查询

数据库结果:

mybatis中实现一对一,一对多查询

==============================【一对多(resultMap方式) end】=============================