JavaWeb-Advanced Paging Query

1. Introduction

Advanced pagination query: display the content in the database on the page according to the conditions entered by the user (such as age, name, etc.), and those that do not meet the conditions will not be displayed
insert image description here

2. Parameters

Data passed from the front end to the back end

1、当前页码  -  currentPage  (当前第几页)
2、页容量    -  pageSize   (一页能够容纳多少条数据) 

5个条件
1、姓名 name
2、籍贯 address
3、邮箱 email
4、最小年龄 startAge
5、最大年龄 endAge

The data that the backend responds to the frontend

5个参数
1、当前页码  - currentPage
2、页容量    - pageSize
3、总记录数  - count  (数据库中总共有多少条数据)  
4、总页数    - totalPage ( (总记录数%页容量)==0 ? (总记录数/页容量): (总记录数/页容量)+15、数据(集合) - records  (从数据库中查询到的所有数据放入集合中后返回集合)

sql statement

1.总记录数 
	SELECT COUNT(1) FROM tb_userinfo WHERE 条件;
2、数据集合
	SELECT * FROM tb_userinfo WHERE 条件 LIMIT (currntPage-1, pageSize;

3. Steps

前端用的list.jsp页面,web层用的ListServlet , service层用的UserInfoService , dao层用的UserInfoDao


First step preparation

In order to facilitate the subsequent transfer of parameters, the 2 parameters and 5 conditions passed from the front end to the back end are encapsulated into the classes under the vo package. The vo package is dedicated to storing the data encapsulated by the front end to the back end. Page class, 5 conditions are encapsulated as Condition class
insert picture here
注意: the total number of pages in the encapsulated Page class totalPage cannot provide the set method, which needs to be obtained through a formula, but the total number of pages obtained without the set method is the default value null, So you need to modify the return value of the total number of pages of get
insert image description here


The second step is to add code in ListServlet

1. Specify the code

2. Obtain the currentPage, pageSize, and various conditions passed in from the front end

3. Judgment and initialize the obtained values, initialize currentPage=1, pageSize=3, startAge=null, endAge=null
      if the front-end passes it If the value is not empty and not an empty string, assign the value passed from the front end to currentpage...

4. Create an object of the Condition condition class, and assign the values ​​​​passed by the front end one by one through the set method of the class object

. 5. Create The object of the service layer implements the class, calls the object, 获取带条件查询的queryByCondition(currentPage,pageSize , Condition condition)方法得到满足条件的多个对象

6, stores the parameter returned by the query method queryByCondition() as the Page parameter in the request field

7, stores the condition object Condition condition in the request field

8, and forwards it to The original list.jsp page


The third step is to write the method in the Service layer

1. Create the implementation class UserInfoServiceImpl of the UserInfoService interface , and implement the method queryByCondition(...) called by UserInfoService.

2. Assign a value to the Page class in this method (the reason for not assigning a value in the ListServlet class: because the total number of records count in the Page needs to be called The database is queried, and the database needs to be called to obtain the data that meets the conditions. According to the web three-tier architecture, the Servlet layer cannot directly call the method of querying the database in the dao layer across layers) 3. Return the Page object whose data type is

insert image description here
UserInfo


The fourth step is to write the corresponding method in the dao layer

1. Create the UserInfoDaoImpl class to implement the methods in the UserInfoDao interface, count(...) and query()
[In order to distinguish the method of the service layer from the method of the dao layer, the dao layer renames the query method as queryByCondition (the parameters are the same)]

2 , write code in count(…) as follows
insert image description here


3, queryByCondition(…) method as follows
insert image description here


4, method of concatenating strings
insert image description here


The fifth step is to fill the data into the page through EL expressions + JSPL tags
In the first step, the data obtained from the database and the conditions passed in by the front end have been stored in the domain. Now it is necessary to obtain the data from the domain.
insert image description here

注意: If the result is obtained through the conditional query, but when the page number is clicked (that is, the first page and the second page), the page will request the Servlet again. If the condition box is empty at this time, the condition will no longer exist, and the detected data will not Conditional data, this is a bug, you need to add a hidden field with a value of 1 and a type of hidden in the query form. When you click on the page number, submit the hidden field (used as currentPage) together. When you click on the page number, pass js The code submits the form form
insert image description here

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/qq_52998673/article/details/127559106