问题遇到的现象和发生背景
-在学习Mybaits时发现如果Mybaits的动态SQL的if标签中如果有三个条件, 第二个条件的为空。
运行时只执行了第一个。为空的条件和后面的条件都不会执行如何解决呢?
谢谢
问题相关代码,请勿粘贴截图
**<resultMap id="brnadResultMap" type="brand">
<result column="brand_name" property="brandName"/>
<result column="company_name" property="companyName"/>
</resultMap>
<select id="selectByConfige" resultMap="brnadResultMap">
select *
from t_brand
<where>
<if test="status != NULL">
and status = #{status}
</if>
<if test=" companyName != null and companyName !='' ">
and company_name like #{companyName}
</if>
<if test=" brand_name != null and brand_name !='' ">
and brand_name like #{brandName};
</if>
</where>
</select>
**
运行结果及报错内容
当中间的条件companyName为空时 运行的结果(部分)
[DEBUG] 10:31:39.233 [main] c.a.m.B.selectByConfige - ==> Preparing: select * from tb_brand WHERE status = ?
[DEBUG] 10:31:39.259 [main] c.a.m.B.selectByConfige - ==> Parameters: 1(Integer)
[DEBUG] 10:31:39.270 [main] c.a.m.B.selectByConfige - <== Total: 3
当最后的条件brandName为空时 运行的结果(部分)
[DEBUG] 10:32:54.654 [main] c.a.m.B.selectByConfige - ==> Preparing: select * from tb_brand WHERE status = ? and company_name like ?
[DEBUG] 10:32:54.680 [main] c.a.m.B.selectByConfige - ==> Parameters: 1(Integer), %华为%(String)
[DEBUG] 10:32:54.695 [main] c.a.m.B.selectByConfige - <== Total: 1
我想要达到的结果
想问下有没有什么方法让中间条件没有时,也能执行后面有值的条件?谢谢