一、動態條件處理
需要使用mybatis的動態sql
1 <select id="selectItemByCondition" parameterType="com.rl.ecps.model.QueryCondition" resultMap="BaseResultMap"> 2 select * 3 from (select rownum rw, a.* 4 from (select * from eb_item t 5 <where> 6 <if test="brandId != null">t.brand_id = #{brandId}</if> 7 <if test="auditStatus != null"> and t.audit_status = #{auditStatus}</if> 8 <if test="showStatus != null"> and t.show_status = #{showStatus}</if> 9 <if test="itemName != null and itemName != ''"> and t.item_name like '%${itemName}%'</if> 10 </where> 11 <![CDATA[ 12 ) a 13 where rownum < #{endNum}) b 14 where b.rw > #{startNum} 15 ]]> 16 </select>
二、分頁
Oracle的分頁sql:
oracle的分頁必須要使用rownum(只支持小於號)
select *
from (select rownum rw, a.*
from (select * from eb_item) a
where rownum < 21) b
where b.rw > 10
1、查詢全量結果集
2、以全量結果集作為一張表,以rownum作為查詢條件小於開始行號,以全量結果集和rownum作為結果集,並給rownum起個別名
3、以第二部為結果,查詢rownum 大於其實行號
分頁思想:
從查詢角度,計算startNum和endNum (需要pageNo 和 pageSize計算)
startNum = (pageNo - 1 ) * pageSize ;
endNum = pageNo * pageSize + 1;
從展示角度,需要指導pageNo, totalCount, totalPage
totalPage三種情況:
totalCount pageSize totalPage
0 10 1
100 10 10
92 10 10
1 public int getTotalPage() { 2 totalPage = totalCount/pageSize; 3 if(totalCount == 0 || totalCount % pageSize != 0){ 4 totalPage++; 5 } 6 return totalPage; 7 }