由于需求,遇到一个问题,商品在查询的时候不仅仅能按照自己的商品名字去查询,还要能够根据所属的商店进行多条件的查询 。
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.此例采用的是左连接,其他链接应该同理。自己可以验证一下。