分页处理方式

1、将分页的相应信息放入一个对象中,包括分页返回值。
import java.io.Serializable;

public class Paginate implements Serializable {
private static final long serialVersionUID = 1L;
//总数
private long total;
//当前页数
private int currentPageNum = 1;
  //分页后返回的list集合
private Object data;
//每页的数量
private int recordsPerPage = 10;
//能显示的页数数量
private int showTags = 5;
//分页的查询条件
private Object condition;
//下面两部分是从js中获取的
private int startNum;
private long pageTotal;

public long getPageTotal() {
return ((this.total + this.recordsPerPage - 1L) / this.recordsPerPage);
}

public void setPageTotal(long pageTotal) {
this.pageTotal = pageTotal;
}

public void setStartNum(int startNum) {
this.startNum = startNum;
}

public int getStartNum() {
return ((this.currentPageNum - 1) * this.recordsPerPage);
}

public long getTotal() {
return this.total;
}

public void setTotal(long total) {
this.total = total;
}

public int getCurrentPageNum() {
return this.currentPageNum;
}

public void setCurrentPageNum(int currentPageNum) {
this.currentPageNum = currentPageNum;
}

public Object getData() {
return this.data;
}

public void setData(Object data) {
this.data = data;
}

public int getRecordsPerPage() {
return this.recordsPerPage;
}

public void setRecordsPerPage(int recordsPerPage) {
this.recordsPerPage = recordsPerPage;
}

public int getShowTags() {
return this.showTags;
}

public void setShowTags(int showTags) {
this.showTags = showTags;
}

public Object getCondition() {
return this.condition;
}

public void setCondition(Object condition) {
this.condition = condition;
}
}

2、数据库中的处理方式mybatis(一下只是主要部分,其他详细配置请参考其他)。
<resultMap id="selectByNameResultMap" type="cn.ftiao.tm.model.Lecturer">
<id column="ID" property="id" />
<result column="LECTURER_NAME" property="lecturerName" />
</resultMap>

<select id="selectByName" resultMap="selectByNameResultMap" parameterType="java.lang.String">
select ID,LECTURER_NAME from LECTURER where DELETED_FLAG='N' and LECTURER_NAME like CONCAT('%',#{lecturerName},'%') limit 0,10
</select>

<select id="paginate" resultMap="BaseResultMap" parameterType="cn.ftiao.common.Paginate">
select ID,LECTURER_NAME,CP_NAME,PHONE,EMAIL,APPLY_STATUS,DELETED_FLAG from LECTURER
<include refid="paginateCondition" />
order by ID desc limit #{startNum}, #{recordsPerPage}
</select>

<select id="paginateByCount" resultType="java.lang.Long" parameterType="cn.ftiao.common.Paginate">
select count(1) from LECTURER
<include refid="paginateCondition" />
</select>

<sql id="paginateCondition">
<where>
DELETED_FLAG='N'
<if test="condition!=null">
<if test="condition.keyword != null">
and (LECTURER_NAME like CONCAT('%',#{condition.keyword},'%') or CP_NAME like CONCAT('%',#{condition.keyword},'%'))
</if>
</if>
</where>
</sql>

3、service中的业务处理(只贴出了方法,请挑主要部分)
@Override
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
public Paginate paginate(Lecturer lecturer, Paginate paginate) {
if (paginate == null) { throw new IllegalArgumentException("paginate"); }
if (lecturer == null) { throw new IllegalArgumentException("lecturer"); }
LOG.debug(ToString.reflectionToString(paginate));
paginate.setCondition(lecturer);
long count = this.lecturerMapper.paginateByCount(paginate);
if (count > 0) {
paginate.setTotal(count);
List<Lecturer> lecturers = this.lecturerMapper.paginate(paginate);
paginate.setData(lecturers);
List<Long> ids = getIds(lecturers);
setLessonTotal(lecturers, this.getLessonTotal(ids));
setSyllabusTotal(lecturers, this.getSyllabusTotal(ids));
}
return paginate;
}

猜你喜欢

转载自irh.iteye.com/blog/2093260