The project uses the AJAX technology query, the query result many times must the page display. These two days groped for a moment, here to do a summary, convenient for you to view at any time,
Also convenient for posterity reference.
Here's the order of compliance from the foreground page to the backend controller, Business layer, DAO layer, Mapper
The following first page, the page JS code is as follows:
<span style= "FONT-SIZE:14PX;" >/* global variable */var usercount;//The total number of users matching the search criteria, page reference var pageIndex = 0;//The current page, default to 0var PageSize = 8;//display per page 8//conditionally find user function Searchuser (index,size) {var findterm = $ ("#serchTerm"). Val (); var Provinceid = $ (' #province '). Val (); var Cityid = $ (' # City '). Val (); $.ajax ({type: "POST", url: "User/findcontactsajax", Cache:false,data: {provinceid:provinceid,cityid:ci Tyid,pageindex:index,pagesize:size},async:true,error:function () {Alert ("Network exception! ");},success:function (data) {<span style=" White-space:pre "></span>usercount=math.ceil (Data[0]. USERCOUNT/8) <span style= "White-space:pre" ></span>var page= ' <div id= "userPage" align= "Center" > <font size= "2" > Total ' +usercount+ ' page </font> <font size= "2" > First ' + (pageindex+1) + ' page </font> <a href= "Javascript:void" onclick= "Gotofirstpage ()" id= "Afirstpage" > Home </a> ' + ' <a href= "javascript:void" Onclick= "Gotoprepage ()" id= "Aprepage" > Previous </a> ' + ' <a href= "Javascript:void" onclick= "Gotonextpage ()" id= "Anextpage" > Next </a> ' + ' <a href= " Javascript:void "onclick=" Gotoendpage () "id=" Aendpage "> Last </a> ';p age+= ' </div> '; $ (" # Serchresult "). Append (page);d Ocument.getelementbyid (" Dltitle "). InnerHTML =" Find the result as follows ";}});} Home function Gotofirstpage () {pageIndex = 0; Searchuser (PageIndex, pageSize);} Previous function Gotoprepage () {pageIndex-= 1; PageIndex = PageIndex >= 0? pageindex:0; Searchuser (PageIndex, pageSize);} Next function Gotonextpage () {if (PageIndex + 1 < usercount) {pageIndex + = 1; } searchuser (PageIndex, pageSize);} Last function Gotoendpage () {pageIndex = userCount-1; Searchuser (PageIndex, pageSize);} </span>
The control layer code is as follows:
@RequestMapping ("Findcontactsajax") public @ResponseBodyMap <string, object> findcontactajax (String Provinceid , String cityid,string pageindex,string pageSize) {list<user> listUsers = userdao.selectuserbyprovinceandcity ( Provinceid, Cityid,pageindex,pagesize)}map.put ("User", listUsers); return map;}
DAO Layer:
List<user> selectuserbyprovinceandcity (@Param ("Provinceid") integer Provinceid, @Param ("Cityid") integer Cityid, @Param ("PageIndex") integer pageIndex, @Param ("pageSize") integer pageSize);
Mapper File:
<select id= "selectuserbyprovinceandcity" resultmap= "Baseresultmap" >select *, (select COUNT (*) from User_ user_t Province_id=#{provinceid} and City_id=#{cityid}) as Usercountfrom User_user_tprovince_id=#{provinceid} and City _id=#{cityid}limit #{pageindex},#{pagesize}</select>
User entity
public class User { private Integer userId; Private String userName; Private Integer Provinceid; Private Integer Cityid; Private Integer usercount;//The number of users that meet the query criteria as the basis for paging}
Springmvc+ajax Paging Query