|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
java也能做一些底层语言开发做的事情(难度很高,不是java顶尖高手是做不来的),
比来利用了spring中的JdbcTemplate完成数据库的查询和拔出操纵,发明spring的JdbcTemplate不象HibernateTemplate那末好,已完成了分页功效。以是要本人完成,利用getJdbcTemplate().queryForList(stringsql)失掉的了局集是一切的。
假如你的查询有10000笔记录,大概更多,速率一定慢了,固然你能够经由过程resultset中的游标把持查询的肇端和停止。我这里用的是Oracle数据库,利用伪列ROWNUM来完成分页。我的分页代码以下:
packagecom.deity.ranking.util;importjava.util.List;
importorg.springframework.jdbc.core.JdbcTemplate;
importorg.springframework.jdbc.core.support.JdbcDaoSupport;
/***分页函数* *@authorallenpan*/publicclassPaginationextendsJdbcDaoSupport{
publicstaticfinalintNUMBERS_PER_PAGE=10;
//一页显现的纪录数
privateintnumPerPage;
//纪录总数
privateinttotalRows;
//总页数
privateinttotalPages;
//以后页码
privateintcurrentPage;
//肇端行数
privateintstartIndex;
//停止行数
privateintlastIndex;
//了局集寄存List
privateListresultList;
//JdbcTemplatejTemplate
privateJdbcTemplatejTemplate;
/**
*每页显现10笔记录的机关函数,利用该函数必需先给Pagination设置currentPage,jTemplate初值
*@paramsqloracle语句
*/
publicPagination(Stringsql){
if(jTemplate==null){
thrownewIllegalArgumentException("com.deity.ranking.util.Pagination.jTemplateisnull,pleaseinitialitfirst.");
}elseif(sql.equals("")){
thrownewIllegalArgumentException("com.deity.ranking.util.Pagination.sqlisempty,pleaseinitialitfirst.");
}
newPagination(sql,currentPage,NUMBERS_PER_PAGE,jTemplate);
}
/**分页机关函数
*@paramsql依据传进的sql语句失掉一些基础分页信息
*@paramcurrentPage以后页
*@paramnumPerPage每页纪录数
*@paramjTemplateJdbcTemplate实例
*/
publicPagination(Stringsql,intcurrentPage,intnumPerPage,JdbcTemplatejTemplate){
if(jTemplate==null){
thrownewIllegalArgumentException("com.deity.ranking.util.Pagination.jTemplateisnull,pleaseinitialitfirst.");
}elseif(sql==null||sql.equals("")){
thrownewIllegalArgumentException("com.deity.ranking.util.Pagination.sqlisempty,pleaseinitialitfirst.");
}
//设置每页显现纪录数
setNumPerPage(numPerPage);
//设置要显现的页数
setCurrentPage(currentPage);
//盘算总纪录数
StringBuffertotalSQL=newStringBuffer("SELECTcount(*)FROM(");
totalSQL.append(sql);
totalSQL.append(")totalTable");
//给JdbcTemplate赋值
setJdbcTemplate(jTemplate);
//总纪录数
setTotalRows(getJdbcTemplate().queryForInt(totalSQL.toString()));
//盘算总页数
setTotalPages();
//盘算肇端行数
setStartIndex();
//盘算停止行数
setLastIndex();
System.out.println("lastIndex="+lastIndex);//////////////////
//机关oracle数据库的分页语句
StringBufferpaginationSQL=newStringBuffer("SELECT*FROM(");
paginationSQL.append("SELECTtemp.*,ROWNUMnumFROM(");
paginationSQL.append(sql);
paginationSQL.append(" )tempwhereROWNUM<="+lastIndex);
paginationSQL.append(")WHERE num>"+startIndex);
//装进了局集
setResultList(getJdbcTemplate().queryForList(paginationSQL.toString()));
}
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub }
publicintgetCurrentPage(){
returncurrentPage;
}
publicvoidsetCurrentPage(intcurrentPage){
this.currentPage=currentPage;
}
publicintgetNumPerPage(){
returnnumPerPage;
}
publicvoidsetNumPerPage(intnumPerPage){
this.numPerPage=numPerPage;
}
publicListgetResultList(){
returnresultList; }
publicvoidsetResultList(ListresultList){
this.resultList=resultList;
}
publicintgetTotalPages(){
returntotalPages;
}
//盘算总页数
publicvoidsetTotalPages(){
if(totalRows%numPerPage==0){
this.totalPages=totalRows/numPerPage;
}else{
this.totalPages =(totalRows/numPerPage)+1;
}
}
publicintgetTotalRows(){
returntotalRows;
}
publicvoidsetTotalRows(inttotalRows){
this.totalRows=totalRows;
}
publicintgetStartIndex(){
returnstartIndex;
}
publicvoidsetStartIndex(){
this.startIndex=(currentPage-1)*numPerPage;
}
publicintgetLastIndex(){
returnlastIndex;
}
publicJdbcTemplategetJTemplate(){
returnjTemplate;
}
publicvoidsetJTemplate(JdbcTemplatetemplate){
jTemplate=template;
}
//盘算停止时分的索引
publicvoidsetLastIndex(){
System.out.println("totalRows="+totalRows);///////////
System.out.println("numPerPage="+numPerPage);///////////
if(totalRows<numPerPage){
this.lastIndex=totalRows;
}elseif((totalRows%numPerPage==0)||(totalRows%numPerPage!=0&¤tPage<totalPages)){
this.lastIndex=currentPage*numPerPage;
}elseif(totalRows%numPerPage!=0&¤tPage==totalPages){//最初一页
this.lastIndex=totalRows;
}
}}在我的营业逻辑代码中:
/**
*findseasonrankinglistfromDC
*@paramareaId选手地区id
*@paramrankDate赛季
*@paramcategory种别
*@paramcharacterName脚色名
*@returnList
*/
publicListfindSeasonRankingList(LongareaId,intrankYear,intrankMonth,
LongcategoryId,StringcharacterName){
//SQL语句
StringBuffersql=newStringBuffer("SELECTC.USERIDuserid,D.POSNAMEposname,C.GAMEIDgameid,C.AMOUNTamount,C.RANKrankFROM");
//表 sql.append("(SELECTB.USERIDUSERID,");
sql.append("B.POSIDPOSID,");
sql.append("A.DISTRICT_CODEDISTRICTCODE,");
sql.append("A.GAMEIDGAMEID,");
sql.append("AMOUNTAMOUNT,");
sql.append("RANKRANK");
sql.append("FROMTB_FS_RANKA");
sql.append("LEFTJOINTB_CHARACTER_INFOB");
sql.append("ONA.DISTRICT_CODE=B.DISTRICT_CODE");
sql.append("ANDA.GAMEID=B.GAMEID");
//附加前提
if(areaId!=null&&areaId.intValue()!=0){
sql.append("andA.DISTRICT_CODE="+areaId.intValue());
}
if(rankYear>1970&&rankMonth>0){
//hql.append("andsas.id.dt>=to_date("+rankYear+"-"+rankMonth+"-0100:00:00,"+"YYYY-MM-DDHH24:MI:SS");
//hql.append("andsas.id.dt<=to_date("+rankYear+"-"+rankMonth+"-"+TimeTool.findMaxDateInMonth(rankYear,rankMonth)+"23:59:59,"+"YYYY-MM-DDHH24:MI:SS");
sql.append("andA.DT=fn_time_convert(to_date("+rankYear+"-"+rankMonth+","+"YYYY-MM))");
}
if(categoryId!=null&&categoryId.intValue()!=0){
sql.append("andA.CID="+categoryId.intValue());
}
if(characterName!=null&&!characterName.trim().equals("")){
sql.append("andA.GAMEID="+characterName.trim()+"");
}
sql.append("ORDERBYRANKASC)C");
sql.append("LEFTJOINTB_FS_POSITIOND");
sql.append("ONC.POSID=D.POSID");
sql.append("ORDERBYC.RANK");
System.out.println("hql="+sql.toString());////////////////
//利用本人的分页程序把持了局集
PaginationpageInfo=newPagination(sql.toString(),1,10,getJdbcTemplate());
returnpageInfo.getResultList();
//returngetJdbcTemplate().queryForList(sql.toString());
}
主要缺点就是:速度比较慢,没有C和C++快 |
|