使用带参数的sql语句有什么优点与缺点???

默认分类 未结 1 907
_P_erhaps
_P_erhaps 2023-03-17 17:36
1条回答
  • 2023-03-17 17:55

    public User getUser(@Param("id")long id);@Select("select id,name from user where id=${id}")public User getUSer(@Param("id")long id);

    如果id传入为1,则实际sql为

    select id,name from user where id=‘1‘select id,name from user where id=1

    Mybaits方法有一种情况

    @Select("select id,name from user where id=#{id}")public User getUser(@Param("id") long id);@Select("select id,name from user where id=#{id}")public User getUser(long id);

    以上两种都可以,因为传一个参数是可以省略@Param("")的,但是这种情况下不能使用${},

    传两个参数以上时,必须要写@Param("")

    带参sql$和#的区别

    标签:

    优点:不用关心语句的单引号的问题了 ,令外可以有效的防止SQL注入的非法入侵,这样写程序在编译的时候就把那语句编译了,不会与其它字符匹配了,这就是防止SQL注入的问题了, 唯一的缺点就是占用系统资源的问题了,因为它是早被预编译好的东西,所以系统在调用的时候是直接使用的,不需要再次进行对SQL语句进行编译了,如果项目小的话,少量的这样的代码可以不用计较资源的问题了

    这个事必须地吧有时候需要参数

    我觉得带参数的SQL,更安全,不会因为像'这种单引号引起sql语句错误或者sql注入带参数可能稍微得多写几句代码了。

    0 讨论(0)
提交回复