前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SSM框架的sql中参数注入(#和$的区别)

SSM框架的sql中参数注入(#和$的区别)

作者头像
Krry
发布2018-09-10 17:47:19
7690
发布2018-09-10 17:47:19
举报
文章被收录于专栏:Krryblog
代码语言:javascript
复制
 1 <select id="findUsersByUserName2" resultType="java.util.Map" parameterType="Params">
 2         SELECT
 3             id as uid,
 4             username as uname,
 5             password as pwd,
 6             account as act,
 7             telephone,
 8             idcard,
 9             create_time as createTime,
10             is_delete as isDelete,
11             male,
12             birthday,
13             email,
14             address,
15             update_time as updateTime,
16             teacher_id as teacherId,
17             subject_id as subjectId,
18             age,
19             status,
20             type
21         FROM
22             tz_user
23         WHERE
24             username LIKE '%${username}%'
25         AND `password` = #{password}
26         ORDER BY ${order}
27 </select>

ORDER BY ${order} 和模糊查询 username LIKE '%${username}%' 是用$符号,其他的大多是用 #{} 来获取传递的参数。

ORDER BY 还可以用#{}符号传递参数。

 #{} 将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号。如:order by #{userId},如果传入的值是111, 那么解析成sql时的值为order by "111", 如果传入的值是id,则解析成的sql为order by "id".

 ${} 将传入的数据直接显示生成在sql中,是什么就是什么,没有加双引号:select * from table1 where id=${id}   若 id = 4,则就是:select * from table1 where id = 4;

最好是能用 #{} 就用它,因为它可以防止sql注入,且是预编译的,在需要原样输出时才使用 ${} 

记住一点:单引号里面的用 ${} 符号,ORDER BY 可以用${}或者#{}符号,用 #{} 的不能加单引号,因为默认加了引号

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-03-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档