如何实现模糊查询LIKE
生活随笔
收集整理的這篇文章主要介紹了
如何实现模糊查询LIKE
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、字符串拼接
在Java 代碼中拼接%%(比如name = "%" + name + "%"; ),直接LIKE。因為沒有預編譯,存在SQL 注入的風險,不推薦使用。
2、CONCAT(推薦)
<when test="empName != null and empName != ''">AND e.emp_name LIKE CONCAT(CONCAT('%', #{emp_name, jdbcType=VARCHAR}),'%') </when>3、bind 標簽
<select id="getEmpList_bind" resultType="empResultMap" parameterType="Employee"><bind name="pattern1" value="'%' + empName + '%'" /><bind name="pattern2" value="'%' + email + '%'" />SELECT * FROM tbl_emp<where><if test="empId != null">emp_id = #{empId,jdbcType=INTEGER},</if><if test="empName != null and empName != ''">AND emp_name LIKE #{pattern1}</if><if test="email != null and email != ''">AND email LIKE #{pattern2}</if></where>ORDER BY emp_id </select>?
總結
以上是生活随笔為你收集整理的如何实现模糊查询LIKE的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么获取插入的最新自动生成的ID
- 下一篇: 什么时候用#{},什么时候用${}?