在数据库操作中都会有sql语句,而这个sql语句是 String类型的,如果用 + 来拼接,表示的是直接操作这个String 类型的字符串,这是改变了sql的具体内容了
如果用#{id},表示的是操作字改变里面字段的参数值。
用+拼接的: "select * from user where code="+code+ " and password="+password;
那么code = “1233 or code=456” password="2123":
结果: select * from user where code='123' or code='456' and password=‘2123’那么这个sql的规则就乱了
用#操作的 select * from user where code=#{code} and password=#{password};
那么code = “1233 or code=456” password="2123":
结果: select * from user where code=' 1233 or code=456 ' and password='2123'那么这个sql的规则还是老样子
总节就是,一个是你自己写规则定义sql ,一个是定义好sql 你去填写值
使用#{parameterName}引用参数的时候,Mybatis会把这个参数认为是一个字符串,例如传入参数是“xx”,那么在SQL(Select * from temp where name = #{employeeName})使用的时候就会转换为Select * from emp where name = 'xx'; 那么你想sql注入1=1之类的就起不来作用