MyBatis實現模糊查詢的幾種方式

 

  在學習MyBatis過程當中想實現模糊查詢,惋惜失敗了。後來上百度上查了一下,算是解決了。記錄一下MyBatis實現模糊查詢的幾種方式。
  數據庫表名爲test_student,初始化了幾條記錄,如圖:
  數據庫表內容
  
  起初我在MyBatis的mapper文件中是這樣寫的: sql

<select id="searchStudents" resultType="com.example.entity.StudentEntity"
        parameterType="com.example.entity.StudentEntity">
        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 '%#{name}%'
            </if>
            <if test="address != null and address != ''">
                AND address LIKE '%#{address}%'
            </if>
        </where>
        ORDER BY id
    </select>

寫完後自我感受良好,很開心的就去跑程序了,結果固然是報錯了:
報錯數據庫

  經百度得知,這麼寫經MyBatis轉換後(‘%#{name}%’)會變爲(‘%?%’),而(‘%?%’)會被看做是一個字符串,因此Java代碼在執行找不到用於匹配參數的 ‘?’ ,而後就報錯了。app

解決方法

1.用${…}代替#{…}函數

<select id="searchStudents" resultType="com.example.entity.StudentEntity"
        parameterType="com.example.entity.StudentEntity">
        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 '%${name}%'
            </if>
            <if test="address != null and address != ''">
                AND address LIKE '%${address}%'
            </if>
        </where>
        ORDER BY id
    </select>

查詢結果以下圖:
查詢結果學習

  注:使用${…}不能有效防止SQL注入,因此這種方式雖然簡單可是不推薦使用!!!spa

2.把’%#{name}%’改成」%」#{name}」%」code

<select id="searchStudents" resultType="com.example.entity.StudentEntity"
        parameterType="com.example.entity.StudentEntity">
        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 "%"#{name}"%"
            </if>
            <if test="address != null and address != ''">
                AND address LIKE "%"#{address}"%"
            </if>
        </where>
        ORDER BY id
    </select>

查詢結果:
查詢結果blog

3.使用sql中的字符串拼接函數ip

<select id="searchStudents" resultType="com.example.entity.StudentEntity"
        parameterType="com.example.entity.StudentEntity">
        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 CONCAT(CONCAT('%',#{name},'%'))
            </if>
            <if test="address != null and address != ''">
                AND address LIKE CONCAT(CONCAT('%',#{address},'%'))
            </if>
        </where>
        ORDER BY id
    </select>

查詢結果:
查詢結果字符串

4.使用標籤

<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代碼中拼接字符串
  這個方法沒試過,就不貼代碼和結果了。

————2017.07.03

相關文章
相關標籤/搜索