1.一对一的映射关系
一对一关系(One-to-One)表示两个实体对象之间存在唯一的关联关系。例如,一个学生只能拥有一个身份证。在 MyBatis 中,我们可以使用结果嵌套或一对一映射来处理一对一关系。
1.1 创建模型类和Vo类
package com.YU.model;public class OrderItem {private Integer orderItemId;private Integer productId;private Integer quantity;private Integer oid;public OrderItem(Integer orderItemId, Integer productId, Integer quantity, Integer oid) {this.orderItemId = orderItemId;this.productId = productId;this.quantity = quantity;this.oid = oid;}public OrderItem() {super();}public Integer getOrderItemId() {return orderItemId;}public void setOrderItemId(Integer orderItemId) {this.orderItemId = orderItemId;}public Integer getProductId() {return productId;}public void setProductId(Integer productId) {this.productId = productId;}public Integer getQuantity() {return quantity;}public void setQuantity(Integer quantity) {this.quantity = quantity;}public Integer getOid() {return oid;}public void setOid(Integer oid) {this.oid = oid;}
}
package com.YU.vo;import com.YU.model.Order;
import com.YU.model.OrderItem;/*** @author YU* @create 2023-09-04 9:31*/
public class OrderItemVo extends OrderItem {private Order order;public Order getOrder() {return order;}public void setOrder(Order order) {this.order = order;}
}
1.2 配置当前模型类的mapper.xml
<resultMap id="OrderItemVoMap" type="com.YU.vo.OrderItemVo"><result column="order_item_id" property="orderItemId"></result><result column="product_id" property="productId"></result><result column="quantity" property="quantity"></result><result column="oid" property="oid"></result><association property="order" javaType="com.YU.model.Order"><result column="order_id" property="orderId"></result><result column="order_no" property="orderNo"></result></association></resultMap><select id="selectByOrderItemId" parameterType="OrderItemVoMap" parameterMap="java.lang.Integer">SELECT * FROM t_hibernate_order o,t_hibernate_order_item oiwhere o.order_id = oi.oid and oi.order_item_id = #{oiid}</select>
1.3 开始测试
省略了业务逻辑层的编写,这里使用junit直接进行测试
package com.YU.biz.Impl;import com.YU.biz.OrderItemBiz;
import com.YU.vo.OrderItemVo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** @author YU* @create 2023-09-04 9:49*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring-context.xml"}
)
public class OrderTest {@Autowiredprivate OrderItemBiz orderItemBiz;@Testpublic void selectOrderItemId(){OrderItemVo orderItemVo = orderItemBiz.selectByOrderItemId(27);System.out.println(orderItemVo);System.out.println(orderItemVo.getOrder());}
}
2.一对多的映射关系
一对多关系(One-to-Many)表示一个实体对象(一的一方)可以与多个相关实体对象(多的一方)建立关联。例如,一个部门可以有多个员工。在 MyBatis 中,我们可以使用嵌套查询或结果映射来处理一对多关系。
2.1 创建模型类和Vo类
package com.YU.model;import lombok.ToString;@ToString
public class Order {private Integer orderId;private String orderNo;public Order(Integer orderId, String orderNo) {this.orderId = orderId;this.orderNo = orderNo;}public Order() {super();}public Integer getOrderId() {return orderId;}public void setOrderId(Integer orderId) {this.orderId = orderId;}public String getOrderNo() {return orderNo;}public void setOrderNo(String orderNo) {this.orderNo = orderNo;}
}
package com.YU.vo;import com.YU.model.Order;
import com.YU.model.OrderItem;import java.util.ArrayList;
import java.util.List;/*** @author YU* @create 2023-09-04 9:31*/
public class OrderVo extends Order {private List<OrderItem> OrderItems = new ArrayList<>();public List<com.YU.model.OrderItem> getOrderItem() {return OrderItems;}public void setOrderItem(List<com.YU.model.OrderItem> OrderItems) {this.OrderItems = OrderItems;}
}
2.2 配置当前模型类的mapper.xml
<resultMap id="OrderVoMap" type="com.YU.vo.OrderVo"><result column="order_id" property="orderId"></result><result column="order_no" property="productId"></result><collection property="orderItems" ofType="com.YU.model.Order"><result column="order_item_id" property="orderItemId"></result><result column="product_id" property="productId"></result><result column="quantity" property="quantity"></result><result column="oid" property="oid"></result></collection></resultMap><select id="selectByOid" resultMap="OrderVoMap" parameterType="java.lang.Integer">SELECT * FROM t_hibernate_order o,t_hibernate_order_item oiwhere o.order_id = oi.oid and o.order_id = #{oid}</select>
2.3 开始测试
@Autowiredprivate OrderBiz orderBiz;@Testpublic void selectByOid(){OrderVo orderVo = orderBiz.selectByOid(7);System.out.println(orderVo);orderVo.getOrderItem().forEach(System.out::println);}
3.多对多的映射关系
多对多的映射关系是指两个实体之间存在多对多的关系,其中一个实体可以关联多个另一个实体,而另一个实体也可以关联多个第一个实体。
在关系型数据库中,多对多的关系需要通过中间表(也称为连接表)来实现。中间表包含两个外键关联到两个实体的主键,用于记录它们之间的关系。
总结
在 MyBatis 中,映射关系是指数据库表和 Java 对象之间的映射配置,用于将查询结果映射到 Java 对象或将 Java 对象的属性映射到数据库表的列
基于 XML 的映射方式:
- 使用
<resultMap>
元素配置结果集的映射关系,指定数据库列和 Java 对象属性之间的映射。- 可以使用
<result>
元素将数据库列映射到 Java 对象的属性,并指定属性的类型、映射关系及相关配置。- 可以使用
<association>
元素配置关联对象的映射关系,用于映射复杂对象之间的关系。- 可以使用
<collection>
元素配置集合类型对象的映射关系,用于映射一对多或多对多的关系。- 使用
<sql>
元素定义可重用的 SQL 片段,提供了组织和共享 SQL 语句的能力。
注意点:
- 确保数据库表和 Java 对象的属性名称、类型一致,以便正确地映射数据。
- 配置正确的映射关系,确保查询结果能正确映射到 Java 对象,或对象的属性能正确映射到数据库表。
- 对于复杂的关联关系,需要配置适当的映射关系,以便处理关联对象之间的关系。
正确配置映射关系可以提高数据的访问效率和开发效率,使数据库表和 Java 对象之间的转换更加方便和灵活。