PageHelper查询结果排序

1.使用PageHelper

PageHelper.startPage(pageNum , pageSize);PageHelper.orderBy("A B");
其中A为排序依据的字段名,B为排序规律,desc为降序,asc为升序

或者一步到位

String orderBy="字段名 排序规律";

PageHelper.startPage(pageNum, pageSize, orderBy);
 

2.使用Mybatis排序

XXXExample example = new XXXExample();

example.setOrderByClause("字段名1 ASC/DESC,字段名2 ASC/DESC,...");

我的例子:

public class AdminBaseBusiness {

    protected void startPage() {
        Integer currentPage = ServletUtils.getIntParameter("current_page");
        Integer pageSize = ServletUtils.getIntParameter("page_size");
        IAssert.notNull(currentPage, "current_page is require");
        IAssert.notNull(pageSize, "page_size is require");
        //排序
        String orderByColumn = ServletUtils.getStrParameter("order_by_column");
        String sort = ServletUtils.getStrParameter("sort");//排序的方向 "desc" 或者 "asc".

        PageHelper.startPage(currentPage, pageSize, getOrderBy(orderByColumn, sort));
    }

    private String getOrderBy(String orderByColumn, String sort) {
        if (StringUtils.isEmpty(orderByColumn)) {
            return "";
        }
        if (StringUtils.isEmpty(sort)) sort = "desc";
        return orderByColumn + " " + sort;
    }
}

 

你可能感兴趣的:(java,mybatis)