mybatis--分页

APIImpl
@Override
    public Pagination<Goods> getGoodsListByPager(Pagination<Goods> pager,List<String> gcList) {
        // TODO Auto-generated method stub
        List<Goods> goods = goodsDAO.getGoodsListByPager(pager,
                pager.getCondition(), gcList);
        Long size = goodsDAO.getGoodsListByPagerCount(pager.getCondition(),gcList);
        pager.setRows(goods);
        pager.setTotal(size.intValue());
        return pager;
    }

Dao层
/**
     * 
     * @param gcList // 分类ID集合
     * @return 商品搜索查询接口开发
     */
    public List<Goods> getGoodsListByPager(
            @Param("pager") Pagination<Goods> pager, @Param("goods") Goods goods,@Param("param1")List<String> gcList);

    /**
     * 商品条数
     * 
     * @param goods
     * @return
     */
    public Long getGoodsListByPagerCount(@Param("goods") Goods goods,@Param("param1")List<String> gcList);

Mybatis

<sql id="pagination_Head">
        <![CDATA[select * from (  ]]>
    </sql>
    <!-- oracle 分页尾 -->
    <sql id="pagination_Tail">
           <![CDATA[ )rows limit #{pager.startIndex},#{pager.pageSize}]]>
    </sql>
    <!-- count * from -->
    <sql id="count_Head">
        <![CDATA[select count(*) from ( ]]>
</sql>
<select id="getGoodsListByPager" resultMap="goodsMap">
        <include refid="pagination_Head" />
        <include refid="searchGoods_fragement" /> <!-- 代码片段 -->
        <include refid="pagination_Tail" />
    </select>
   <select id="getGoodsListByPagerCount" resultType="long">
        <include refid="count_Head" />
        <include refid="searchGoods_fragement" />
        <include refid="count_Tail" />
</select>

package com.XXX.common.page;

import java.io.Serializable;

public class SimplePage implements Serializable {
	
	private static final long serialVersionUID = 1L;

	public static final int DEF_COUNT = 20;
	
	protected int total = 0; // 数据总量
	protected int pageSize = 20; // 每页数据数量
	protected int pageNumber = 1; // 页码
	protected int startIndex = 0; // 每页开始序号
	
	public static int checkPageNo(Integer pageNumber) {
		return (pageNumber == null || pageNumber < 1) ? 1 : pageNumber;
	}
	
	public SimplePage() {
		
	}
	
	public SimplePage(int pageNumber, int pageSize, int total) {
		setTotal(total);
		setPageSize(pageSize);
		setPageNo(pageNumber);
		adjustPageNo();
	}
	
	/**
	 * 调整页码,使其不超过最大页数
	 */
	public void adjustPageNo() {
		if (pageNumber == 1) {
			return;
		}
		int totalPage = getTotalPage();
		if (pageNumber > totalPage) {
			pageNumber = totalPage;
		}
		startIndex = this.pageSize * (this.pageNumber - 1);
	}

	public int getTotal() {
		return total;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getPageNumber() {
		return pageNumber;
	}
	
	public int getStartIndex() {
		return startIndex;
	}
	
	public int getTotalPage() {
		int totlePage = total/pageSize;
		if(totlePage == 0 || total/pageSize != 0){
			totalPage ++;
		}
		return totalPage;

		int totalPage = total / pageSize;
		if (totalPage == 0 || total % pageSize != 0) {
			totalPage++;
		}
		return totalPage;
	}
	
	/**
	 * 是否第一页
	 * @return
	 */
	public boolean isFirstPage() {
		return pageNumber <= 1;
	}
	
	/**
	 * 是否最后一页
	 * @return
	 */
	public boolean isLastPage() {
		return pageNumber >= getTotalPage();
	}
	
	/**
	 * 下一页页码
	 * @return
	 */
	public int getNextPage() {
		if (isLastPage()) {
			return pageNumber;
		} else {
			return pageNumber + 1;        
		}
	}
	
	public int getPretPage() {
		if (isFirstPage()) {
			return pageNumber;
		} else {
			return pageNumber - 1;
		}
	}
	
	public void setTotal(int total) {
		if (total < 0) {
			this.total = 0;
		} else {
			this.total = total;
		}
	}
	
	public void setPageSize(Integer pageSize) {
		if (pageSize == null || pageSize < 1) {
			this.pageSize = DEF_COUNT;
		} else {
			this.pageSize = pageSize;
		}
		startIndex = this.pageSize * (this.pageNumber - 1);
	}
	
	public void setPageNo(Integer pageNumber) {
		if (pageNumber == null || pageNumber < 1) {
			this.pageNumber = 1;
		} else {
			this.pageNumber = pageNumber;
		}
		startIndex = this.pageSize * (this.pageNumber - 1);
	} 
}


package com.XXX.common.page;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Pagination<T> extends SimplePage implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	private String id;
	
	public Pagination() {
		
	}
	
	public Pagination(int pageNumber, int pageSize, int total) {
		super(pageNumber, pageSize, total);
	}
	
	public Pagination(int pageNumber, int pageSize, int total, List<?> rows) {
		super(pageNumber, pageSize, total);
		this.rows = rows;
	}
	
	public int getFirstResult() {
		return (pageNumber - 1) * pageSize;
	}
	
	private List<?> rows = new ArrayList();
	
	private T condition;
	
	private Integer sort;

	public List<?> getRows() {
		return rows;
	}

	public void setRows(List<?> rows) {
		this.rows = rows;
	}

	public T getCondition() {
		return condition;
	}

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

	public Integer getSort() {
		return sort;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public final static String PAGE_CONDITION_CACHE = "PageCondition";
	public final static String PAGE_CURRENT_NO_CACHE = "PageCurrentNo";
	public final static String PAGE_SIZE_CACHE = "PageSize";
	public final static String PAGE_TOTAL_COUNT_CACHE = "PageTotalCount";
	public final static String PAGE_ALL_CONDITION_CACHE = "PageAllCondition";
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值