mybatis #{}和${}的区别

为了实现

select * from xd_lottery where begin_time/draw_time between xx and xxx;

我写了这个mapper.xml

<select id="selectLotteryByTime" resultMap="BaseResultMap">

select

<include refid="Base_Column_List"/>

from xd_lottery

where #{type} between #{startTime} and #{endTime}

</select>

 

被坑了一波。。

光知道

${}是替换

#{}是占位符

 

#因为不会动态注入,所以可以防注入,推荐优先使用这个

 

然后这句话就拿到了 empty set

 

但是不知道的是

#{}会给那个值带上引号,导致实际上我的语句是:

select * from xd_lottery where 'begin_time' between from_unixtime(1573466400) and from_unixtime(1573473600);

这句话拿到mysql.client 得到的结果是:

Empty set, 2 warnings (0.05 sec)

 

为啥'begin_time' 作为column没有报错呢?原因是 正常where是选择column 满足后面的条件的。

但是对于这个表达式

是where后面是一个表达式 : 'begin_time' between from_unixtime(1573466400) and from_unixtime(1573473600);

这个表达式只有两个结果 true or false

如果是true 则返回全部的Result。如果是false 则返回empty

 

同样可以用 select * from xd_lottery where 2 between 1 and 3; 2 between 1 and 3也是true。

 

好了现在就清楚为啥 where #{type} between #{startTime} and #{endTime} 拿到的结果是empty了。所以改一下应该是

 

<select id="selectLotteryByTime" resultMap="BaseResultMap">

select

<include refid="Base_Column_List"/>

from xd_lottery

where ${type} between #{startTime} and #{endTime}

</select>

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值