小结一下hibernate占位符.

1.最常见的?占位符.

String hql = "select a from Apple a where a.color=? a.weight>?";
Query query = session.createQuery(hql);
query.setParameter(0, "red");
query.setParameter(1, "10");

下标从0开始,最常见的.这个让人头疼的是数?个数…

2.以一个变量名的形式占位.

String hql = "select a from Apple a where a.color=:pcolor a.weight>:pweight";
Query query = session.createQuery(hql);
query.setParameter("pcolor", "red");
query.setParameter("pweight", "10");

这个就不存在数?个数的问题了.应该是比较方便的一种方法了

3.JPA方式,这种方式是1的改良版本..

String hql = "select a from Apple a where a.color=?2 a.weight>?5";
Query query = session.createQuery(hql);
query.setParameter("2", "red");
query.setParameter("5", "10");

方法1中的?的索引可以自己随意任命了..

版权声明:本文为xinglongbing521原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xinglongbing521/p/10495968.html