MyBatis學習總結_12_Mybatis+Mysql分頁查詢


package cn.tsjinrong.fastfile.util;


/**
 * @ClassName: Page
 * @Description: TODO(分頁組件的父類,用來封裝分頁的 通用內容和邏輯)
 * @author zhanghaiyang
 * @date 2016年1月14日 下午12:37:55
 * @Copyright © 2016上海通善互聯網金融信息服務有限公司
 */
public class Page {


// 用戶輸入的分頁條件
private int currentPage = 1; // 當前頁
private int pageSize = 15; // 每頁最大行數


// 用於實現分頁SQL的條件,是根據用戶輸入條件計算而來的
private int begin;
private int end;


// 自動計算出的總行數
private int rows;
// 根據總行數計算總頁數,然后將總頁數輸出給頁面
private int totalPage;


public int getRows() {
return rows;
}


public void setRows(int rows) {
this.rows = rows;
}


public int getTotalPage() {
// 根據總行數,計算總頁數
if (rows % pageSize == 0) {
totalPage = rows / pageSize;
} else {
totalPage = rows / pageSize + 1;
}
return totalPage;
}


public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}


public int getBegin() {
// 在mapper.xml使用begin屬性時,對其進行計算
begin = (currentPage - 1) * pageSize;
return begin;
}


public void setBegin(int begin) {
this.begin = begin;
}


public int getEnd() {
// 在mapper.xml使用end屬性時,對其進行計算
end = currentPage * pageSize + 1;
return end;
}


public void setEnd(int end) {
this.end = end;
}


public int getCurrentPage() {
return currentPage;
}


public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}


public int getPageSize() {
return pageSize;
}


public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}


}

public ModelAndView findVideosByPage(HttpServletRequest request, HttpServletResponse response, FileProperties fp) {


ModelAndView model = new ModelAndView("/video/video_list");
Map<String, Object> params = new HashMap<String, Object>(3);
if (StringUtils.isNotBlank(fp.getBusiId())) {
params.put("busiId", fp.getBusiId());
}
if (StringUtils.isNotBlank(fp.getApplyName())) {
params.put("applyName", fp.getApplyName());
}
if (fp.getApplyDateStart() != null && StringUtils.isNotBlank(fp.getApplyDateStart())) {
params.put("applyDateStart", DateUtil.parseDate(fp.getApplyDateStart()));
} else {
params.put("applyDateStart", DateUtil.addDay(new Date(), -7));
}
if (fp.getApplyDateEnd() != null && StringUtils.isNotBlank(fp.getApplyDateEnd())) {
params.put("applyDateEnd", DateUtil.parseDate(fp.getApplyDateEnd()));
} else {
params.put("applyDateEnd", DateUtil.format(new Date()));
}
fp.setRows(fastfileVideoService.selectRows(params));
model.addObject("fastfileVideoInfoPage", fp);
List<FastfileVideoInfo> fastfileVideoInfos = fastfileVideoService.selectByPage(fp);
model.addObject("fastfileVideoInfos", fastfileVideoInfos);
model.addObject("applyDateStart", DateUtil.format(DateUtil.addDay(new Date(), -7)));
model.addObject("applyDateEnd", DateUtil.format(new Date()));
return model;
}

<select id="selectByPage" resultMap="BaseResultMap" parameterType="cn.tsjinrong.fastfile.util.FileProperties">
select
<include refid="Base_Column_List" />
from fastfile_video_info where 1=1
<if test="busiId != null and busiId !=''">
and busi_id = #{busiId,jdbcType=VARCHAR}
</if>
<if test="applyName != null and applyName !=''">
and apply_name=#{applyName,jdbcType=VARCHAR}
</if>
<if test="applyDateStart != null and applyDateStart !=''">
and apply_date &gt;= #{applyDateStart,jdbcType=DATE}
</if>
<if test="applyDateEnd != null and applyDateEnd !=''">
and apply_date &lt;= #{applyDateEnd,jdbcType=DATE}
</if>
and del_flag = 0
order by apply_date desc limit #{beginRow},#{pageSize}
</select>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM