sql语句转化为分页查询的一种实现

版权声明:本文为博主原创文章,无需授权即可转载,甚至无需保留以上版权声明,转载时请务必注明作者。 https://blog.csdn.net/lmb55/article/details/50516285
/**
     * 将sql语句转化为分页查询
     *
     * */
    public static String addfy4oracle(String sql, int start, int limit, List args,String order,
            String ascOrdesc ) {
        StringBuffer strB = new StringBuffer(sql);
        StringBuffer orderby = new StringBuffer();
        if (order != null) {
            orderby.append(" ORDER BY ").append(order).append(" ").append(
                    ascOrdesc);
            // args.add(order);//如果order写成?,会使排序无效
        }
        if (start > 0 || limit > 0) {
            strB.insert(0, "SELECT * FROM ( SELECT temp.* ,ROWNUM num  FROM (");
            strB.append(orderby);
            strB.append(") temp ");
            if (limit > 0) {
                if (start < 0) {
                    start = 0;
                }
                strB.append(" WHERE ROWNUM <= ? )");
                args.add(start + limit);
            }
            if (start > 0) {
                strB.append(" where num > ? ");
                args.add(start);
            }
        }
//      log.debug("strB::" + strB.toString());
        return  strB.toString();
    }
展开阅读全文

没有更多推荐了,返回首页