?
- If標簽:作為判斷入參來使用的,如果符合條件,則把if標簽體內的SQL拼接上。
注意:用if進行判斷是否為空時,不僅要判斷null,也要判斷空字符串‘’;
- Where標簽:會去掉條件中的第一個and符號。
?
mybatis like模糊查詢。通過if和where通過判斷可以選擇那些語句來執行,那些語句不執行,生成最終SQL語句
?
在第一個底層if判斷中,SQL語句前面加上and也可以,系統會自動去掉
<resultMap id="userByresultmap" type="user"><id property="id" column="id_"></id><result property="username" column="username_"></result><result property="birthday" column="birthday_"></result><result property="sex" column="sex_"></result><result property="address" column="address_"></result> </resultMap><select id="findUserByifwhere" parameterType="userQueryVO" resultMap="userByresultmap">select * from USER<where><if test="user!=null and user!=''"><if test="user.sex!=null and user.sex!=''"> <!—也可以寫成and sex=#{user.sex},系統會自動去掉-->sex=#{user.sex}</if><if test="user.username!=null and user.username!=''">and username LIKE "%${user.username}%"</if></if></where></select>
?