mybatis的#{}和${}的区别以及order by注入问题

一、问题

根据前端传过来的表格排序字段和排序方式,后端使用的mybaits

select XXXX from table order by #{column} #{desc}

如上面的形式发现排序没有生效,查看打印的日志发现实际执行的sql为,排序没有生效

select XXXXX from table order by "column" "desc"
二、原因分析

主要还是对mybatis传参形式不了解,官方介绍如下:

By default, using the #{} syntax will cause MyBatis to generate PreparedStatement properties and set the values safely against the PreparedStatement parameters (e.g. ?). While this is safer, faster and almost always preferred, sometimes you just want to directly inject an unmodified string into the SQL Statement. For example, for ORDER BY, you might use something like this:
@Select("select * from user where ${column} = #{value}")
User findByColumn(@Param("column") String column, @Param("value") String value);

从官方文档中可以看出#{}相当于jdbc中的preparedstatement,进行了预编译,而${}直接是字符串本身,是有意设计成这样,方便拼接成动态sql,但是这样也带来缺点,可能存在注入问题。

参考阅读:http://www.mybatis.org/mybatis-3/sqlmap-xml.html#Parameters

转载于:https://www.cnblogs.com/xieshuang/p/11017121.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值