我的presto版本不支持用offset,还有别的办法吗,比如这段代码: private static QueryAndValues generateQuery(Map<String, String> parameters) { Timer time = new Timer(); StringBuilder query = new StringBuilder("SELECT * FROM " // "( SELECT temp.*, ROW_NUMBER() OVER (ORDER BY cta_onb) AS rownum FROM ( SELECT * FROM " ).append(TABLES); List<Object> paramValues = new ArrayList<>(); boolean firstCondition = true; for (String key : parameters.keySet()) { // let's assume the keys that end with "_double" should be treated as double boolean isDouble = key.endsWith("_double"); if (!"page".equals(key) && !"size".equals(key)) { String value = parameters.get(key); if (value == null || value.isEmpty()) { query.append(firstCondition ? " WHERE " : " AND ").append(key).append(" IS NULL "); firstCondition = false; } else { if (value.contains(",")) { String[] values = value.split(","); query.append(firstCondition ? " WHERE " : " AND ").append(key).append(" IN ("); for (String val : values) { query.append("?,"); paramValues.add(isDouble ? Double.parseDouble(val) : val); } query.setLength(query.length() - 1); query.append(") "); firstCondition = false; } else { query.append(firstCondition ? " WHERE " : " AND ").append(key).append(" = ? "); paramValues.add(isDouble ? Double.parseDouble(value) : value); firstCondition = false; } } } } System.out.println("QueryAndValues 消费时间:"+ time.getElapsedTims()); int size = Integer.parseInt(parameters.get("size")); int page = Integer.parseInt(parameters.get("page")); query.append(" limit 500"); // query.append(") AS temp ) AS temp2 WHERE rownum BETWEEN ? AND ?"); // paramValues.add((page - 1) * size + 1); // Start index // paramValues.add(page * size); // End index return new QueryAndValues(query.toString(), paramValues); },用hibrenate做分页可以实现吗,如果可以,代码应该怎么修改,请附上完整代码
时间: 2024-03-29 08:39:49 浏览: 133
datax支持presto读取
可以使用 Hibernate 来实现分页。下面是修改后的代码:
private static QueryAndValues generateQuery(Map<String, String> parameters) {
Timer time = new Timer();
StringBuilder query = new StringBuilder("FROM " + TABLES);
boolean firstCondition = true;
for (String key : parameters.keySet()) {
// let's assume the keys that end with "_double" should be treated as double
boolean isDouble = key.endsWith("_double");
if (!"page".equals(key) && !"size".equals(key)) {
String value = parameters.get(key);
if (value == null || value.isEmpty()) {
query.append(firstCondition ? " WHERE " : " AND ").append(key).append(" IS NULL ");
firstCondition = false;
} else {
if (value.contains(",")) {
String[] values = value.split(",");
query.append(firstCondition ? " WHERE " : " AND ").append(key).append(" IN (");
for (String val : values) {
query.append("?,");
paramValues.add(isDouble ? Double.parseDouble(val) : val);
}
query.setLength(query.length() - 1);
query.append(") ");
firstCondition = false;
} else {
query.append(firstCondition ? " WHERE " : " AND ").append(key).append(" = ? ");
paramValues.add(isDouble ? Double.parseDouble(value) : value);
firstCondition = false;
}
}
}
}
Session session = sessionFactory.openSession();
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Entity> criteriaQuery = builder.createQuery(Entity.class);
Root<Entity> root = criteriaQuery.from(Entity.class);
criteriaQuery.select(root).where(builder.and(paramValues.toArray(new Predicate[0])));
Query<Entity> queryObj = session.createQuery(criteriaQuery);
int size = Integer.parseInt(parameters.get("size"));
int page = Integer.parseInt(parameters.get("page"));
int start = (page - 1) * size;
queryObj.setFirstResult(start);
queryObj.setMaxResults(size);
List<Entity> results = queryObj.getResultList();
System.out.println("QueryAndValues 消费时间:" + time.getElapsedTims());
return new QueryAndValues(results, start, size);
}
注意:你需要将 Entity.class 替换成你的实体类,并在代码中添加相应的 import 语句。此外,你还需要在代码中添加 Hibernate 的配置信息和 sessionFactory 的初始化代码。
阅读全文