在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>