/*** Tool class used for hql statement concatenation * @ author G-xia **/public class queryhelper {private string fromclause; // from clause private string whereclause = ""; // where clause private string orderbyclause = ""; // orderby clause private list <Object> parameters = new arraylist <Object> (); // parameter list/*** generate from clause ** @ Param clazz * @ Param alias */Public queryhelper (class clazz, string alias) {fromclause = "from" + clazz. getsimplename () + "" + Alias;}/*** concatenate the WHERE clause ** @ Param condition * @ Param ARGs */Public queryhelper addwherecondition (string condition, object... ARGs) {// concatenate if (whereclause. length () = 0) {whereclause = "where" + condition;} else {whereclause + = "and" + condition;} // processing parameter if (ARGs! = NULL & args. length> 0) {for (Object Arg: ARGs) {parameters. add (ARG) ;}} return this ;}/ *** if the value of the first parameter is true, concatenate the WHERE clause ** @ Param append * @ Param condition * @ Param ARGs */Public queryhelper addwherecondition (Boolean append, string condition, object... ARGs) {If (append) {addwherecondition (condition, argS);} return this ;} /*** splice the orderby clause ** @ Param propertyname * @ Param ASC * true indicates ascending order, and false indicates descending order */ Public queryhelper addorderbyproperty (string propertyname, Boolean ASC) {If (orderbyclause. Length () = 0) {orderbyclause = "order by" + propertyname + (ASC? "ASC": "DESC");} else {orderbyclause + = "," + propertyname + (ASC? "ASC": "DESC");} return this;}/*** if the value of the first parameter is true, concatenate the orderby clause ** @ Param append * @ Param propertyname * @ Param ASC */Public queryhelper addorderbyproperty (Boolean append, string propertyname, Boolean ASC) {If (append) {addorderbyproperty (propertyname, ASC);} return this;}/*** obtain the hql statement for querying the data list ** @ return */Public String getquerylisthql () {return fromclause + whereclause + orderbyclause;}/*** get Hql statement for querying the total number of records (without the orderby clause) ** @ return */Public String getquerycounthql () {return "select count (*)" + fromclause + whereclause ;} /*** get the parameter list ** @ return */public list <Object> getparameters () {return parameters ;} /*** prepare the pagebean object to the top of the struts2 stack * @ Param Service * @ Param pagenum */Public void preparepagebean (daosupport <?> Service, int pagenum) {pagebean = service. getpagebean (pagenum, this); actioncontext. getcontext (). getvaluestack (). Push (pagebean );}}
/*** Public method for querying paging information (Final Version) ** @ Param pagenum * @ Param queryhelper * query statement + parameter list * @ return */Public pagebean getpagebean (INT pagenum, queryhelper) {system. out. println ("------------> daosupportimpl. getpagebean (INT pagenum, queryhelper) "); // retrieves information such as pagesize int pagesize = configuration. getpagesize (); List <Object> parameters = queryhelper. getparameters (); // query the data list on one page. query = Ge Tsession (). createquery (queryhelper. getquerylisthql (); If (parameters! = NULL & parameters. size ()> 0) {// set the parameter for (INT I = 0; I <parameters. size (); I ++) {query. setparameter (I, parameters. get (I) ;}} query. setfirstresult (pagenum-1) * pagesize); query. setmaxresults (pagesize); List list = query. list (); // query the total number of records query = getsession (). createquery (queryhelper. getquerycounthql (); // note the space! If (parameters! = NULL & parameters. size ()> 0) {// set the parameter for (INT I = 0; I <parameters. size (); I ++) {query. setparameter (I, parameters. get (I) ;}} Long Count = (long) query. uniqueresult (); // query return New pagebean (pagenum, pagesize, Count. intvalue (), list );}
// Prepare paging data -- use queryhelpernew queryhelper (topic. class, "T") //. addwherecondition ("T. Forum =? ", Forum) //. addwherecondition (viewtype = 1)," T. type =? ", Topic. type_best) // 1 indicates that only the excellent posts are read. addorderbyproperty (orderby = 1), "T. lastupdatetime ", ASC) // 1 indicates that only the last update time is used. addorderbyproperty (orderby = 2), "T. posttime ", ASC) // indicates to sort by topic posting time only. addorderbyproperty (orderby = 3), "T. replycount ", ASC) // indicates to sort by the number of replies only. addorderbyproperty (orderby = 0), "(Case T. type when 2 then 2 else 0 end) ", false )//. addorderbyproperty (orderby = 0), "T. lastupdatetime ", false) // 0 indicates that all top posts are sorted by default (sorted in descending order of the last update time ). preparepagebean (topicservice, pagenum );
Page Information in JSP
<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% @ taglib prefix =" S "uri ="/Struts-tags "%> <% -- paging information -- %> <Div id = pageselectorbar> <Div id = pageselectormemo> page times: on the $ {currentpage}/$ {pagecount} page, the total number of records of $ {pagesize} is displayed: $ {recordcount} </div> <Div id = pageselectorselectorarea> <a href = "javascript: gotopage (1)" Title = "Homepage" style = "cursor: hand; "> </a> <% -- page number list -- %> <s: iterator begin = "% {beginpageindex}" End = "% {endpageindex}" Var = "num"> <s: If test = "# num! = Currentpage "> <% -- Non-current page with Link -- %> <SPAN class =" pageselectornum "style =" cursor: hand; "onclick =" gotopage ($ {num}); ">$ {num} </span> </S: If> <s: else> <% -- current page, no link -- %> <SPAN class = "pageselectornum pageselectorselected" >$ {num} </span> </S: else> </s: iterator> <a href = "javascript: gotopage ($ {pagecount})" Title = "last page" style = "cursor: hand; "> </a> to <select id =" PN "onchange =" gotopage (this. value) "> <s: iterator begin = "1" End = "% {pagecount}" Var = "num"> <option value = "$ {num}" >$ {num} </option> </s: iterator> </SELECT> <% -- enable select to select the current page by default -- %> <SCRIPT type = "text/JavaScript" >$ ("# PN "). val ("$ {currentpage }"); </SCRIPT> </div> <SCRIPT type = "text/JavaScript">/*** go to the specified page number * @ Param {object} pagenum */ function gotopage (pagenum) {// Method 1: // Window. Location. href = "forum_show.do? Id = $ {ID} & pagenum = "+ pagenum; // alert (" Please implement the gotopage () method! "); // Method 2: $ (" # pageform "). append ("<input type = 'den den 'name = 'pagenum' value = '" + pagenum + "'> "); // Add the pagenum form field $ ("# pageform "). submit (); // submit a form} </SCRIPT>