spring4 -- 基于jdbcTemplate的分页

本文介绍了如何在Spring4项目中使用JdbcTemplate进行分页查询。提供了一个分页类Page,包含页数、总数等属性,并展示了如何在DAO层调用该分页类进行分页查询。示例代码中,DAO层的方法利用limit关键字构造SQL,返回分页后的数据列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目中的通用分页组主要是分页类,下面的分页类是对网上常见的一个类的稍稍改造,针对的库是mysql,所以分页语句就比较简单,使用了limit关键字:
public class Page{
//一页显示的记录数
private int numPerPage;
//记录总数
private int totalRows;
//总页数
private int totalPages;
//当前页码
private int currentPage;
//起始行数
private int startIndex;
//结束行数
private int lastIndex;
//结果集存放List
private List<Map<String, Object>> resultList;

/**分页构造函数
* @param sql 包含筛选条件的sql,但不包含分页相关约束,如mysql的limit
* @param currentPage 当前页
* @param numPerPage 每页记录数
* @param jTemplate JdbcTemplate实例
*/
public Page(String sql,int currentPage,int numPerPage,JdbcTemplate jTemplate){
	if(jTemplate == null){
	throw new IllegalArgumentException("Page.jTemplate is null");
	}else if(sql == null || sql.equals("")){
	throw new IllegalArgumentException("Page.sql is empty");
	}
	//设置每页显示记录数
	setNumPerPage(numPerPage);
	//设置要显示的页数
	setCurrentPage(currentPage);
	//计算总记录数
	StringBuffer totalSQL = new StringBuffer(" SELECT count(*) FROM ( ");
	totalSQL.append(sql);
	totalSQL.append(" ) totalTable ");
	//总记录数
	setTotalRows(jTemplate.queryForInt(totalSQL.toString()));
	//计算总页数
	setTotalPages();
	//计算起始行数
	setStartIndex();
	//计算结束行数
	setLastIndex();
	System.out.println("lastIndex="+lastIndex);
	//使用mysql时直接使用limits
	StringBuffer paginationSQL = new StringBuffer();
	paginationSQL.append(sql);
	paginationSQL.append(" limit " + startIndex + "," + lastIndex);
	//装入结果集
	setResultList(jTemplate.queryForList(paginationSQL.toString()));
}

public int getCurrentPage() {
	return currentPage;
}

public void setCurrentPage(int currentPage) {
	this.currentPage = currentPage;
}

public int getNumPerPage() {
	return numPerPage;
}

public void setNumPerPage(int numPerPage) {
	this.numPerPage = numPerPage;
}

public List<Map<String, Object>> getResultList() {
	return resultList;
}

public void setResultList(List<Map<String, Object>> resultList) {
	this.resultList = resultList;
}

public int getTotalPages() {
	return totalPages;
}

//计算总页数
public void setTotalPages() {
	if(totalRows % numPerPage == 0){
		this.totalPages = totalRows / numPerPage;
	}else{
		this.totalPages = (totalRows / numPerPage) + 1;
	}
}

public int getTotalRows() {
	return totalRows;
}

public void setTotalRows(int totalRows) {
	this.totalRows = totalRows;
}

public int getStartIndex() {
	return startIndex;
}

public void setStartIndex() {
	this.startIndex = (currentPage - 1) * numPerPage;
}

public int getLastIndex() {
	return lastIndex;
}


//计算结束时候的索引
public void setLastIndex() {
	System.out.println("totalRows="+totalRows);///
	System.out.println("numPerPage="+numPerPage);///
	if( totalRows < numPerPage){
		this.lastIndex = totalRows;
	}else if((totalRows % numPerPage == 0) || (totalRows % numPerPage != 0 && currentPage < totalPages)){
		this.lastIndex = currentPage * numPerPage;
	}else if(totalRows % numPerPage != 0 && currentPage == totalPages){//最后一页
		this.lastIndex = totalRows ;
	}
}

}
dao层:
/**
* 查询包含所有字段的分页数据
* @param tableName
* @param where
* @return
*/
public List<Map<String, Object>> getPageListAllCol(String tableName,String where,int currentPage,int numPerPage){
String sql = "select * from " + tableName + where;
Page page = new Page(sql, currentPage, numPerPage, sjc);
return page.getResultList();
}
部分数据结果:
totalRows=60
numPerPage=15
lastIndex=15

其中分页类中用到的数据源,是由dao层作为参数传递进去的,dao层的数据源使用@autowared自动注入,具体注入可参考前面内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值