1 命名参数
Bind parameters
Methods on Query are provided for binding values to named parameters or JDBC-style ?
parameters. Contrary to JDBC, Hibernate numbers parameters from zero. Named parameters
are identifiers of the form :name in the query string. The advantages of named parameters are
as follows:
• named parameters are insensitive to the order they occur in the query string
不受参数顺序的制约
• they can occur multiple times in the same query
在一个查询中可以出现多次
• they are self-documenting (是说可读性? 不是很理解)
//named parameter (preferred)
Query q = sess.createQuery("from DomesticCat cat where cat.name = :name");
q.setString("name", "Fritz");
Iterator cats = q.iterate();
//positional parameter
Query q = sess.createQuery("from DomesticCat cat where cat.name = ?");
q.setString(0, "Izi");
Iterator cats = q.iterate();
//named parameter list
List names = new ArrayList();
names.add("Izi");
names.add("Fritz");
Query q = sess.createQuery("from DomesticCat cat where cat.name in (:namesList)");
q.setParameterList("namesList", names);
List cats = q.list();
2 分页查询
不同数据库 的分页关键字是不同的,根据配置文件的方言设定值,hibernate自动生成分叶语句
这样就可以跨数据库。
Pagination
If you need to specify bounds upon your result set, that is, the maximum number of rows you want
to retrieve and/or the first row you want to retrieve, you can use methods of the Query interface:
Query q = sess.createQuery("from DomesticCat cat");
如果参数是0的话,那么是从第一条记录开始拿
q.setFirstResult(20);
q.setMaxResults(10);
List cats = q.list();
Hibernate knows how to translate this limit query into the native SQL of your DBMS.
本文介绍了使用 Hibernate 进行数据库查询的两种方法:命名参数查询和位置参数查询,并详细讲解了命名参数的优势及其应用场景,同时探讨了如何实现分页查询。
1827

被折叠的 条评论
为什么被折叠?



