Mybatis-collection标签

YangeIT大约 5 分钟Mybatis

Mybatis-collection标签

ResultMap: 它是一种数据库映射模式。描述如何从结果集中加载对象,主要作用是定义映射规则、级联的更新、定制类型转化器。

ResultMap的组成元素:

  • constructor - 实例化类时,注入结果到构造方法中
    • idArg - ID 参数; 将结果集标记为ID,以方便全局调用
    • arg -注入构造器的结果集
  • id – 结果集ID,将结果集标记为ID,以方便全局调用
  • result – 注入到字段或 JavaBean 属性的普通结果
  • association – 一个关联;一对一的关系,即将结果包装成这种类型

    嵌套结果映射 – associations能引用自身,或者从其它地方引用

  • collection – 一个复杂类型的集合,一对多的关系

    嵌套结果映射 – collections能引用自身,或者从其它地方引用

  • discriminator – 使用结果值来决定使用哪个 resultMap
  • case – 基于某些值的结果映射

    嵌套结果映射 – case 也是一个结果映射,因此具有相同的结构和元素;或者引用其它的结果映射

collection标签作用和使用场景

作用:在Mybaits中collection标签是用来实现连表查询的。 使用的场景:collection的使用场景为1:n和n:n两种情况。 添加的内容:使用collection的时候需要在类中添加关联集合(查询哪个类就在哪个类中添加)。

素材准备: 👈

  1. 1对多:
点击Orders实体类

/**
 * 订单
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Orders implements Serializable {

    /**
     * 订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
     */
    public static final Integer PENDING_PAYMENT = 1;
    public static final Integer TO_BE_CONFIRMED = 2;
    public static final Integer CONFIRMED = 3;
    public static final Integer DELIVERY_IN_PROGRESS = 4;
    public static final Integer COMPLETED = 5;
    public static final Integer CANCELLED = 6;

    /**
     * 支付状态 0未支付 1已支付 2退款
     */
    public static final Integer UN_PAID = 0;
    public static final Integer PAID = 1;
    public static final Integer REFUND = 2;

    private static final long serialVersionUID = 1L;

    private Long id;

    //订单号
    private String number;

    //订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消 7退款
    private Integer status;

    //下单用户id
    private Long userId;

    //地址id
    private Long addressBookId;

    //下单时间
    private LocalDateTime orderTime;

    //结账时间
    private LocalDateTime checkoutTime;

    //支付方式 1微信,2支付宝
    private Integer payMethod;

    //支付状态 0未支付 1已支付 2退款
    private Integer payStatus;

    //实收金额
    private BigDecimal amount;

    //备注
    private String remark;

    //用户名
    private String userName;

    //手机号
    private String phone;

    //地址
    private String address;

    //收货人
    private String consignee;

    //订单取消原因
    private String cancelReason;

    //订单拒绝原因
    private String rejectionReason;

    //订单取消时间
    private LocalDateTime cancelTime;

    //预计送达时间
    private LocalDateTime estimatedDeliveryTime;

    //配送状态  1立即送出  0选择具体时间
    private Integer deliveryStatus;

    //送达时间
    private LocalDateTime deliveryTime;

    //打包费
    private int packAmount;

    //餐具数量
    private int tablewareNumber;

    //餐具数量状态  1按餐量提供  0选择具体数量
    private Integer tablewareStatus;
}

点击查看订单明细OrderDetail实体类

/**
 * 订单明细
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class OrderDetail implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;

    //名称
    private String name;

    //订单id
    private Long orderId;

    //菜品id
    private Long dishId;

    //套餐id
    private Long setmealId;

    //口味
    private String dishFlavor;

    //数量
    private Integer number;

    //金额
    private BigDecimal amount;

    //图片
    private String image;
}

collection标签-1对多

collection标签入门案例

场景1

一对多: 一个订单有多个订单详情,一个订单详情只能有一个订单 查询信息:查询订单信息的时候返回订单详情信息

代码实现 👇

1、查询订单Orders信息的时候返回订单详情OrderDetail,所以应该在Orders类中添加一个OrderDetail的信息作为两个表之间的关联,又因为一个订单对应多个订单详情,所以关联的字段应该是集合。

@Data
@NoArgsConstructor
@AllArgsConstructor
public class OrderVO extends Orders implements Serializable {

    //订单菜品信息
    private String orderDishes;

    //订单详情
    private List<OrderDetail> orderDetailList;

}