为了实现
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>