在userMapper.xml文件中新建映射sql的標籤 <!-- ******************** 模糊查詢的經常使用的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>