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);
}
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: mybatis一对多添加数据怎么实现