最近使用了spring中的jdbctemplate实现数据库的查询和插入操作,发现spring的jdbctemplate 不象hibernatetemplate那么好,已经实现了分页功能。所以要自己实现,使用getjdbctemplate().queryforlist(string sql)得到的结果集是所有的。
如果你的查询有10000条记录,或者更多,速度肯定慢了,当然你可以通过resultset中的游标控制查询的起始和结束。我这里用的是oracle数据库,使用伪列rownum来实现分页。我的分页代码如下:
package com.deity.ranking.util;import java.util.list;
import org.springframework.jdbc.core.jdbctemplate;
import org.springframework.jdbc.core.support.jdbcdaosupport;
/** * 分页函数 * * @author allenpan */public class pagination extends jdbcdaosupport{
public 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;
//jdbctemplate jtemplate
private jdbctemplate jtemplate;
/**
* 每页显示10条记录的构造函数,使用该函数必须先给pagination设置currentpage,jtemplate初值
* @param sql oracle语句
*/
public pagination(string sql){
if(jtemplate == null){
throw new illegalargumentexception("com.deity.ranking.util.pagination.jtemplate is null,please initial it first. ");
}else if(sql.equals("")){
throw new illegalargumentexception("com.deity.ranking.util.pagination.sql is empty,please initial it first. ");
}
new pagination(sql,currentpage,numbers_per_page,jtemplate);
}
/**分页构造函数
* @param sql 根据传入的sql语句得到一些基本分页信息
* @param currentpage 当前页
* @param numperpage 每页记录数
* @param jtemplate jdbctemplate实例
*/
public pagination(string sql,int currentpage,int numperpage,jdbctemplate jtemplate){
if(jtemplate == null){
throw new illegalargumentexception("com.deity.ranking.util.pagination.jtemplate is null,please initial it first. ");
}else if(sql == null || sql.equals("")){
throw new illegalargumentexception("com.deity.ranking.util.pagination.sql is empty,please initial it first. ");
}
//设置每页显示记录数
setnumperpage(numperpage);
//设置要显示的页数
setcurrentpage(currentpage);
//计算总记录数
stringbuffer totalsql = new stringbuffer(" select count(*) from ( ");
totalsql.append(sql);
totalsql.append(" ) totaltable ");
//给jdbctemplate赋值
setjdbctemplate(jtemplate);
//总记录数
settotalrows(getjdbctemplate().queryforint(totalsql.tostring()));
//计算总页数
settotalpages();
//计算起始行数
setstartindex();
//计算结束行数
setlastindex();
system.out.println("lastindex="+lastindex);//////////////////
//构造oracle数据库的分页语句
stringbuffer paginationsql = new stringbuffer(" select * from ( ");
paginationsql.append(" select temp.* ,rownum num from ( ");
paginationsql.append(sql);
paginationsql.append(" ) temp where rownum <= " + lastindex);
paginationsql.append(" ) where num > " + startindex);
//装入结果集
setresultlist(getjdbctemplate().queryforlist(paginationsql.tostring()));
}
/**
* @param args
*/
public static void main(string[] args) {
// todo auto-generated method stub }
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 getresultlist() {
return resultlist; }
public void setresultlist(list 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 jdbctemplate getjtemplate() {
return jtemplate;
}
public void setjtemplate(jdbctemplate template) {
jtemplate = template;
}
//计算结束时候的索引
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 ;
}
}}在我的业务逻辑代码中:
/**
* find season ranking list from dc
* @param areaid 选手区域id
* @param rankdate 赛季
* @param category 类别
* @param charactername 角色名
* @return list
*/
public list findseasonrankinglist(long areaid, int rankyear,int rankmonth,
long categoryid,string charactername) {
//sql语句
stringbuffer sql = new stringbuffer(" select c.userid userid,d.posname posname,c.gameid gameid,c.amount amount,c.rank rank from ");
//表 sql.append(" (select b.userid userid,");
sql.append(" b.posid posid,");
sql.append(" a.district_code districtcode,");
sql.append(" a.gameid gameid,");
sql.append(" amount amount,");
sql.append(" rank rank ");
sql.append(" from tb_fs_rank a ");
sql.append(" left join tb_character_info b ");
sql.append(" on a.district_code = b.district_code ");
sql.append(" and a.gameid = b.gameid ");
//附加条件
if(areaid != null && areaid.intvalue() != 0){
sql.append(" and a.district_code = " + areaid.intvalue());
}
if( rankyear > 1970 && rankmonth > 0){
//hql.append(" and sas.id.dt >= to_date('" + rankyear + "-" + rankmonth + "-01 00:00:00'," + "yyyy-mm-dd hh24:mi:ss");
//hql.append(" and sas.id.dt <= to_date('" + rankyear + "-" + rankmonth + "-" + timetool.findmaxdateinmonth(rankyear,rankmonth) + " 23:59:59'," + "yyyy-mm-dd hh24:mi:ss");
sql.append(" and a.dt = fn_time_convert(to_date('" + rankyear + "-" + rankmonth + "'," + "'yyyy-mm')) ");
}
if(categoryid != null && categoryid.intvalue() != 0){
sql.append(" and a.cid = " + categoryid.intvalue());
}
if(charactername != null && !charactername.trim().equals("")){
sql.append(" and a.gameid = '" + charactername.trim()+"' ");
}
sql.append(" order by rank asc) c ");
sql.append(" left join tb_fs_position d ");
sql.append(" on c.posid = d.posid ");
sql.append(" order by c.rank ");
system.out.println("hql="+sql.tostring());////////////////
//使用自己的分页程序控制结果集
pagination pageinfo = new pagination(sql.tostring(),1,10,getjdbctemplate());
return pageinfo.getresultlist();
//return getjdbctemplate().queryforlist(sql.tostring());
}
闽公网安备 35060202000074号