import cn.hutool.db.Page;
public class SqlServerPageUtil {
public static String convert(String sql, Page page, String primaryKey) {
//一定要注意分页是从O页开始的
// Page page = new Page(0,2);
// sql = "select [spid] as 'SKU编码',[default_price] as '售价' from [t_item] ";
// String sql2 = "select * FROM (select ROW_NUMBER()Over(order by id desc) as rowId,* from c_class) as t where rowId between 21 and 40";
String[] str = sql.split("from", sql.length());
String selectSQL = str[0];
String fromSQL = " from ";
String tableSQL = str[1];
String modelSQL = " (select ROW_NUMBER()Over(order by " + primaryKey + " desc ) as rowId,* from " + tableSQL + " ) as t ";
String whereSQL = "where rowId between " + (page.getPageSize() * page.getPageNumber() + 1) + " and " + (page.getPageSize() * page.getPageNumber() + page.getPageSize());
String exeSql = selectSQL + fromSQL + modelSQL + whereSQL;
return exeSql;
//System.out.println(exeSql);
}
// public static void main(String[] args) {
// String sql = "select [spid] as 'SKU编码',[default_price] as '售价' from [t_item] ";
// Page page = new Page(0,2);
// convert(sql,page,"id");
// }
}
sqlServer2008版本不支持offset 分页,手动分页sql
最新推荐文章于 2024-01-30 15:32:42 发布