假设我们只需要从表中获取 5 条记录,但我的 where 子句匹配数据库中的 25k 条记录。那么在 ofbiz
框架中有没有一种方法可以只选择 5 条记录,而不是从数据库中获取一个列表,然后只从列表中取出 5 条记录?
如果无法达到限制(因为 ofbiz
API 与数据库无关),我还有哪些其他选择?
最佳答案
我建议你看看这个 entity engine cookbook
主要是为了从数据库中获取有限的行集,你会这样做:
// first get a list iterator
productsELI = delegator.findListIteratorByCondition("Product",
new EntityExpr("productId", EntityOperator.NOT_EQUAL, null),
UtilMisc.toList("productId"), null);
// then get a partial list by count TO RETURN first 5 records
productsELI.getPartialList(0, 5);
// and finally just close the iterator
productsELI.close();
此外,如果您希望将直接 SQL 发送到您的数据库,那么只需这样做:
// gets the helper (localmysql, localpostgres, etc.) for your entity group org.ofbiz
String helperName = delegator.getGroupHelperName("org.ofbiz");
SQLProcessor sqlproc = new SQLProcessor(helperName);
sqlproc.prepareStatement("SELECT * FROM PARTY LMIT 0, 5");
ResultSet rs1 = sqlproc.executeQuery();
// and then get your data from ResultSet like regular JDBC
关于sql - Apache OFBiz delegator API 是否支持通过限制或偏移量从数据库中获取记录列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5410461/