08_传智播客hibernate教程_hql的命名参数与Query接口的分页查询

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.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值