在使用 MyBatis 进行联合查询时,有时会出现 Product 属性为 Null 的情况。这通常是因为关联查询中的结果没有正确映射到 Java 对象中。

要解决这个问题,需要在 resultMap 中添加关联查询结果的映射,并将 select 语句的 resultMap 属性设置为 BaseResultMap

以下代码展示了如何解决这个问题:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE mapper
        PUBLIC '-//mybatis.org//DTD Mapper 3.0//EN'
        'http://mybatis.org/dtd/mybatis-3-mapper.dtd'>
<mapper namespace='com.ecommerce.book.mapper.OrderItemsMapper'>

    <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='product_id' jdbcType='INTEGER'/>
            <result property='name' column='name' jdbcType='VARCHAR'/>
            <result property='description' column='description' jdbcType='VARCHAR'/>
            <result property='price' column='price' jdbcType='DECIMAL'/>
            <result property='categoryId' column='category_id' jdbcType='INTEGER'/>
            <result property='imageUrl' column='image_url' jdbcType='VARCHAR'/>
            <result property='stock' column='stock' jdbcType='INTEGER'/>
            <result property='isFeatured' column='is_featured' jdbcType='TINYINT'/>
            <result property='createdAt' column='created_at' jdbcType='TIMESTAMP'/>
            <result property='updatedAt' column='updated_at' jdbcType='TIMESTAMP'/>
            <result property='weight' column='weight' jdbcType='DECIMAL'/>
            <result property='brand' column='brand' jdbcType='VARCHAR'/>
            <result property='supplier' column='supplier' jdbcType='INTEGER'/>
            <result property='origin' column='origin' jdbcType='VARCHAR'/>
            <result property='isActive' column='is_active' jdbcType='TINYINT'/>
            <result property='isDeleted' column='is_deleted' jdbcType='TINYINT'/>
        </association>
    </resultMap>


    <sql id='Base_Column_List'>
        id,order_id,product_id,
        quantity,price,created_at,
        updated_at
    </sql>
    <insert id='insertOrderItem'>
        insert into order_items
        (order_id,product_id,quantity,price,created_at,updated_at)
        values
        (#{orderId},#{productId},#{quantity},#{price},#{createdAt},#{updatedAt})
    </insert>
    <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.name,p.description,p.price,p.category_id,p.image_url,p.stock,p.is_featured,p.created_at,p.updated_at,p.weight,p.brand,p.supplier,p.origin,p.is_active,p.is_deleted
        from order_items oi
        left join products p on oi.product_id = p.id
        where oi.order_id = #{orderId}

    </select>
</mapper>

通过在 resultMap 中添加关联查询结果的映射,并将 select 语句的 resultMap 属性设置为 BaseResultMap,就可以将关联查询的结果正确映射到 Java 对象中,从而解决 Product 属性为 Null 的问题。

MyBatis 联合查询中 Product 为 Null 的解决方法

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

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