W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
方式1:$ 這種方式,簡單,但是無法防止SQL注入,所以不推薦使用
LIKE '%${name}%'
方式2:#
LIKE "%"#{name}"%"
有興趣的可以看一下:Mybatis 中#{} 和${}區(qū)別
方式3:字符串拼接
AND name LIKE CONCAT(CONCAT('%',#{name},'%'))
方式4:bind標簽
<select id="searchStudents" resultType="com.example.entity.StudentEntity"
parameterType="com.example.entity.StudentEntity">
<bind name="pattern1" value="'%' + _parameter.name + '%'" />
<bind name="pattern2" value="'%' + _parameter.address + '%'" />
SELECT * FROM test_student
<where>
<if test="age != null and age != '' and compare != null and compare != ''">
age
${compare}
#{age}
</if>
<if test="name != null and name != ''">
AND name LIKE #{pattern1}
</if>
<if test="address != null and address != ''">
AND address LIKE #{pattern2}
</if>
</where>
ORDER BY id
</select>
方式5:java代碼里寫
param.setUsername("%CD%"); 在 java 代碼中傳參的時候直接寫上
<if test="username!=null"> AND username LIKE #{username}</if>
然后 mapper 里面直接寫 #{} 就可以了
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: