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

/**
     * 将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();
    }

你可能感兴趣的:(数据库)