mybatis模糊查詢語句及注意事項

<select id="queryCount" resultType="int">
		select count(*)
		from t_user
		<where>
			<if test="queryText!=null">loginacct like concat("%",#{queryText},"%")</if>
		</where>
	</select>

1.動態查詢語句

2.SQL中佔位符不能在單引號中,否則,會以?進行查詢數據

'%#{param}%'

'%?%'

3.SQL中不能使用加號進行字符串拼接,加號是用來做運算的

'%'+'D'+'%'

4.MyBatis進行拼串,拼串會出現  SQL 注入情況 ,例如:“or 1=1”

'%${param}%'

5.使用內置方法進行拼串

concat('%',#{param},'%')

6.查詢條件值本身爲%,查詢出所有的數據

concat('%',#{param},'%') => '%%%'

 '%\%%'  使用轉譯字符再進行查詢。

7.#和\是一個意思,表示轉譯。使用#代替\

select * from t_user where username like '%#%%' escape '#'

select * from t_user where loginacct like '%@%%' escape '@'

SELECT * FROM t_user WHERE loginacct LIKE concat('%','@%','%') ESCAPE '@'

 

8.常見的SQL文,在Oracle中,使用兩個豎線用來表示字符串拼接,MySQL中沒有這樣的語法。

select * from t_user where username like '%'|| #{param} ||'%'

 

  • SQL參數問題

    <select id="queryCount" resultType="int">

        select count(*) from t_user

        <where>

                <if test="queryText!=null">

                loginacct like '%#{queryText}%'

                </if>

        </where>

    </select>

 

    <select id="pageQuery" resultType="User">

        select * from t_user

         <where>

                <if test="queryText!=null">

                loginacct like '%#{queryText}%'

                </if>

        </where>

        limit #{start}, #{size}

    </select>

  • 有3個參數,但是隻是指定了2個.
  • Select * from t_user where loginacct like '%#{loginacct}%' limit?,?

org.springframework.dao.TransientDataAccessResourceException:

### Error querying database.  Cause: java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).

### The error may exist in URL [jar:file:/F:/atcrowdfunding/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/atcrowdfunding-main/WEB-INF/lib/atcrowdfunding-user-0.0.1-SNAPSHOT.jar!/mybatis/mapper-user.xml]

### The error may involve defaultParameterMap

### The error occurred while setting parameters

### SQL: select * from t_user           WHERE loginacct like '%?%'          limit ?, ?

### Cause: java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).

; SQL []; Parameter index out of range (3 > number of parameters, which is 2).; nested exception is java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).

  • SQL注入問題 :

Id = 100 OR 1=1

SELECT * FROM t_user WHERE id= ${id}

SELECT * FROM t_user WHERE id=  100 OR 1=1

 

在特定場合可以使用${}:

例如:

  • Create table ${tableName} …      //表名稱位置不能使用?佔位符,所以也就不能使用#{}
  • Order by ${fieldName} asc   //對字段進行排序,可以 傳遞動態字段名稱.

    <select id="queryCount" resultType="int">

        select count(*) from t_user

        <where>

                <if test="queryText!=null">

                loginacct like '%${queryText}%'

                </if>

        </where>

    </select>

 

    <select id="pageQuery" resultType="User">

        select * from t_user

         <where>

                <if test="queryText!=null">

                loginacct like '%${queryText}%'

                </if>

        </where>

        limit #{start}, #{size}

    </select>

  • SQL拼接問題
    • 不能使用加號拼接

  • 使用concat()函數拼接字符串

    <select id="queryCount" resultType="int">

        select count(*) from t_user

        <where>

                <if test="queryText!=null">

                loginacct like concat('%',#{queryText},'%')

                </if>

        </where>

    </select>

 

    <select id="pageQuery" resultType="User">

        select * from t_user

         <where>

                <if test="queryText!=null">

                loginacct like concat('%',#{queryText},'%')

                </if>

        </where>

        limit #{start}, #{size}

    </select>

  • 查詢關鍵字爲% 和 \
    • 查詢關鍵字爲%將數據都查詢出來了,不安全.
    • 對查詢的特殊符號進行轉譯.

  • 解決:
    • Java中轉譯

String queryText = "%";

if(StringUtil.isNotEmpty(queryText)){

 //斜線本身需要轉譯,regex中兩個\\表示一個\ ; Java中也是兩個\\表示一個\;所以,需要四個斜線

queryText = queryText.replaceAll("%", "\\\\%");

System.out.println("--------------"+queryText);                        

}

  • SQL語句中轉譯
    • 對特殊符號進行轉譯;斜槓本身也屬於特殊符號,需要轉譯.

select * from t_user where loginacct like '%\\%%'

  • 注意:
    • 儘量在保存數據時,驗證數據的合法性,儘量避免存在的字符串中含有特殊符號.
    • 但是,有時無法避免,則需要進行特殊處理:

例如:

"<<合同編號[101]文件>>"

"D:\\atguigu"

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章