MyBatis实现两个表联合查询:order_items和products
新建文件名:ProductMapper.xml
代码:
<resultMap id='BaseResultMap' type='com.ecommerce.book.pojo.Product'>
<id property='id' column='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='createdAt' column='created_at' jdbcType='TIMESTAMP'/>
<result property='updatedAt' column='updated_at' jdbcType='TIMESTAMP'/>
</resultMap>
<sql id='Base_Column_List'>
id,name,description,price,created_at,updated_at
</sql>
<select id='selectProductByOrderId' resultMap='BaseResultMap'>
select p.id,p.name,p.description,p.price,p.created_at,p.updated_at
from products p join order_items oi on p.id = oi.product_id
where oi.order_id = #{orderId}
</select>
原文地址: https://www.cveoy.top/t/topic/nDJf 著作权归作者所有。请勿转载和采集!