⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pagemodel.java

📁 iBatis分页实例 内含数据库文件在src文件夹中
💻 JAVA
字号:
package org.sanguo.dao;

import java.util.List;

import org.sanguo.util.SqlClientManager;

import com.ibatis.sqlmap.client.SqlMapClient;

public class PageModel {

	private SqlMapClient client = null;

	private int current; // 当前页索引

	private int pageSize; // 每页显示的记录数

	private int start; // 当前页的第一条记录索引

	private int pageCount; // 需要用几页显示

	private int totalRecord; // 总记录数

	private boolean next; // 是否有下一页

	private boolean previous; // 是否有上一页

	public boolean isNext() {
		return next;
	}

	public int getPageCount() {
		return pageCount;
	}

	public boolean isPrevious() {
		return previous;
	}

	public int getPageSize() {
		return pageSize;
	}

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

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getCurrent() {
		return current;
	}

	public void setCurrent(int current) {
		this.current = current;
	}

	// 构造函数,完成成员变量初始化
	public PageModel() {
		this.current = 1;
		client = SqlClientManager.getSqlMapClient();
		pageSize = 5;

		try {
			// 得到总记录数
			this.totalRecord = ((Integer) client.queryForObject("getTotal"))
					.intValue();

			if (totalRecord <= pageSize) {
				pageCount = 1;
				next = false;
				previous = false;

			} else {
				next = true;
				previous = false;
				this.pageCount = (totalRecord + pageSize - 1) / pageSize;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 获得下一页数据
	public List getNextPage() {
		this.current++;
		if (current >= pageCount) {
			current = pageCount;
			next = false;
		} else {
			next = true;
		}
		this.start = (current - 1) * pageSize;
		previous = true;
		return this.getOnePageRecord();
	}

	// 获得上一页数据
	public List getPreviousPage() {
		this.current--;
		if (current <= 1) {
			current = 1;
			previous = false;
		} else {
			previous = true;
		}
		next = true;
		this.start = (current - 1) * pageSize;
		return this.getOnePageRecord();
	}

	// 根据当前页索引获得单页记录信息
	public List getOnePageRecord() {
		try {
			return client.queryForList("getAllPersons", start, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// public static void main(String[] args) throws Exception {
	// PageModel model = new PageModel();
	// Iterator it = model.getNextPage().iterator();
	// while (it.hasNext()) {
	// Person person = (Person) it.next();
	// System.out.println(person.getName());
	// }
	// // SqlMapClient client=SqlClientManager.getSqlMapClient();
	// // System.out.println(client.queryForList("getAllPersons").size());
	// }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -