1、分页处理函数
package com.wangyin.platarch.util;
import java.util.List;
public class PageUtil <T>{
private List<T> values;
private int totalSize = 0;
private int maxResults = 10;
private int firstResult = 0;
public List<T> getValues() {
return values;
}
public void setValues(List<T> values) {
this.values = values;
this.finger();
}
public int getTotalSize() {
return totalSize;
}
public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}
public int getMaxResults() {
return maxResults;
}
public void setMaxResults(int maxResults) {
this.maxResults = maxResults;
}
public int getFirstResult() {
return firstResult;
}
public void setFirstResult(int firstResult) {
this.firstResult = firstResult;
}
private String pageMaker = "";
public String getPageMaker() {
return pageMaker;
}
public void setPageMaker(String pageMaker) {
this.pageMaker = pageMaker;
}
private void finger(){
if(this.totalSize==0){
this.setPageMaker("");
return;
}
StringBuffer buffer = new StringBuffer();
if(this.getFirstResult()==0){
buffer.append("‹‹ 首页 ");
buffer.append("‹ 上一页 ");
}else{
buffer.append("<a class=\"more\" href=\"?first=0\" onclick=\"pageFlag\">‹‹ 首页</a> ");
buffer.append("<a class=\"more\" href=\"?first="+(this.firstResult-this.maxResults)+"\" title=\"上一页\">‹ 上一页</a> ");
}
buffer.append("<span class=\"ft-normal\">"+(this.getFirstResult()+1)+"-"+((this.getFirstResult()+this.getMaxResults())>totalSize?totalSize:(this.getFirstResult()+this.getMaxResults()))+"条,共"+this.getTotalSize()+"条</span> ");
int totalPage = this.totalSize/this.maxResults;
if(0==this.totalSize%this.maxResults)totalPage--;
if(this.firstResult+this.maxResults>=totalSize){
buffer.append("下一页 › ");
buffer.append("末页 ››");
}else{
buffer.append("<a class=\"more\" href=\"?first="+(this.firstResult+this.maxResults)+"\">下一页 ›</a> ");
buffer.append("<a class=\"more\" href=\"?first="+totalPage*this.maxResults+"\" >末页 ››</a>");
}
this.setPageMaker(buffer.toString());
}
@Override
public String toString() {
return "PageUtil [ totalSize=" + totalSize
+ ", maxResults=" + maxResults + ", firstResult=" + firstResult +"]";
}
}
2、分页调用实现函数
@Autowired
private PlaDefrayInfoMapper plaDefrayInfoMapper;
public PageUtil<PlaDefrayInfo> queryDefraysByPage(PlaDefrayInfo defray,
int first) {
PageUtil<PlaDefrayInfo> pu=new PageUtil<PlaDefrayInfo>();
Map<String , Object> map=new HashMap<String , Object>();
map.put("vo", defray);
pu.setTotalSize(this.plaDefrayInfoMapper.totalCount(map));
pu.setFirstResult(first);
map.put("pageno",first);
map.put("maxresult", pu.getMaxResults());
List<PlaDefrayInfo> plaOrderInfos = plaDefrayInfoMapper.queryPaging(map);
//赋值内容到信息详情类中
pu.setValues(plaOrderInfos);
return pu;
}
3、Mapper文件
<select id="totalCount" resultType="java.lang.Integer" parameterType="java.util.Map">
select count(*) from pla_defray_info where 1=1
<if test="vo.merchantid!='' and vo.merchantid!=null">
and merchantid like CONCAT('%',#{vo.merchantid},'%')
</if>
<if test="vo.ordernum!='' and vo.ordernum!=null">
and ordernum like CONCAT('%',#{vo.ordernum},'%')
</if>
<if test="vo.orderType!=null and vo.orderType!=''">
and order_type=#{vo.orderType}
</if>
<if test="vo.defraynum!=null and vo.defraynum!=''">
and defraynum like CONCAT('%',#{vo.defraynum},'%')
</if>
<if test="vo.defrayState!=null and vo.defrayState!=''">
and defray_state=#{vo.defrayState}
</if>
</select>
<select id="queryPaging" resultMap="BaseResultMap" resultType="java.util.ArrayList" parameterType="java.util.Map">
select * from pla_defray_info where 1=1
<if test="vo.merchantid!='' and vo.merchantid!=null">
and merchantid like CONCAT('%',#{vo.merchantid},'%')
</if>
<if test="vo.ordernum!='' and vo.ordernum!=null">
and ordernum like CONCAT('%',#{vo.ordernum},'%')
</if>
<if test="vo.orderType!=null and vo.orderType!=''">
and order_type=#{vo.orderType}
</if>
<if test="vo.defraynum!=null and vo.defraynum!=''">
and defraynum like CONCAT('%',#{vo.defraynum},'%')
</if>
<if test="vo.defrayState!=null and vo.defrayState!=''">
and defray_state=#{vo.defrayState}
</if>
order by created_date desc
limit #{pageno},#{maxresult}
</select>
4、函数调用
@RequestMapping("/query")
public ModelAndView query(@ModelAttribute("defray") PlaDefrayInfo defray,@RequestParam(required=false,defaultValue="0") int first){
ModelAndView model=new ModelAndView("/defray/defrayDetail");
PageUtil<PlaDefrayInfo> pu=defrayService.queryDefraysByPage(defray,first);
model.addObject("pu", pu);
return model;
}
5、页面显示
当有数据时显示
<pre class="html" name="code"><c:if test="${fn:length(pu.values)>0}">
<div>显示数据</div>
<!--进行分页--!>
<div>${pu.pageMaker }</div>
</c:if>
<span style="font-size:12px;"></span>
没有数据时显示
<span style="font-size:12px;"><c:if test="${fn:length(pu.values)==0}"> <div class="ui-box-container" style="width:100%;font-size: 20px;color:blue;" align="center">无数据</div> </c:if></span>