SPRINGJDBC implementing MySQL Simple paging

Source: Internet
Author: User

:

The front-desk JSP code is as follows:

<Divclass= "Listnav">                    <Divclass= "instruction">you are viewing the results of section ${start+1}-${limit} in ${result} results. </Div>                        <fieldset><legend>Home</legend>                        <inputtype= "button"name= "Eventsubmit_dolist_first"onclick= "Javascript:window.location.href= ' ${pagecontext.request.contextpath}/cw/allsitecourse.do?start=0& Limit=${limit} ' "value= "|<">                        </fieldset>                        <fieldset><legend>Previous page</legend>                        <inputtype= "button"name= "Eventsubmit_dolist_prev"onclick= "Javascript:window.location.href= ' ${pagecontext.request.contextpath}/cw/allsitecourse.do?start=${ Start-limit}&limit=${limit} ' "value= "<">                        </fieldset>                        <Selectname= "Selectpagesize"ID= "Selectpagesize"onchange= "Changepager (this.value)">                            <optionvalue= "5">Show 5 items ...</option>                            <optionvalue= "Ten">Show 10 items ...</option>                            <optionvalue= " the"selected>Show 20 items ...</option>                            <optionvalue= " the">Show 50 items ...</option>                            <optionvalue= "+">Show 100 Items ...</option>                            <optionvalue= "$">Show 200 items ...</option>                        <fieldset><legend>Next page</legend>                        <inputtype= "button"name= "Eventsubmit_dolist_next"onclick= "Javascript:window.location.href= ' ${pagecontext.request.contextpath}/cw/allsitecourse.do?start=${start+ Limit}&limit=${limit} ' "value= ">">                        </fieldset>                        <fieldset><legend>Last</legend>                        <inputtype= "button"name= "Eventsubmit_dolist_last"onclick= "javascript:window.location.href= ' ${pagecontext.request.contextpath}/cw/allsitecourse.do?start=${result} &limit=${limit} ' "value= ">|">                        </fieldset>                    </Div>

The controller is as follows:

@RequestMapping (value= "/allsitecourse", method=requestmethod.get) PublicString allsitecourse (httpservletrequest request,pager Pager) {//querying all of the bars in the database        intresult =Cwinfoservice.queryallsitescount (); //Set Page PagingPager =Cwinfoservice.setpage (pager, result); List<Sites> sitelist =cwinfoservice.queryallsites (Pager); Request.setattribute ("Sitelist", sitelist); Request.setattribute ("Start", Pager.getstart ()); Request.setattribute ("Limit", Pager.getlimit ()); Request.setattribute ("Result", result); return"/jsp/coursewareselectallsites"; }

Service, and the implementation class are as follows:

/**      * Description: Query out the total number of     all sites @return     *    /int  Queryallsitescount ( );     /**      * Description: Set     The starting bar number of pagination @param  Pager     @return     */      Pager setpage (Pager Pager,int result);

@Override Public intQueryallsitescount () {returnCwinfodao.queryallsitescount (); } @Override PublicPager setpage (Pager Pager,intresult) {        //total number of pages                intPageCount = 0; //number of starting bars on the last page                intLaststart = 0; if(Pager.getlimit ()! =NULL){                    //determine whether the last page is full based on the remainder                    if(Result%pager.getlimit () ==0){                        //calculates how many pages the current data has in totalPageCount = result/Pager.getlimit (); //The current number of pages is less than one page                        if(pagecount<=1) {Laststart= 0; }Else{Laststart= result-Pager.getlimit (); }                    }Else{PageCount= (Result/pager.getlimit ()) +1; if(pagecount<=1) {Laststart= 0; }Else{Laststart= (pageCount-1) *Pager.getlimit (); }                    }                }                //Set Page Paging                if(! (Pager.getstart ()! =NULL&&pager.getlimit ()! =NULL) {Pager.setstart (0); Pager.setlimit (20); //negative condition on previous page}Else if(Pager.getstart () <0) {Pager.setstart (0); //The current page does not have enough data for one page}Else if(Result-pager.getlimit () <0) {Pager.setstart (0); //the next page appears out of date}Else if(Pager.getstart () >=result)                {Pager.setstart (Laststart); }        returnPager; }

DAO and the underlying SPRINGJDBC:

/**      * Description: Query All site     information @return     */    List<Sites>  Queryallsites (Pager Pager);     /**      * Description: Query out the total number     of all sites @return     */    int Queryallsitescount ();

@Override PublicList<sites>queryallsites (Pager Pager) {String SQL= "SELECT * from Sakai_site ORDER by site_id limit?,?"; object[] obj=NewObject[]{pager.getstart (), Pager.getlimit ()}; List<Sites> list = Jdbctemplate.query (Sql,obj,NewSitemapper ()); returnlist; } @Override Public intQueryallsitescount () {String SQL= "SELECT count (*) from Sakai_site"; intresult = Jdbctemplate.queryforobject (Sql,integer.class); returnresult; }

classSitemapperImplementsRowmapper<sites>{@Override PublicSites Maprow (ResultSet RS,intRowNum)throwsSQLException {Sites site=NewSites (); Site.setcreatedby (Rs.getstring ("CreatedBy")); Site.setcreatedon (Rs.getdate ("CreatedOn")); Site.setcustompageordered (Rs.getstring ("Custom_page_ordered")); Site.setdescription (Rs.getstring ("DESCRIPTION")); Site.seticonurl (Rs.getstring ("Icon_url")); Site.setinfourl (Rs.getstring ("Info_url")); Site.setissoftlydeleted (Rs.getstring ("Is_softly_deleted")); Site.setisspecial (Rs.getstring ("Is_special")); Site.setisuser (Rs.getstring ("Is_user")); Site.setjoinable (Rs.getstring ("Joinable")); Site.setjoinrole (Rs.getstring ("Join_role")); Site.setmodifiedby (Rs.getstring ("ModifiedBy")); Site.setmodifiedon (Rs.getdate ("ModifiedOn")); Site.setpublished (Rs.getint ("PUBLISHED")); Site.setpubview (Rs.getstring ("Pubview")); Site.setshortdesc (Rs.getstring ("Short_desc")); Site.setsiteid (Rs.getstring ("SITE_ID")); Site.setskin (Rs.getstring ("SKIN")); Site.setsoftlydeleteddate (Rs.getdate ("Softly_deleted_date")); Site.settitle (Rs.getstring ("TITLE")); Site.settype (Rs.getstring ("TYPE")); returnsite; }}

There is also a short JS:

// page Out function Changepager (obj) {    window.location.href=ctx + "/cw/allsitecourse.do?start=0&limit=" +obj;} $ (document). Ready (function() {    if(limit!= ') {        $ (' option[ Value= ' +limit+ '). attr (' selected ',true);    });

SPRINGJDBC implementing MySQL Simple paging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.