Use Spring's JDBCTemplate to implement paging features

xiaoxiao2021-04-08  376

Recently, JDBCTemplate in Spring implements the query and insert operation of the database, found that Spring's JDBCTemplate is not as good as HibernateTemplate, has implemented paging feature. So you have to realize it yourself, using getjdbctemplate (). QueryforList (String SQL) result set is all.

If your query has 10,000 records, or more, the speed is sure slow, and of course you can control the start and end of the query through the cursor in the Resultset. I use an Oracle database here to implement pagination using pseudo colum rownum. My page code is as follows:

package com.deity.ranking.util; import java.util.List; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.support.JdbcDaoSupport; / ** * * * @author paging function allenpan * / public class Pagination extends JdbcDaoSupport {public static final int NUMBERS_PER_PAGE = 10; number of records displayed in a private // int numPerPage; // record the total number of private int totalRows; // total number of pages private int totalPages; // current page private int currentPage; // start line number private int startIndex; // end rows private int lastIndex; // store the result set List private List resultList; // JdbcTemplate jTemplate private JdbcTemplate jTemplate; / ** * 10 per The recorded constructor, using this function must first set the CurrentPage, JTemplate initial value * @Param SQL Oracle statement * / public PAGINATION (String SQL) {if (Jtemplate == Null) {throw new illegalargumentexception ("com.deity. Ranking.util.pagination.jtemplate is Null, Plea Se initial it first. ");} else if (SQL.Equals (")) {throw new illegalargumentException ("com.deity.ranking.util.pagination.sql is empty, please initial it first."); PAGINATION (SQL, CURRENTPAGE, NUMBERS_PER_PAGE, JTEMPLATE);} / ** Paging Constructor * @Param SQL A Some Basic Patement Information * @Param CurrentPage Current Page * @Param NumPage JTemplate JDBCTemplate Instance * / Public Pagination (String SQL, INT CurrentPage, Int NumPage, 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.");} // Set the number of records in each page setNumPage (NumperPage); // Set the number of pages to display setCurrentPage CurrentPage); // Calculate total record number StringBuffer Totalsql = new stringbuffer ("Select Count (*) from ("); Totalsql.Append (SQL); Totalsql.Append (") TotalTable"); // Give JDBCTemplate assignment setjdbcTemplate JTemplate); // Total record number settotalrows (getjdbcTemplate (). Queryforint (Totalsql.tostring ())))); // Calculate the total number of pages settotalpages (); // calculate the number of startings setStartIndex (); // Calculations Bunch of lines setLastIndex (); system.out.println ("LastIndex =" lastindex); // Structure StringBuffer PagingSql = 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); / / Loading result set setResultList (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;} // Calculate total page PUBLIC VOID setTotalpages () {i (Totalrows% NumperPage == 0) {this.totalpages = TotalRows / NumperPage;} else {this.totalpage s = (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;

} // Calculate the index of the index public void setlastindex () {system.out.println ("TotalRows =" TotalRows; /// system.out.println ("NumperPage =" NumperPage); /// i ( totalRows

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 "); // Additional condition 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 = '"

转载请注明原文地址:https://www.9cbs.com/read-132711.html

New Post(0)