-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Use_PageUtils
高铁 edited this page Apr 19, 2017
·
7 revisions
PageUtils是基于Druid SQL Parser实现的分页SQL自动生成类。目前支持MySql、Oracle、DB2、SQL Server四种数据库类型。
package com.alibaba.druid.sql;
public class PagerUtils {
// 生成获取count的sql
public static String count(String sql, String dbType) { }
// 生成分页取数的sql
public static String limit(String sql, String dbType, int offset, int count) { }
}
import com.alibaba.druid.sql.PagerUtils;
import com.alibaba.druid.util.JdbcConstants;
String sql = "select id, name from t order by id";
String result = PagerUtils.count(sql, JdbcConstants.MYSQL); // 这里的dbType是MySql,可以更换为Oracle、DB2和SQL_Server
输出结果
SELECT COUNT(*)
FROM t
count方法能够自动去掉selectList上字段,也能够去掉order by,能够自动识别union
String sql = "select * from t";
String result = PagerUtils.limit(sql, JdbcConstants.ORACLE, 20, 10);
输出结果
SELECT *
FROM (SELECT XX.*, ROWNUM AS RN
FROM (SELECT *
FROM t
) XX
WHERE ROWNUM <= 30
) XXX
WHERE RN > 20
String sql = " select * from test t limit 3";
assertTrue(PagerUtils.hasUnorderedLimit(sql, JdbcConstants.MYSQL));