日期:2014-05-16  浏览次数:20821 次

生成MySQL、SqlServer、Oracle数据的分页语句

/**
* @Title: getMySQLSql 
* @Description: 生成适合MySQL方言的SQL分页语句
* @param  sql 
* @param  offset
* @param  limit
* @return String    
 */
public String getMySQLSql(String sql, int offset, int limit) {
   return sql + " limit " + offset + "," + limit;
}

/**
* @Title: getSqlServeSql 
* @Description: 生成适合SqlServer方言的SQL分页语句
* @param  sql 
* @param  pageNo
* @param  pageSize
* @return String    
 */
public String getSqlServeSql(String sql, int pageNo, int pageSize) {
  return "select top " + pageSize + " from (" + sql
	+ ") t where t.id not in (select top " + (pageNo-1)*pageSize + " t1.id 
		  from ("+ sql + ") t1)";
}

/**
* @Title: getOrcaleSql 
* @Description: 生成适合Oracle方言的SQL分页语句
* @param  sql 
* @param  pageNo
* @param  pageSize
* @return String    
 */
public String getOrcaleSql(String sql, int pageNo, int pageSize) {
   return "select * from (select rownum rn, t.* from (" + sql
		  + ") t where rownum <= " + (pageNo* pageSize)
		  + ") t1 where t1.rn > " + ((pageNo- 1) * pageSize);
}