区分 #{} 和 ${}的不同应用场景
1)#{} 会生成预编译SQL,会正确的处理数据的类型,而KaTeX parse error: Expected 'EOF', got '#' at position 70: …'S123456'; 如果使用#̲{} 那么生成的SQL为: s…{}
那么生成的SQL为:select * from student where xCode = S123456
如果xCode的数据类型为varchar,那么使用${}就会报错。
2)${}一般用在order by, limit, group by等场所。
假设我们使用#{} 来指定order by字段,比如
select * from student order by #{xCode},
那么产生的SQL为
select * from student order by ?, 替换值后为
select * from student order by ‘xCode’
Mybatis对xCode加了引号导致排序失败
一般${}用在我们能够确定值的地方,也就是我们程序员自己赋值的地方。
而#{}一般用在用户输入值的地方!!