Mybatis中#和$的区别

来源:互联网 发布:淘宝链接转淘客链接 编辑:程序博客网 时间:2024/06/05 15:14

 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 模糊查询中,如果使用如下方式:select * from reason_detail where reason_en like '%${reason}%',此处只能使用$,使用#的话,反而会被解析为列,报错java.sql.SQLException: Column 'reason' not found