这个分页采用的是oracle的后台分页,但是却没有将他分层。因为只是写给别人的一个小参考。而我也是扒的别人的代码,不过在自己机器上调试还是通过了的。之前一直知道分页,但是自己还真没有写过分页。只有毕业设计的时候写过,现在看看惨不忍睹,一开始就是一个select * 从数据库里面全部取出来,然后放到一个List里面。而这个确实后台分页。因为是扒的别人的代码所以有点凌乱,不过对自己理解分页却还是不过。那个人呢的注释很详细。
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import cn.ac.caf.ld.pojo.LinDiXianZhuang;
@Controller
public class Pagination {
private Log log = LogFactory.getLog(Pagination.class);
private static final int NUMBERS_PER_PAGE = 10;
// 一页显示的记录数
private int numPerPage;
// 记录总数
private int totalRows;
// 总页数
private int totalPages;
// 当前页码
private int currentPage;
// 起始行数
private int startIndex;
// 结束行数
private int lastIndex;
// 结果集存放List
private List resultList;
@Autowired
private JdbcTemplate jdbcTemplate;
public Pagination() {
}
/**
* * 每页显示10条记录的构造函数,使用该函数必须先给Pagination设置currentPage,jTemplate初值 * @param
* sql oracle语句
*/
public Pagination(String sql, int currentPage, int numPerPage,
JdbcTemplate jdbcTemplate) {
// 设置每页显示记录数
setNumPerPage(numPerPage);
// 设置要显示的页数
setCurrentPage(currentPage);
// 计算总记录数
StringBuffer totalSQL = new StringBuffer(" SELECT count(*) FROM ( ");
totalSQL.append(sql);
totalSQL.append(" ) ");
setJdbcTemplate(jdbcTemplate);
// 总记录数
setTotalRows(getJdbcTemplate().queryForInt(totalSQL.toString()));
// 计算总页数
setTotalPages();
// 计算起始行数
setStartIndex();
// 计算结束行数
setLastIndex();
// 构造oracle数据库的分页语句
StringBuffer paginationSQL = new StringBuffer(" SELECT * FROM ( ");
paginationSQL.append(" SELECT A.* , ROWNUM RN FROM ( ");
paginationSQL.append(sql);
paginationSQL.append(" ) A WHERE ROWNUM <= " + lastIndex);
paginationSQL.append(" and qsdm!='00' and qydm!='00' ) WHERE RN > "
+ startIndex);
log.info(totalSQL);
log.info(paginationSQL);
// 装入结果集
setResultList(getJdbcTemplate().queryForList(paginationSQL.toString()));
final List<LinDiXianZhuang> ldMessage = new ArrayList<LinDiXianZhuang>();
jdbcTemplate.query(paginationSQL.toString(), new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
LinDiXianZhuang linDiXianZhuang = new LinDiXianZhuang();
setAllProperty(rs, linDiXianZhuang);
ldMessage.add(linDiXianZhuang);
}
});
setResultList(ldMessage);
log.info(getJdbcTemplate().queryForList(paginationSQL.toString()));
}
//这个是通过服务器访问测试
@RequestMapping(value = "/page.do")
public ModelAndView page(@RequestParam("currentPage") int currentPage,
HttpServletRequest request, HttpServletResponse response) {
String sql = "select * from t_sta_01_2010";
Pagination pageInfo = new Pagination(sql.toString(), currentPage, 10,
jdbcTemplate);
log.info("总页数" + pageInfo.getTotalPages());
log.info("总记录数:" + pageInfo.getTotalRows());
pageInfo.getTotalPages();
pageInfo.getResultList();
pageInfo.getStartIndex();
pageInfo.getLastIndex();
ModelAndView mv = new ModelAndView();
mv.addObject("result", pageInfo.getResultList());
mv.addObject("totalRows",pageInfo.getTotalRows());
mv.addObject("totalPage",pageInfo.getTotalPages());
mv.addObject("startIndex", pageInfo.getStartIndex());
mv.addObject("lastIndex", pageInfo.getLastIndex());
mv.setViewName("page");
return mv;
}
//这是用来测试,不需要跑tomcat。不过可以采用junit
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "select * from t_sta_01_2010";
Pagination pageInfo = new Pagination(sql.toString(), 2, 10,
jdbcTemplate);
pageInfo.getTotalPages();
}
// 计算起始行数
public void setStartIndex() {
this.startIndex = (currentPage - 1) * numPerPage;
}
// 计算总页数
public void setTotalPages() {
if (totalRows % numPerPage == 0) {
this.totalPages = totalRows / numPerPage;
} else {
this.totalPages = (totalRows / numPerPage) + 1;
}
}
public void setLastIndex() {
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;
}
}
public int getNumPerPage() {
return numPerPage;
}
public void setNumPerPage(int numPerPage) {
this.numPerPage = numPerPage;
}
public int getTotalRows() {
return totalRows;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getStartIndex() {
return startIndex;
}
public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}
public int getLastIndex() {
return lastIndex;
}
public void setLastIndex(int lastIndex) {
this.lastIndex = lastIndex;
}
public List getResultList() {
return resultList;
}
public void setResultList(List resultList) {
this.resultList = resultList;
}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/**
* 封装所有林地信息
* */
public void setAllProperty(ResultSet rs, LinDiXianZhuang linDiXianZhuang)
throws SQLException {
//垃圾代码。就不贴出来了
}
可以通过http://localhost:8080/xxx/page.do?currentPage=xx来访问了。代码没有优化过,只是为了记忆