Mybatis使用Oracle分页

Mybatis使用Oracle分页
一、
pageResult.java类

public class PageResult {

	private List dataList;
	private int page;// 当前页
	private int pageSize;
	private int total;// 数据总数
	private int totalPage;// 总页数
	private int start;
	private String url;

	public PageResult(int page, int pageSize, int total, String url) {
		this.page = page;
		this.pageSize = pageSize;
		this.total = total;
		totalPage = (total % pageSize == 0) ? (total / pageSize) : (total
				/ pageSize + 1);
		if (page < 1) {
			this.page = 1;
			this.totalPage = 1;
		}
		if (page > totalPage && totalPage > 0) {
			this.page = totalPage;
		}
		start = (page - 1) * pageSize;
		if (url.indexOf("?") >= 0) {
			this.url = url + "&page=";
		} else {
			this.url = url + "?page=";
		}
	}
	//get set方法省略
}

二、
controller控制器

public ModelAndView list(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("name/list");
		String key = request.getParameter("key");
		String pageStr = request.getParameter("page");
		int page = 1;
		StringBuffer url = new StringBuffer("list");
		if (Utils.isNotEmpty(key)) {
			url.append("?key=").append(key);
			if (Utils.isNotEmpty(pageStr)) {
				page = Integer.valueOf(pageStr);
			}else {
				page = 1;
			}
		}
		else if (Utils.isNotEmpty(pageStr)) {
			page = Integer.valueOf(pageStr);
		}else {
			page = 1;
		}
		PageResult pageResult = service.findInfoPageResult(url.toString(), key, page, Common.PAGESIZE);
		mv.addObject("key", key);
		mv.addObject("list", pageResult.getDataList());
		mv.addObject("pageResult", pageResult);
		return mv;
	}

三、
service类

public PageResult findInfoPageResult(
		String url, String key, int page, int pageSize) {
	Map map = new HashMap();
	map.put("key", key);
	int total = dao.findTotalInfo(map);
	PageResult result = new PageResult(page, pageSize, total, url);
	map.put("start", (result.getPage()-1)*result.getPageSize());
	map.put("end", result.getPage()*result.getPageSize());
	List list = taskDao.findInfoPageResult(map);
	result.setDataList(list);
	return result;
}

四、mybatis中的sql(针对oracle数据库)



五、
html-可以独立引入页面
模仿百度分页样式




上一页 <#if pageResult.totalPage <= 10> <#list 1..pageResult.totalPage as t> <#if pageResult.page==t> ${t} <#if pageResult.page!=t> ${t} <#if pageResult.totalPage gt 10> <#if pageResult.page <= 6> <#list 1..pageResult.totalPage as t> <#if pageResult.page==t> ${t} <#if pageResult.page!=t && t < 10> ${t} <#if pageResult.page!=t && t == pageResult.totalPage> ${t} <#if pageResult.page gt 6> <#list 1..pageResult.totalPage as t> <#if pageResult.page!=t && t == 1> ${t} <#if pageResult.page==t> ${t} <#if (pageResult.totalPage - pageResult.page) gte 4> <#if t gt (pageResult.page - 5) && t < (pageResult.page + 5) && pageResult.page!=t && t != pageResult.totalPage> ${t} <#if (pageResult.totalPage - pageResult.page) < 4> <#if pageResult.page!=t && t gt pageResult.totalPage-9 && t != pageResult.totalPage> ${t} <#if pageResult.page!=t && t == pageResult.totalPage> <#if (pageResult.totalPage - pageResult.page) gt 5> ${t} 下一页

你可能感兴趣的:(java)