Linux 拨号vps windows公众号手机端

mybatis模糊查询如何防止sql注入

lewis 7年前 (2018-11-30) 阅读数 12 #VPS/云服务器
文章标签 mybatissql注入

mybatis模糊查询防止sql注入的方法:

bind + #{}模糊查询可以防止SQL注入,bind元素可以从OGNL表达式中创建一个变量并将其绑定到上下文,例如:

<selectid="selectBlogsLike"resultType="Blog">

<bindname="pattern"value="'%'+_parameter.getTitle()+'%'"/>

SELECT*FROMBLOG

WHEREtitleLIKE#{pattern}

</select>

sql:

<selectid="getInfo"resultType="cn.xm.exam.bean.haul.Haulinfo"

parameterType="hashmap">

SELECT*FROMhaulinfo

<where>

<iftest="name!=null">

<bindname="names"value="'%'+name+'%'"/>

andbignamelike#{names}

</if>

<iftest="status!=null">

andbigStatus=#{status}

</if>

</where>

</select>

java测试方法:

@Test

publicvoidtest1()throwsSQLException{

Mapcondition=newHashMap();

condition.put("name","%'andbigdescriptionlike'阳城");

condition.put("status","未开始");

testMapper.getInfo(condition);

}

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门