查询,分页封装

一,后台查询,分页封装

1.BaseQuery.java:

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class BaseQuery implements Serializable {
private Integer pageSize = 5;

private Integer currentPage = 1;

private Integer startRow = 0;


public Integer getPageSize() {
return pageSize;
}


public void setPageSize(Integer pageSize) {
if(pageSize == null || pageSize <= 0) {
return;
}
this.pageSize = pageSize;
}


public Integer getCurrentPage() {
return currentPage;
}


public void setCurrentPage(Integer currentPage) {
if(currentPage == null || currentPage <= 0) {
return;
}
this.currentPage = currentPage;
}


public Integer getStartRow() {
if(currentPage == null || currentPage < 0 || pageSize == null && pageSize < 0) {
return 0;
}
startRow = (currentPage - 1) * pageSize;
return startRow;
}

public Map<String, Object> bulid() {
Map<String, Object> params = new HashMap<String, Object>();
params.put("pageSize", this.pageSize);
params.put("currentPage", this.currentPage);
params.put("startRow", this.getStartRow());

return params;
}
}


2.查询类继承BaseQuery,以UserQuery.java为例。

import java.util.Map;


public class UserQuery extends BaseQuery {

private String name;

public String getName() {

return name;

}


public void setName(String name) {

this.name = name;

}

public Map<String, Object> build() {

Map<String, Object> params = super.bulid();

params.put("name", name);

return params;

}

public String queryString() {

StringBuilder sb = new StringBuilder();

sb.append("name=");

sb.append(name == null ? "" : name);

return sb.toString();

}

}


3.QueryResult.java:

import java.io.Serializable;
import java.util.*;

/**
 * 分页查询结果
 * @param <T>
 */
public class QueryResult<T> implements Serializable {


    private Integer totalPage;//总页数
    private Integer amount;//总条数
    private Collection<T> resultList = new ArrayList<T>();//当前页数据集合
    private BaseQuery query;

    public Integer getTotalPage() {
        return totalPage;
    }


    public Integer getAmount() {
        return amount;
    }


    public void setAmount(Integer amount) {
        if (amount == null) {
            return;
        }
        if (query == null) {
            return;
        }
        int pageSize = query.getPageSize();
        int mod = amount % pageSize;
        int page = amount / pageSize;
        totalPage = (mod == 0 ? page : page + 1);
        this.amount = amount;
    }


    public Collection<T> getResultList() {
        return resultList;
    }


    public void setResultList(Collection<T> resultList) {
        this.resultList = resultList;
    }


    public BaseQuery getQuery() {
        return query;
    }


    public void setQuery(BaseQuery query) {
        this.query = query;
    }


    public Integer getNextPage() {
        Integer nextPage = query.getCurrentPage() + 1;
        return nextPage > totalPage ? -1 : nextPage;
    }


    public List<Integer> pageNumbers() {
        if(totalPage == 0) {
            return Collections.EMPTY_LIST;
        }
        List<Integer> pages = new ArrayList<Integer>();
        for(int i = 1; i<= totalPage; i++) {
            pages.add(i);
        }
        return pages;
    }


    //***************************utils*****************************//
    public Map<String, Object> propertyMap() {
        Map<String, Object> pm = new HashMap<String, Object>(5);
        pm.put("amount", this.amount);
        pm.put("totalPage", this.totalPage);
        pm.put("resultList", this.resultList);
        if (query != null) {
            pm.putAll(query.build());
        }
        return pm;
    }
}


二,前台用velocity的宏定义显示分页。

1.list_user.vm:

<div class="content">

<form action="list_user.jhtml" method="post">

<label>姓名</label>

<input type="text" name="name" value="$!{query.name}" />

<input type="submit" value="查询">

</form>

    <table class="table1" width="100%" style="margin-top: 20px;">

        <tr class="headline">

            <td>借款人姓名</td>

        </tr>

        #foreach( $user in $!users )

            <tr>

                <td>$!user.name</td>

            </tr>

        #end

    </table>

    #pagination("list_user.jhtml?$!{query.queryString()}" $!queryResult)

</div>


2.macro.vm

#macro(pagination $targetUrl $queryResult)

<div id="kkpager"></div>

<script type="text/javascript" src="/system-web/lib/kkpager.min.js"></script>

<script type="text/javascript">

//init

    $(function(){

        //生成分页

        //有些参数是可选的,比如lang,若不传有默认值

        kkpager.generPageHtml({

            //当前页码

            pno : $!{query.currentPage},

            //总页码

            total : $!{queryResult.totalPage},

            //总数据条数

            totalRecords : $!{queryResult.amount},

            //链接前部

            hrefFormer : '$targetUrl',

            //链接尾部

            hrefLatter : '',

            getLink : function(n){

                if (n == 1){

                    return this.hrefFormer;

                }

                return this.hrefFormer + "&page="+n;

            }

        });

    });

</script>

#end


3.使用velocity配置引入macro.vm

a. 在spring-velocity.xml配置中加入macroHelper:

<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">

<property name="exposeSpringMacroHelpers" value="true"/>

</bean>

b. 在velocity.properties中加入:

velocimacro.library.autoreload=true

velocimacro.library=macro.vm


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值