String p_dbmsType = "oracle";// 数据库类型
String pageSql = "";
try {
p_dbmsType = Platform.getPlatform().getDbTpye();
} catch (Exception e) {
} finally {
// 无法读取默认为oracle数据库
if (p_dbmsType == null || "".equals(p_dbmsType)) {
p_dbmsType = "oracle";
}
}
if (p_dbmsType.toUpperCase().equals("ORACLE")) {
int rowCount = start + limit;
// 组装分页sql
StringBuilder sb = new StringBuilder(
"select * from ( select row_.*, rownum rownum_ from( ");
sb.append(sql);
sb.append(" ) row_ where rownum <=").append(rowCount)
.append(") where rownum_ > ").append(start);
pageSql = sb.toString();
}// oralce
else if (p_dbmsType.toUpperCase().equals("SQLSERVER")) {
/** 替换sql为可分组方式 */
int rowCount = start + limit;
sql = sql.replaceFirst("(?i)select", "select top " + rowCount
+ " tempColumn=0,");
/** 编辑为分页sql */
StringBuilder sb = new StringBuilder();
sb.append(" select * from ( ")
.append(" select row_number()over(order by tempColumn)tempRowNumber,* from ( ")
.append(sql).append(" )t ").append(" )tt ")
.append(" where tempRowNumber>").append(start);
pageSql = sb.toString();
}// sqlserver
else if (p_dbmsType.toUpperCase().equals("MYSQL")) {
StringBuilder sb = new StringBuilder();
sb.append(sql);
sb.append(" LIMIT ").append(start).append(",").append(limit);
pageSql = sb.toString();
}
return pageSql;
}
数据库分页sql
最新推荐文章于 2024-10-09 15:04:16 发布