在 MyBatis 的 XML 映射文件中,可以使用 '' 标签来实现关联查询,但需要注意以下几点:

  1. 关联查询的结果集需要使用 '' 标签进行映射,同时需要在 '' 标签中使用 '' 标签指定关联对象的映射。

  2. 在关联对象的映射中,需要使用 '' 标签指定关联对象的主键,而不是使用 '' 标签。同时,需要使用 '' 标签映射关联对象的其他属性。

  3. 在 SQL 查询语句中,需要使用 left join 来关联查询两张表,并且需要在查询语句中指定关联对象的所有属性。

根据上述要点,可以对给定的 XML 映射文件进行如下修改:

<resultMap id="BaseResultMap" type="com.ecommerce.book.pojo.OrderItems">
    <id property="id" column="id" jdbcType="INTEGER"/>
    <result property="orderId" column="order_id" jdbcType="INTEGER"/>
    <result property="productId" column="product_id" jdbcType="INTEGER"/>
    <result property="quantity" column="quantity" jdbcType="INTEGER"/>
    <result property="price" column="price" jdbcType="DECIMAL"/>
    <result property="createdAt" column="created_at" jdbcType="TIMESTAMP"/>
    <result property="updatedAt" column="updated_at" jdbcType="TIMESTAMP"/>
    <association property="products" javaType="com.ecommerce.book.pojo.Products">
        <id property="id" column="p_id" jdbcType="INTEGER"/>
        <result property="name" column="p_name" jdbcType="VARCHAR"/>
        <result property="description" column="p_description" jdbcType="VARCHAR"/>
        <result property="price" column="p_price" jdbcType="DECIMAL"/>
        <result property="categoryId" column="p_category_id" jdbcType="INTEGER"/>
        <result property="imageUrl" column="p_image_url" jdbcType="VARCHAR"/>
        <result property="stock" column="p_stock" jdbcType="INTEGER"/>
        <result property="isFeatured" column="p_is_featured" jdbcType="TINYINT"/>
        <result property="createdAt" column="p_created_at" jdbcType="TIMESTAMP"/>
        <result property="updatedAt" column="p_updated_at" jdbcType="TIMESTAMP"/>
        <result property="weight" column="p_weight" jdbcType="DECIMAL"/>
        <result property="brand" column="p_brand" jdbcType="VARCHAR"/>
        <result property="supplier" column="p_supplier" jdbcType="INTEGER"/>
        <result property="origin" column="p_origin" jdbcType="VARCHAR"/>
        <result property="isActive" column="p_is_active" jdbcType="TINYINT"/>
        <result property="isDeleted" column="p_is_deleted" jdbcType="TINYINT"/>
    </association>
</resultMap>

<select id="selectByOrderId" resultMap="BaseResultMap">
    select
    oi.id,oi.order_id,oi.product_id,oi.quantity,oi.price,oi.created_at,oi.updated_at,
    p.id as p_id,p.name as p_name,p.description as p_description,p.price as p_price,p.category_id as p_category_id,p.image_url as p_image_url,p.stock as p_stock,p.is_featured as p_is_featured,p.created_at as p_created_at,p.updated_at as p_updated_at,p.weight as p_weight,p.brand as p_brand,p.supplier as p_supplier,p.origin as p_origin,p.is_active as p_is_active,p.is_deleted as p_is_deleted
    from order_items oi
    left join products p on oi.product_id = p.id
    where oi.order_id = #{orderId}
</select>

在修改后的 XML 映射文件中,更改了 '' 标签中的 '' 标签,将其指定为关联对象的主键,并且在 SQL 查询语句中指定了关联对象的所有属性。

MyBatis 关联查询:解决 products 为 null 的问题

原文地址: https://www.cveoy.top/t/topic/nDLQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录