<select id="selectByCondition" resultMap="brandResultMap">
select * from tb_brand
where
<if test="status != null">
status =#{status}
</if>
<if test="company_name != null and company_name !=''">
and companyName like #{companyName}
</if>
<if test="brand_name != null and brand_name !=''">
and brandName like #{brandName}
</if>
</select>
上述查询中当status为空时sql语句为 select * from tb_brand where and.......
sql语句报错
修改方案有以下两个
1.在where后添加固定true语句1 = 1
在第一条test中添加and
<select id="selectByCondition" resultMap="brandResultMap">
select * from tb_brand
where
1==1
<if test="status != null">
and status =#{status}
</if>
<if test="company_name != null and company_name !=''">
and companyName like #{companyName}
</if>
<if test="brand_name != null and brand_name !=''">
and brandName like #{brandName}
</if>
</select>
2.使用where标签
select * from tb_brand
<where>
<if test="status != null">
status =#{status}
</if>
<if test="company_name != null and company_name !=''">
and companyName like #{companyName}
</if>
<if test="brand_name != null and brand_name !=''">
and brandName like #{brandName}
</if>
</where>
</select>