mybatis 复杂对象映射

 一对一 一对多


<resultMap id="orderMessageMap" type="com.hywa.orderclient.vo.RiderOrderMessageVo">
    <id column="id" property="orderId"/>
    <result column="complete" property="status"/>
    <result column="create_time" property="createTime"/>
    <result column="delivery_time" property="deliveryTime"/>
    <result column="purchase_order_number" property="purchaseOrderNumber"/>

    <result column="tally_time" property="tallyTime"/>
    <result column="rider" property="rider"/>

    <association property="logisticsMessageWxVo"
      javaType="com.hywa.orderclient.vo.LogisticsMessageWxVo">
      <result column="recipient" property="recipient"/>
      <result column="recipient_phone" property="recipientPhone"/>
      <result column="recipient_address" property="recipientAddress"/>
    </association>

    <collection property="list" javaType="java.util.List"
      ofType="com.hywa.orderclient.vo.TransferOrderOfRiderVo">
      <id column="transfer_order_id" property="id"/>
      <result column="water_number" property="waterNumber"/>
      <result column="seller_name" property="sellerName"/>
      <result column="stall_name" property="stallName"/>
      <result column="product_name" property="productName"/>
      <result column="specification" property="specification"/>
      <result column="amount" property="amount"/>
      <result column="update_time" property="pickUpTime"/>
      <result column="transfer_order_status" property="status"/>
    </collection>

  </resultMap>

一对多

public class DbAssetsStockAllot extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** id */
    private Long aId;


    /** 领用部门id */
    private Long depId;

    /** 领用物资表 */
    private List<Long> stockValue;
}


<resultMap type="com.xtd.project.assets.domain.DbAssetsStockAllot" id="DbAssetsStockAllotResultEx">
    <result property="aId"    column="a_id"    />
    <result property="depId"    column="dep_id"    />
    <collection property="stockValue"  javaType="java.util.List" ofType="java.lang.Long">
        <result column="db_as_st_id"/>
    </collection>
</resultMap>

SELECT
       a.a_id,
       a.dep_id,
       b.db_as_st_id
 FROM
       db_assets_stock_allot a
LEFT JOIN db_assets_stock_allot_ex b ON a.a_id = b.allot_id
      WHERE
         a.a_id = #{aId}

一对多 

/**
 * 卖家商品展示列表
 * @author vic
 * */
@Data
@ApiModel(value = "卖家商品展示列表",description = "卖家商品展示列表")
public class SellerProductInfoWxVO implements Serializable {

    private static final long serialVersionUID = -1315457250100612951L;

    /** 主键*/
    @ApiModelProperty(value = "主键",example = "主键")
    private String id;

    @ApiModelProperty(value = "卖家id",example = "卖家id")
    private String sellerId;

    @ApiModelProperty(value = "卖家名称",example = "卖家名称")
    private String sellerName;

    /** 名称*/
    @ApiModelProperty(value = "商品名称",example = "商品名称")
    private String name;

    /** 别名*/
    @ApiModelProperty(value = "商品别名",example = "商品别名")
    private String goodsName;

    /**  商品类型*/
    @ApiModelProperty(value = "商品大类型",example = "商品大类型")
    private String productBigTypeId;

    /**  商品类型*/
    @ApiModelProperty(value = "商品类型",example = "商品类型")
    private String productTypeId;

    /** 商品主键*/
    @ApiModelProperty(value = "商品主键",example = "商品主键")
    private String productId;

    /**商品图标*/
    @ApiModelProperty(value = "商品图标",example = "商品图标")
    private String imgUrl;

    /**状态*/
    @ApiModelProperty(value = "状态",example = "状态  0启用,1禁用")
    private String enable;

    /** 规格信息*/
    @ApiModelProperty(value = "规格信息",example = "规格信息")
    private List<SellerProductSpec> sellerProductSpecs;

}
<resultMap id="findProductByIdOfBuyerMap" type="com.hywa.supplychain.baseclient.vo.SellerProductInfoWxVO">
        <id column="id" property="id" jdbcType="CHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="seller_id" property="sellerId" jdbcType="VARCHAR"/>
        <result column="seller_name" property="sellerName" jdbcType="VARCHAR"/>
        <result column="goods_name" property="goodsName" jdbcType="VARCHAR"/>
        <result column="img_url" property="imgUrl" jdbcType="VARCHAR"/>
        <result column="enable" property="enable" jdbcType="VARCHAR"/>
        <result column="product_big_type_id" property="productBigTypeId" jdbcType="VARCHAR"/>
        <result column="product_type_id" property="productTypeId" jdbcType="VARCHAR"/>
        <result column="product_id" property="productId" jdbcType="VARCHAR"/>
        <collection property="sellerProductSpecs" javaType="java.util.List"
                    ofType="com.hywa.supplychain.baseclient.entity.SellerProductSpec">
            <id column="specId" property="id" jdbcType="CHAR"/>
            <result column="specification" property="specification" jdbcType="VARCHAR"/>
            <result column="price" property="price" jdbcType="DOUBLE"/>
            <result column="inventory" property="inventory" jdbcType="DOUBLE"/>
            <result column="avg_price" property="avgPrice" jdbcType="DOUBLE"/>
        </collection>
    </resultMap>
 <select id="findProductByIdOfBuyer" resultMap="findProductByIdOfBuyerMap">
        SELECT
            product.id,
	        info.`name` AS name,
	        sel.id as  seller_id,
            sel.`name` AS seller_name,
            product.goods_name,
            info.img_url,
            product.product_big_type_id,
            product.product_type_id,
            product.product_id,
            product. ENABLE,
            spec.id AS specId,
            spec.specification,
            spec.price,
            spec.avg_price,
            spec.inventory
        FROM
            t_bp_seller_product_info product
        LEFT JOIN t_bp_seller_info sel ON sel.id = product.seller_id
        LEFT JOIN t_bs_product_info info ON product.product_id = info.id
        LEFT JOIN t_bp_seller_product_spec spec ON product.id = spec.product_id
        WHERE
         spec.is_del = 0
        AND product.id = #{id}
    </select>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis提供了多种方式来处理复杂的结果集映射。以下是一些常见的技术和方法: 1. 嵌套查询(Nested Queries:可以使用嵌套来处理一对一、一对多和多对多的关联关系。通过定义嵌套的resultMap,可以将结果集映射到包含嵌套对象复杂数据结构中。 2. 关联查询(Association):通过使用association标签,可以将关联对象映射到主对象中。这适用于一对一和多对一的关联关系。 3. 集合查询(Collection):通过使用collection标签,可以将集合对象映射到主对象中。这适用于一对多和多对多的关联关系。 4. resultMap继承(ResultMap Inheritance):可以通过定义一个基础的resultMap,并在子resultMap中使用继承关系来重用已定义的映射规则。这样可以减少重复的配置。 5. 枚举类型映射(Enum Mapping):如果结果集中包含枚举类型的字段,可以使用类型处理器或者自定义的类型处理器来将其映射为对应的Java枚举类型。 6. 自定义映射器(Custom Mappers):如果默认的映射方式无法满足需求,可以通过自定义映射器来实现复杂的结果集映射。自定义映射器可以通过实现ResultMapResolver接口来定义自己的映射规则。 这些只是一些常见的技术和方法,MyBatis在结果集映射方面提供了很多灵活的功能和选项,可以根据具体的需求选择适合的方式来处理复杂的结果集映射
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值