Paging and multi-condition query functions and paging condition Functions
/*** 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 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 queryHelper) {System. out. println ("------------> DaoSupportImpl. getPageBean (int pageNum, QueryHelper queryHelper) "); // retrieves information such as pageSize int pageSize = Configuration. getPageSize (); List <Object> parameters = queryHelper. getParameters (); // Query the data list on one page. query 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>
SQL paging multi-condition Query
The query can be done using if else.
For example:
SQL = "select * from ........."
''''''''''''''
Xh = request ("model ")
······
If xh <> "" then SQL = SQL + "and model = '" & xh &"'"
Paging is available
<% TurnPage (rs, 20, "model =" & xh & "& other six parameters...) %>
<%
Sub TurnPage (ByRef Rs_tmp, PageSize, canshu) 'rs _ tmp record set PageSize the number of records displayed on each page;
Total Dim TotalPage pages
Dim pageno' current page number
Dim RecordCount 'Total number of records
Rs_tmp.PageSize = PageSize
RecordCount = Rs_tmp.RecordCount
TotalPage = INT (RecordCount/PageSize *-1) *-1
PageNo = Request. QueryString ("PageNo ")
'Directly enter the page number to jump;
If Request. Form ("PageNo") <> "" Then PageNo = Request. Form ("PageNo ")
'If no page is selected, the first page is displayed by default;
If PageNo = "" then PageNo = 1
If RecordCount <> 0 then
Rs_tmp.AbsolutePage = PageNo
End If
'Get the current file name so that every page turning is performed on the current page;
Dim fileName, postion
FileName = Request. ServerVariables ("script_name ")
Postion = Limit Rev (fileName, "/") + 1
'Get the name of the current file and direct the page turning link to the current file;
FileName = Mid (fileName, postion)
Response. write "<table border = 0 width = '000000'> <tr>"
If RecordCount = 0 or TotalPage = 1 Then
Response. Write ""
Else
Response. write "<td align = left style = 'font-size: 12px '> total number of pages: <font color = # ff3333>" & TotalPage & "</font> page"
Response. write "Current <font color = # ff3333>" & PageNo & "</font> page <& #47 ...... remaining full text>
Pagination in the GridView multi-condition query !!!
You do not need to have two functions to bind data first. You only need to determine the situation and then read different DataTable data.
Second, in the query button event, remember to restore the page number to 0.
Finally, you need to determine which event triggers the binding and determine how to read the DataTable. That is, whether the "query" button is clicked.
Public partial class Default3: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
Label1.Text = Session ["name"] as string;
ViewState ["search"] = 0; // set the Initial State to non-Query
BinData ();
}
}
Protected void BinData ()
{
UserDB db = new UserDB ();
// Determine whether the query button is clicked and the text box contains values
DataTable table = TextBox1.Text. Trim (). Length> 0 & ViewState ["search"]. ToString (). Equals ("1 ")? Db. InsertN (TextBox1.Text. Trim (): db. InsertUsern ();
PagedDataSource PPS = new PagedDataSource ();
PPS. DataSource = table. DefaultView;
PPS. AllowPaging = true;
AspNetPager1.RecordCount = PPS. performancecount;
PPS. CurrentPageIndex = AspNetPager1.CurrentPageIndex-1;
PPS. PageSize = AspNetPager1.PageSize;
This. GridView1.DataSource = pds;
This. GridView1.DataBind ();
}
Protected void AspNetPager1_PageChanging (object src, Wuqi. Webdiyer. PageChangingEventArgs e)
{
AspNetPager1.CurrentPageIndex = e. NewPageIndex;
ViewState ["search"] = 0; // restore the initial state
BinData ();
}
// Query button
Protected void button#click (object sender, EventArgs e)
{
AspNetPager1.CurrentPageIndex = 0;
ViewState [&... the remaining full text>