1、query也是从hibernateTemplate中获得
2、query能够设置setFirstResult、setMaxResults
3、query设置参数的方法setParameter
4、通过query.list()方法获得查询结果
具体代码如下:
public List productsNotIn(String subcategoryId, List products,
Integer offset, Integer length) throws Exception {
String queryStr = "from Product where 1=1";
if ((subcategoryId != null) && (!"".equals(subcategoryId.trim()))) {
queryStr = queryStr + " and subcategoryId = '" + subcategoryId
+ "'";
}
StringBuffer idsBuffer = new StringBuffer("");
if (!products.isEmpty()) {
int i = 0;
for (int j = products.size(); i < j; i++) {
Product product = (Product) products.get(i);
idsBuffer.append(",'").append(product.getId()).append("'");
}
}
String ids = idsBuffer.toString();
if (ids.length() > 0) {
ids = ids.substring(1);
queryStr = queryStr + " and id not in(" + ids + ")";
}
Query query = openSession().createQuery(queryStr);
query.setParameter
if (offset != null) {
query.setFirstResult(offset.intValue());
}
if (length != null) {
query.setMaxResults(length.intValue());
}
List list = query.list();
return list;
}