<!-- ******************** 模糊查詢的常用的3種方式:********************* -->
<select id="getUsersByFuzzyQuery" parameterType="User" resultType="User">
select <include refid="columns"/> from users
<where>
<!--
方法一: 直接使用 % 拼接字符串
注意:此處不能寫成 "%#{name}%" ,#{name}就成了字符串的一部分,
會發生這樣一個異常: The error occurred while setting parameters,
應該寫成: "%"#{name}"%",即#{name}是一個整體,前後加上%
-->
<if test="name != null">
name like "%"#{name}"%"
</if>
<!--方法二: 使用concat(str1,str2)函數將兩個參數連接 -->
<if test="phone != null">
and phone like concat(concat("%",#{phone}),"%")
</if>
<!--方法三: 使用 bind 標籤,對字符串進行綁定,然後對綁定後的字符串使用 like 關鍵字進行模糊查詢 -->
<if test="email != null">
<bind name="pattern" value="'%'+email+'%'"/>
and email like #{pattern}
</if>
</where>
</select>
mybatis再xml中模糊查詢的常用的3種方式
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.