mybatis多表查询

多表查询

在操作数据库中经常要进行多表查询,这时我们就需要在.xml中编写resultmap,以下是一个示例:

<resultMap id="productMap" type="com.xxx.xxx.entity.Product">
  	<id column="p_id" property="pId"/>
    <result column="last_edit_time" property="lastEditTime"/>
    ...
    <result column="estatus" property="eStatus"/>
    <association property="productCategory" column="product_category_id"
    	javaType="com.imooc.o2o.entity.ProductCategory">
    	<id column="product_category_id" property="productCategoryId"/>
    	<result column="product_category_name" property="productCategoryName"/>	
    </association>
    <association property="shop" column="shop_id"
    	javaType="com.xxx.xxx.entity.Shop">
    	<id column="shop_id" property="shopId"/>
    	<result column="owner_id" property="ownerId"/>	
    	<result column="shop_name" property="shopName"/>
    </association>
    <collection property="productImgList" column="product_id"
    	ofType="com.xxx.xxx.entity.ProductImg">
    	<id column="product_img_id" property="productImgId"/>
    	<result column="detail_img" property="imgAddr"/>	
    	...
    	<result column="product_id"  property="productId"/>
    </collection>
  </resultMap>

association

association用于关联表,表示一对一或多对一。比如这里的Product对应ProductCategory与Shop。这product实体类中有productcategory与shop的属性,而productcategory与shop分别对应自己的表。

collection

collection字面意思就是集合,用于一对多。在这里,因为product类的属性有一个ListproductImgList,表示一对多,因此这里我们不能继续使用association,而是collection

注意

以上就是mybatis多表查询关于resultmap的知识,这里需要注意的是:
在resultmap、association、collection中类型的拼写分别对应着:type、javatype、oftype,不可混淆,不然会出现关联不匹配的错误

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值