mybatis動態(tài)SQL下進行模糊查詢的幾種方式,其中兩個存疑
1.傳參時進行%%的拼接,需要進行null判斷,因為動態(tài)sql的if標簽判斷會由于%null%失效
1 <select id="findByCondition02" parameterType="map" resultType="Brand"> 2 select * from tb_brand 3 <where> 4 <if test="status!=null"> 5 status =#{status} 6 </if> 7 <if test="companyName!=null"> 8 2.拼接單引號 注意加空格 9 10 and company_name like '%' #{companyName} '%' 11 </if> 12 <if test="brandName!=null"> 13 3.拼接雙引號 14 and brand_name like "%"#{brandName}"%"; 15 </if> 16 </where> 17 </select>
4.使用concat函數(shù)進行拼接
非常不推薦使用'%${}%'進行拼接,有SQL注入的風險

浙公網(wǎng)安備 33010602011771號