首页>>帮助中心>>香港VPS的mybatis模糊查询如何防止sql注入

香港VPS的mybatis模糊查询如何防止sql注入

2023/5/24 556次

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

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

<select id="selectBlogsLike" resultType="Blog">

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

SELECT * FROM BLOG

WHERE title LIKE #{pattern}

</select>

sql

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

parameterType="hashmap">

SELECT * FROM haulinfo

<where>

<if test="name != null">

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

and bigname like #{names}

</if>

<if test="status != null">

and bigStatus = #{status}

</if>

</where>

</select>

java测试方法:

@Test

public void test1() throws SQLException {

Map condition = new HashMap();

condition.put("name", "%' and bigdescription like '阳城");

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

testMapper.getInfo(condition);

}

一诺网络香港免备案专区,提供「香港增强VPS」和「香港特惠VPS」两种类型的高可用弹性计算服务,搭载新一代英特尔®至强®铂金处理器,接入CN2低延时高速回国带宽线路,网络访问顺滑、流畅。机房网络架构采用了BGP协议的解决方案可提供多线路互联融合网络,使得不同网络运营商线路的用户都能通过最佳路由实现快速访问。