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"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值