mysql——分页查询的举例

<!--  根据条件分页查询市场活动列表-->
  <select id="selectActivityByConditionForPage" parameterType="map" resultMap="BaseResultMap">
    select a.id,u1.name as owner,a.name,a.startDate,a.endDate,a.cost,a.description,a.createTime,u2.name as createBy,
           a.editTime,u3.name as editBy
    from tbl_activity a
    join tbl_user u1 on a.owner=u1.id
    join tbl_user u2 on a.createBy=u2.id
    left join tbl_user u3 on a.editBy=u3.id
    <where>
      <if test="name!=null and name!=''">
          and a.name like '%' #{name} '%'
      </if>
      <if test="owner!=null and owner!=''">
        and u1.name like '%' #{owner} '%'
      </if>
      <if test="startDate!=null and startDate!=''">
        and a.startDate>=#{startDate}
      </if>
      <if test="startDate!=null and startDate!=''">
        and a.endDate &lt;= #{endDate}
      </if>
    </where>
      order by a.createTime desc
      limit #{beginNo},#{pageSize}
  </select>

分页查询limit是重点 #{beginNo}取map里面名字为beginNo的参数值:(page-1)*pagesize  当前页数-1乘以每页大小,也就是当前也的起始位置的下标

关于如何排序:

这里排序语句在分页语句前面,表示先排序再分页

如果排序语句在分页语句后面,表示先分页再排序

两种操作的结果不一样


版权声明:本文为weixin_54401017原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。