mybatis动态条件组合分页查询

一、动态条件处理

  需要使用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     }

猜你喜欢

转载自www.cnblogs.com/cat-fish6/p/9056034.html
今日推荐