1.用hibernate建议的命名参数方法(推荐):
List<Student> list = session.createQuery("from Student where dept=:sdept and age>:sage and sex=:sex")
.setString("sdept", "计算机学院")
.setParameter("sex", "男")
.setInteger("sage", 20)
.list();
2.用已经废弃的方法:
List<Student> list = session.createQuery("from Student as s where s.age>? and s.dept=?")
.setInteger(0, 20)
.setString(1, "计算机学院")
.list();
3.JPA-Style占位符方法:
List<Student> list = session.createQuery("from Student as s where s.age>?0 and s.dept=?1")
.setInteger(0, 20)
.setString(1, "计软院")
.list();//参数绑定
此方法试过多次未果,都会抛出异常:
java.lang.IllegalArgumentException: No positional parameters in query: from Student where age>?0 and dept=?1
Hibernate中设置query参数的方法
最新推荐文章于 2024-06-10 18:46:56 发布