选择显示字体大小

使用spring的jdbctemplate实现分页功能

[j2me]使用spring的jdbctemplate实现分页功能

作者:潘顺昌


版权声明:本文可以自由转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明
作者:潘顺昌(http://www.matrix.org.cn/user.shtml?username=panshunchang)
原文:http://www.matrix.org.cn/resource/article/43/43976_jdbc_template.html
关键字:sping jdbctemplate

最近使用了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(&quot;  ) temp where rownum <= &quot; + lastindex);
                paginationsql.append(&quot; ) where  num > &quot; + startindex);
                //装入结果集
                setresultlist(getjdbctemplate().queryforlist(paginationsql.tostring()));
        }
        /**
         * @param args
         */
        public static void main(string&#91;&#93; 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(&quot;totalrows=&quot;+totalrows);///////////
                system.out.println(&quot;numperpage=&quot;+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(&quot; select c.userid userid,d.posname posname,c.gameid gameid,c.amount amount,c.rank rank from &quot;);
                        //表
                        sql.append(&quot; (select b.userid userid,&quot;);
                        sql.append(&quot; b.posid posid,&quot;);
                        sql.append(&quot; a.district_code districtcode,&quot;);
                        sql.append(&quot; a.gameid gameid,&quot;);
                        sql.append(&quot; amount amount,&quot;);
                        sql.append(&quot; rank rank &quot;);
                        sql.append(&quot; from tb_fs_rank a &quot;);
                        sql.append(&quot; left join tb_character_info b &quot;);
                        sql.append(&quot; on a.district_code = b.district_code &quot;);
                        sql.append(&quot; and a.gameid = b.gameid &quot;);
                        //附加条件
                        if(areaid != null && areaid.intvalue() != 0){
                                sql.append(&quot; and a.district_code = &quot; + areaid.intvalue());
                        }
                        if( rankyear > 1970 && rankmonth > 0){
                                //hql.append(&quot; and sas.id.dt >= to_date('&quot; + rankyear + &quot;-&quot; + rankmonth + &quot;-01 00:00:00',&quot; + &quot;yyyy-mm-dd hh24:mi:ss&quot;);
                                //hql.append(&quot; and sas.id.dt <= to_date('&quot; + rankyear + &quot;-&quot; + rankmonth + &quot;-&quot; + timetool.findmaxdateinmonth(rankyear,rankmonth) + &quot; 23:59:59',&quot; + &quot;yyyy-mm-dd hh24:mi:ss&quot;);
                                sql.append(&quot; and a.dt = fn_time_convert(to_date('&quot; + rankyear + &quot;-&quot; + rankmonth + &quot;',&quot; + &quot;'yyyy-mm')) &quot;);
                        }
                        if(categoryid != null && categoryid.intvalue() != 0){
                                sql.append(&quot; and a.cid = &quot; + categoryid.intvalue());
                        }
                        if(charactername != null && !charactername.trim().equals(&quot;&quot;)){
                                sql.append(&quot; and a.gameid = '&quot; + charactername.trim()+&quot;' &quot;);
                        }
                        sql.append(&quot; order by rank asc) c &quot;);
                        sql.append(&quot; left join tb_fs_position d &quot;);
                        sql.append(&quot; on c.posid = d.posid &quot;);
                        sql.append(&quot; order by c.rank &quot;);
                        system.out.println(&quot;hql=&quot;+sql.tostring());////////////////
                                                 //使用自己的分页程序控制结果集
                        pagination pageinfo = new pagination(sql.tostring(),1,10,getjdbctemplate());
                        return pageinfo.getresultlist();
                        //return getjdbctemplate().queryforlist(sql.tostring());
        }



资源
&middot;matrix-java开发者社区:http://www.matrix.org.cn/




 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons