MyBatis查询sql写法
1.多条件,可全写,可空缺
<select id="selectByCondition" resultMap="brandResultMap">
select *
from tb_brand
<where>
<if test="status != null">status = #{status}</if>
<if test="companyName != null and companyName != '' ">and company_name like #{companyName}</if>
<if test="brandName != null and brandName != '' ">and brand_name like #{brandName}</if>
</where>
</select>
2.单条件,但可以选择传入的字段名
<select id="selectByConditionSingle" resultMap="brandResultMap">
select *
from tb_brand
<where>
<choose>
<when test="status != null">status = #{status}</when>
<when test="companyName != null and companyName != '' ">and company_name like #{companyName}</when>
<when test="brandName != null and brandName != '' ">and brand_name like #{brandName}</when>
</choose>
</where>
</select>