最近使用了spring中的JdbcTemplate實現數據庫的查詢和插入操作
如果你的查詢有
package com
import org
import org
/** * 分頁函數 * * @author allenpan */public class Pagination extends JdbcDaoSupport{
public static final int NUMBERS_PER_PAGE =
//一頁顯示的記錄數
private int numPerPage;
//記錄總數
private int totalRows;
//總頁數
private int totalPages;
//當前頁碼
private int currentPage;
//起始行數
private int startIndex;
//結束行數
private int lastIndex;
//結果集存放List
private List resultList;
//JdbcTemplate jTemplate
private JdbcTemplate jTemplate;
/**
* 每頁顯示
* @param sql oracle語句
*/
public Pagination(String sql){
if(jTemplate == null){
throw new IllegalArgumentException(
}else if(sql
throw new IllegalArgumentException(
}
new Pagination(sql
}
/**分頁構造函數
* @param sql 根據傳入的sql語句得到一些基本分頁信息
* @param currentPage 當前頁
* @param numPerPage 每頁記錄數
* @param jTemplate JdbcTemplate實例
*/
public Pagination(String sql
if(jTemplate == null){
throw new IllegalArgumentException(
}else if(sql == null || sql
throw new IllegalArgumentException(
}
//設置每頁顯示記錄數
setNumPerPage(numPerPage);
//設置要顯示的頁數
setCurrentPage(currentPage);
//計算總記錄數
StringBuffer totalSQL = new StringBuffer(
totalSQL
totalSQL
//給JdbcTemplate賦值
setJdbcTemplate(jTemplate);
//總記錄數
setTotalRows(getJdbcTemplate()
//計算總頁數
setTotalPages();
//計算起始行數
setStartIndex();
//計算結束行數
setLastIndex();
System
//構造oracle數據庫的分頁語句
StringBuffer paginationSQL = new StringBuffer(
paginationSQL
paginationSQL
paginationSQL
paginationSQL
//裝入結果集
setResultList(getJdbcTemplate()
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this
}
public int getNumPerPage() {
return numPerPage;
}
public void setNumPerPage(int numPerPage) {
this
}
public List getResultList() {
return resultList; }
public void setResultList(List resultList) {
this
}
public int getTotalPages() {
return totalPages;
}
//計算總頁數
public void setTotalPages() {
if(totalRows % numPerPage ==
this
}else{
this
}
}
public int getTotalRows() {
return totalRows;
}
public void setTotalRows(int totalRows) {
this
}
public int getStartIndex() {
return startIndex;
}
public void setStartIndex() {
this
}
public int getLastIndex() {
return lastIndex;
}
public JdbcTemplate getJTemplate() {
return jTemplate;
}
public void setJTemplate(JdbcTemplate template) {
jTemplate = template;
}
//計算結束時候的索引
public void setLastIndex() {
System
System
if( totalRows < numPerPage){
this
}else if((totalRows % numPerPage ==
this
}else if(totalRows % numPerPage !=
this
}
}}在我的業務邏輯代碼中
/**
* find season ranking list from DC
* @param areaId 選手區域id
* @param rankDate 賽季
* @param category 類別
* @param characterName 角色名
* @return List
*/
public List findSeasonRankingList(Long areaId
Long categoryId
//SQL語句
StringBuffer sql = new StringBuffer(
//表 sql
sql
sql
sql
sql
sql
sql
sql
sql
sql
//附加條件
if(areaId != null && areaId
sql
}
if( rankYear >
//hql
//hql
sql
}
if(categoryId != null && categoryId
sql
}
if(characterName != null && !characterName
sql
}
sql
sql
sql
sql
System
//使用自己的分頁程序控制結果集
Pagination pageInfo = new Pagination(sql
return pageInfo
//return getJdbcTemplate()
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28840.html