Mybatis是如何实现防止SQL注入
Q:mybatis框架里 $ 和 # 的区别? A: 1 #是将传入的值当做字符串的形式,eg:select id,name,age from student where id =#{id},当前端把id值1,传入到后台的时候,就相当于 select id,name,age from student where id ='1'. 2 $是将传入的数据直接显示生成sql语句,eg:select id,name,age from student where id =${id},当前端把id值1,传入到后台的时候,就相当于 select id,name,age from student where id = 1. 3 使用#可以很大程度上防止sql注入。(语句的拼接) 4 但是如果使用在order by 中就需要使用 $. 5 在大多数情况下还是经常使用#,但在不同情况下必须使用$. 我觉得#与 的区别最大在于:#{} 传入值时,sql解析时,参数是带引号的,而 的区别最大在于:#{} 传入值时,sql解析时,参数是带引号的,而${}穿入值,sql解析时,参数是不带引号的。 什么是SQL注入 在讨论怎么实现之前,首先了解一下什么是SQL注入,我们有一个简单的查询操作:根据id查询一个用户信息。它的sql语句应该是这样: select * from user where id =