1. 程式人生 > >Mybatis 動態sql if標籤 傳參的問題

Mybatis 動態sql if標籤 傳參的問題

關鍵程式碼:

//模糊查詢先給兩個條件

public List<Flower> findFlower(@Param("name1")String name,@Param("price1")double price);

<select id="findFlower" resultMap="findFlowermapper">
select id,name,price,f.pid,pname 
from t_flower f
join t_production p
on f.pid = p.pid
<where>
<if test="name1 != null and !''.equals(name1)">
and f.name like '%' #{name1} '%'
</if>
<if test="price1

!= 0.0">
and f.price > #{price1}
</if>
</where>
</select>

以後大家碰到這種傳參而且需要判斷情況,可以用這種方法來替代索引:#{0},#{1}