Java中oracle分页查询01

在数据库汇中的查询语句:oracle中使用rownum进行分页,从每页在总记录数的第n条到第m条;这里的是每页10条记录,下面sql所查询的是第页的记录数;oracle的分页是从1开始的,这里写成rownum>=0,rownum<=9,查询的记录是9条。

SELECT * FROM (SELECT ROW_.*, ROWNUM ROWNUM_
          FROM (SELECT *
                  FROM TABLE1
                 WHERE TABLE1_ID = XX
                 ORDER BY GMT_CREATE DESC) ROW_
         WHERE ROWNUM <= 20)
 WHERE ROWNUM_ >= 11;

一般项目从页面传递的数据是:page,pagesize;写成sql语句:

select * from (select rownum rn,a.* from A_MODEL a where rownum<=page*pageSize)aa where aa.rn>(page-1)*pageSize

下面是在实际项目的一条sql语句:值得注意的是在mybatis中不能用<,而要用转义字符(&lt;)

<select id="queryLossModelList" resultMap="BaseResultMap">
        SELECT aaa.*,s.MIN_NAME FROM (SELECT aa.*
        FROM
        (SELECT rownum rn,a.*
        FROM A_MODEL_LOSS a
        WHERE 1=1
        <choose>
            <when test="cateId != null and cateId != ''">
                and a.model_cate_id in (select cate_id from a_category c where c.cate_id = #{cateId})
            </when>
            <when test="brandId != null and brandId != ''">
                and a.model_cate_id in
                (select cate_id from a_category bc where bc.cate_parent_id = #{brandId})
            </when>
            <when test="factoryId != null and factoryId != ''">
                and a.model_cate_id in
                (select cate_id from a_category bc
                where bc.cate_parent_id in
                (select cate_id from a_category fc where fc.cate_parent_id = #{factoryId}))
            </when>
        </choose>
        <if test="country != null and country != ''">
            and a.MODEL_COUNTRY = #{country}
        </if>
        <if test="standardName != null and standardName != ''">
            and a.MODEL_STANDARD_NAME LIKE '%${standardName}%'
        </if>
        <if test="vinNo != null and vinNo !=''">
            and a.MODEL_VIN_NO LIKE '%${vinNo}%'
        </if>
        and rownum &lt;= #{endRow})aa
        WHERE aa.rn > #{startRow})aaa LEFT JOIN S_DICT_MIN s on aaa.MODEL_CLASS = s.MIN_VALUE
    </select>

上一篇:[Oracle]伪列ROWID和ROWNUM


下一篇:关于Oracle数据库的rownum应用