mybatis多表进行连接查询(left join)

由于需求,遇到一个问题,商品在查询的时候不仅仅能按照自己的商品名字去查询,还要能够根据所属的商店进行多条件的查询 。

1.数据库中有两张表,shop 和product,shop的主键作为product的外键,

2.在mybatis中链接查询的代码如下:

<select id="selectByPage" resultMap="BaseResultMap" parameterType="Map" >
    select
    <include refid="Base_Column_List" />
    from oms_product op left join oms_shop os on op.op_shop=os.os_id
    <where>

            <if test="opName!=null and opName!='' ">
                and op.op_name like #{opName}
            </if>
             <if test="osName!=null and osName!='' ">
                and os.os_name like #{osName}
    </if>

    </where>
    
    <if test="start!=null and size!=null">
            limit #{start},#{size}
    </if>
  </select>

3.注意所连接的表的查询条件不能放到where中,如果放到where语句中则不起任何的作用。红色的部分。

4.此例采用的是左连接,其他链接应该同理。自己可以验证一下。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值