hibernate的setParameter()方法有什么用?

来源:百度知道 编辑:UC知道 时间:2024/06/17 19:48:53
如题,是hibernate的,不是request对象的....
如果我先定义一个String变量name,然后用from Customer o where o.name='"+ name + "';那么这样就可以不用setParameter()了吧??

  setParameter() 方法设置一个样式表参数。

  说明:这个方法为指定的样式表参数指定一个值。

  语法::setParameter(namespaceURI,localName,value)

  

可以这么使用。
但容易引出SQL注入漏洞,不推荐使用!

在编程时,都不推荐直接SQL语句的变量参数通过字符串拼接来实现,因为如果被人利用注入漏洞,可以把你的数据库都轻易破解了。
而用setParameter的方法则是安全的,没有注入漏洞,可以放心使用。

设置你的查询参数用的。

比如 "from Customer o where o.name=?1"

setParameter(1,"java");
=====================

可以,但不建议,因