一、前言
关于定义MySQLDialect、OracleDialect及SqlServerDialect分别实现mysql、oracle及sqlserver数据库标准Dialect分页接口,详情参见代码示例部分。
二、代码示例
1. Dialect接口
public abstract interface Dialect {@b@ @b@ public static final String SQL_END_DELIMITER = ";";@b@@b@ public abstract boolean supportPagination();@b@@b@ public abstract String getPaginationSql(String paramString, int paramInt1, int paramInt2);@b@ @b@}
2. MySQLDialect、OracleDialect和SqlServerDialect实现类
import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@@b@public class MySQLDialect implements Dialect {@b@ @b@ private final transient Logger log = LoggerFactory@b@ .getLogger(MySQLDialect.class);@b@@b@ public String getPaginationSql(String sql, int startRow, int rows) {@b@ if ((startRow < 0) || (rows < -1) || (rows == 0)) {@b@ throw new IllegalArgumentException(@b@ "MySQLDialect.getPaginationSql has been passed an illegal or inappropriate argument(startRow < 0 || endRow < startRow).");@b@ }@b@@b@ StringBuilder sqln = new StringBuilder(trim(sql));@b@ sqln.append(" LIMIT ");@b@ if (startRow == 0)@b@ sqln.append(rows);@b@ else {@b@ sqln.append(startRow).append(",").append(rows);@b@ }@b@@b@ if (this.log.isDebugEnabled())@b@ this.log.debug("MySQLDialect PaginationSql = {}", sqln);@b@@b@ return sqln.toString();@b@ }@b@@b@ public boolean supportPagination() {@b@ return true;@b@ }@b@@b@ private String trim(String sql) {@b@ String sqlt = sql.trim();@b@ if (sqlt.endsWith(";"))@b@ sqlt = sqlt.substring(0, sqlt.length() - 1 - ";".length());@b@@b@ return sqlt;@b@ }@b@}
import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@@b@public class OracleDialect implements Dialect {@b@ @b@ private final transient Logger log = LoggerFactory@b@ .getLogger(OracleDialect.class);@b@@b@ public String getPaginationSql(String sql, int startRow, int endRow) {@b@ if ((startRow < 0) || (endRow < startRow)) {@b@ throw new IllegalArgumentException(@b@ "OracleDialect.getPaginationSql has been passed an illegal or inappropriate argument(startRow < 0 || endRow < startRow).");@b@ }@b@@b@ StringBuilder sqln = new StringBuilder(@b@ "select * from (select row_.*, rownum rownum_ from (");@b@ sqln.append(trim(sql)).append(")row_ where rownum <= ").append(endRow)@b@ .append(" ) where rownum_ >= ").append(startRow);@b@ if (this.log.isDebugEnabled())@b@ this.log.debug("OracleDialect PaginationSql = {}", sqln);@b@@b@ return sqln.toString();@b@ }@b@@b@ public boolean supportPagination() {@b@ return true;@b@ }@b@@b@ private String trim(String sql) {@b@ String sqlt = sql.trim();@b@ if (sqlt.endsWith(";"))@b@ sqlt = sqlt.substring(0, sqlt.length() - 1 - ";".length());@b@@b@ return sqlt;@b@ }@b@}
import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@@b@public class SqlServerDialect implements Dialect {@b@@b@ private final transient Logger log = LoggerFactory@b@ .getLogger(SqlServerDialect.class);@b@@b@ public boolean supportPagination() {@b@ return true;@b@ }@b@@b@ public String getPaginationSql(String sql, int startRow, int rows) {@b@ if ((startRow < 0) || (rows < -1) || (rows == 0))@b@ throw new IllegalArgumentException(@b@ "SQLServerDialect.getPaginationSql has been passed an illegal or inappropriate argument(startRow < 0 || endRow < startRow).");@b@@b@ StringBuffer pagingBuilder = new StringBuffer();@b@ String sqlPartString = trim(sql);@b@ String loweredString = sqlPartString.toLowerCase();@b@ String orderby = getOrderByPart(loweredString);@b@ String distinctStr = "";@b@ if (loweredString.startsWith("select")) {@b@ int index = 6;@b@ if (loweredString.startsWith("select distinct")) {@b@ distinctStr = "DISTINCT ";@b@ index = 15;@b@ }@b@ sqlPartString = sqlPartString.substring(index);@b@ }@b@ pagingBuilder.append(sqlPartString);@b@@b@ if ((orderby == null) || (orderby.length() == 0))@b@ orderby = "ORDER BY CURRENT_TIMESTAMP";@b@@b@ StringBuffer result = new StringBuffer();@b@ result.append("WITH query AS (SELECT ").append(distinctStr)@b@ .append("TOP 100 PERCENT ").append(" ROW_NUMBER() OVER (")@b@ .append(orderby).append(") as __row_number__, ")@b@ .append(pagingBuilder)@b@ .append(") SELECT * FROM query WHERE __row_number__ BETWEEN ")@b@ .append(startRow).append(" AND ").append(startRow + rows - 1)@b@ .append(" ORDER BY __row_number__");@b@ if (this.log.isDebugEnabled())@b@ this.log.debug("SQLServerDialect PaginationSql = {}", result);@b@@b@ return result.toString();@b@ }@b@@b@ private final String getOrderByPart(String sql) {@b@ int orderByIndex = sql.indexOf("order by");@b@ if (orderByIndex != -1) {@b@ return sql.substring(orderByIndex);@b@ }@b@ return "";@b@ }@b@@b@ private String trim(String sql) {@b@ String sqlt = sql.trim();@b@ if (sqlt.endsWith(";"))@b@ sqlt = sqlt.substring(0, sqlt.length() - 1 - ";".length());@b@@b@ return sqlt;@b@ }@b@}