A spring and mybatis-based paging query method
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-07-10
- Publication Date
- 2026-03-24
AI Technical Summary
而PageHelper不能将count的结果单独返回
[0025]本发明的有益效果在于:在所述基于spring和mybatis的分页查询方法中,mapper里的方法可返回定义的分页类型。当前方法可以直接将mypage对象返回给调用者即可;当需要只返回count时,@PageSize所标注的参数传0,此时将执行count sql,只返回count结果;当需要只返回List时,@PageSize所标注的参数传Integer.MAX_VALUE,此时将执行Listsql,只返回List结果。如此,使用者可以定义并使用自己的分页对象,还能单独只获取count结果或List结果。
Smart Images

Figure CN116842040B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of data processing technology, and in particular to a pagination query method based on Spring and MyBatis. Background Technology
[0002] Currently, the most widely used MyBatis pagination solution on the market is the com.github.pagehelper pagination solution, whose usage is as follows: Figure 1 . Figure 1 "List" <inserttest>The full definition of the `List` method in `mapper.list("hello")` is: `List = mapper.list("hello")`. <inserttest>list(String s). As can be seen from the figure, the original method with the return type of List, after the execution of PageHelper.startPage before the method, will return the Page type of pagehelper, and the returned result can be forcibly converted into the Page type.
[0003] In actual use, each company will define its own paging object type, so after obtaining the Page type of pagehelper, as shown in Figure 2 , the type is often converted into the paging object type used by itself.
[0004] Secondly, whether to use paging is determined by whether the current method uses PageHelper.startPage. As the caller of the current method, it is impossible to determine by the caller whether to return the paging or List.
[0005] Finally, for the sql statement corresponding to the List method of the mapper, the sql statement for generating count can be used. PageHelper cannot return the result of count alone. In actual use, sometimes because list is not needed, in order to optimize the performance, it is hoped that only the sql statement for count is executed and the result is obtained, and the sql statement for returning List is not executed. SUMMARY
[0006] In order to overcome the defects existing in the prior art, the application provides a paging query method based on spring and mybatis to solve the above problems.
[0007] The technical scheme adopted by the application to solve the technical problems is: a paging query method based on spring and mybatis, comprising the following steps:
[0008] S1: when the method of the spring dao layer is called, the call obtains the method information of the current method of the spring dao layer through the interception layer DaoPageInterceptor, wherein the method information includes the return type of the method and the parameters of the method;
[0009] The interception layer DaoPageInterceptor takes out the return type of the method and finds the List field and the count field;
[0010] S2: the interception layer DaoPageInterceptor finds two parameters annotated by @CurrentPage and @PageSize in the parameters of the method;
[0011] S3: Record the return type returnType = {class information of the return type}, currentPage = {parameter marked by @CurrentPage}, and pageSize = {parameter marked by @PageSize} on the sql context SqlContext;
[0012] S4: After reaching the mybatis paging plug-in PagingInterceptor, first take the value of currentPage and the value of pageSize from the sql context SqlContext;
[0013] S5: If the value of pageSize is 0, only return the count result; if the value of pageSize is equal to Integer.MAX_VALUE, only return the List result;
[0014] S6: When the returned List result reaches the mybatis paging plug-in PagingInterceptor, take the return type returnType from the sql context SqlContext through the interface method boolean hasWrapperFor(Object object);
[0015] S7: The interface method ObjectWrapper getWrapperFor(MetaObject metaObject, Object object) of mybatis creates a return object according to the return type returnType, and creates a paging wrapper object PageObjectWrapper, encapsulates the return object into the paging wrapper object PageObjectWrapper, and finally returns the paging wrapper object PageObjectWrapper;
[0016] S8: After getting the List result returned by the mybatis executor, the mybatis calls the addAll method of the PageObjectWrapper object;
[0017] Finally, mybatis takes the return object from the paging wrapper object PageObjectWrapper and returns it to the method of the springdao layer.
[0018] It is worth mentioning that in the step S1, when the method of the spring dao layer is called, the method acquires the method information currently called by the spring dao layer through the parameter pj in the Object doAround(ProceedingJoinPoint pj) method in the intercepting layer DaoPageInterceptor.
[0019] Optionally, in the step S1, the intercepting layer DaoPageInterceptor finds the fields marked by @List and @Amount after taking the return type of the method.
[0020] Specifically, in the step S5, if the value of pageSize is equal to 0, the countsql is obtained through the original sql statement, then only the countsql statement is executed, the result pagecount={count result} is recorded in the sql context SqlContext, and the original sql statement is modified to select 1 from dual where 1=0, the modified original sql statement is executed and an empty List field is returned.
[0021] Preferably, in the step S5, if the value of pageSize is equal to Integer.MAX_VALUE, the original sql statement is executed, and the List result is returned.
[0022] It is worth mentioning that in the step S5, if the value of pageSize is not 0 and the value of pageSize is not equal to Integer.MAX_VALUE, the countsql result and the pageSql result are obtained through the original sql statement, the countsql result pagecount={count result} is recorded in the sql context SqlContext, and the pageSql result is returned.
[0023] Optionally, in the step S8, the mybatis calls the addAll method of the PageObjectWrapper object, including: the result of the input List is placed in the List field marked by @List in the returned object by using the reflection mechanism of java;
[0024] The pageCount in the sql context SqlContex is placed in the count field marked by @Amount in the returned object by using the reflection mechanism of java.
[0025] The beneficial effects of this invention are as follows: In the pagination query method based on Spring and MyBatis, the methods in the mapper can return the defined pagination type. The current method can directly return the `mypage` object to the caller; when only the count needs to be returned, the parameter annotated with `@PageSize` is passed as 0, in which case a count SQL query will be executed, returning only the count result; when only the List needs to be returned, the parameter annotated with `@PageSize` is passed as `Integer.MAX_VALUE`, in which case a list SQL query will be executed, returning only the list result. Thus, users can define and use their own pagination objects, and can also retrieve only the count result or the list result separately. Attached Figure Description
[0026] Figure 1 This refers to the code for an existing MyBatis pagination scheme.
[0027] Figure 2 This is the existing code that converts the Page type to the pagination object type used by the user.
[0028] Figure 3 This is a flowchart of a pagination query method based on Spring and MyBatis in one embodiment of the present invention;
[0029] Figure 4 In one embodiment of the present invention, PageMap returns the code for a user-defined pagination type;
[0030] Figure 5 This is the code in one embodiment of the present invention that returns the mypage object to the caller;
[0031] Figure 6 A system diagram of an existing system based on Spring and MyBatis;
[0032] Figure 7 This is a block diagram of the system corresponding to the pagination query method based on Spring and MyBatis in one embodiment of the present invention;
[0033] Figure 8 The code for configuring PageObjectWrapperFactory in the spring-mybatis configuration file is shown in one embodiment of the present invention;
[0034] Figure 9 This is the code in the mybatis.config.xml file that configures the paging wrapper object PagingInterceptor in one embodiment of the present invention;
[0035] Figure 10 This is the code for configuring the interception layer DaoPageInterceptor in the Spring configuration file in one embodiment of the present invention. Detailed Implementation
[0036] The specific embodiments of the present invention will be further described below with reference to the accompanying drawings. It should be noted that these descriptions are for the purpose of aiding understanding the present invention, but do not constitute a limitation thereof. Furthermore, the technical features involved in the various embodiments of the present invention described below can be combined with each other as long as they do not conflict with each other.
[0037] like Figure 3-10 As shown, a pagination query method based on Spring and MyBatis is as follows: Figure 3 As shown, it includes the following steps:
[0038] S1: When a method in the Spring DAO layer is invoked, the invocation obtains information about the currently invoked method in the Spring DAO layer through the interception layer DaoPageInterceptor. This information includes the method's return type and parameters.
[0039] The interceptor layer DaoPageInterceptor retrieves the return type of the method and searches for the List and count fields;
[0040] S2: The interceptor layer DaoPageInterceptor searches for the two parameters marked with @CurrentPage (current page) and @PageSize (page size) in the method's parameters;
[0041] S3: Record the return type returnType = {class information of the return type}, currentPage = {parameters marked with @CurrentPage} and pageSize = {parameters marked with @PageSize} in the SQL context SqlContext;
[0042] S4: After the call reaches the MyBatis paging plugin PagingInterceptor, the values of currentPage and pageSize are first retrieved from the SQL context SqlContext. The value of currentPage is the parameter marked with @CurrentPage, and the value of pageSize is the parameter marked with @PageSize. If both currentPage and pageSize have values, it means that paging query is to be performed, and then step S5 is executed.
[0043] S5: If the value of pageSize is 0, only the count result is returned; if the value of pageSize is equal to Integer.MAX_VALUE, only the List result is returned.
[0044] S6: When the returned List result reaches the MyBatis pagination plugin PagingInterceptor, the return type returnType is retrieved from the SQL context SqlContext through the interface method boolean hasWrapperFor(Object object), and true is returned to inform MyBatis that the returned object needs to be encapsulated;
[0045] S7: The MyBatis interface method ObjectWrapper getWrapperFor(MetaObjectmetaObject, Object object) creates a new return object based on the return type returnType, and also creates a pagination wrapper object PageObjectWrapper (which inherits from MyBatis' BaseWrapper type). The return object is encapsulated in the pagination wrapper object PageObjectWrapper, and finally the pagination wrapper object PageObjectWrapper is returned.
[0046] S8: After obtaining the List result returned by the MyBatis executor, MyBatis calls the addAll method of the PageObjectWrapper object;
[0047] Finally, MyBatis retrieves the returned object from the pagination wrapper object PageObjectWrapper and returns it to the method in the Spring DAO layer.
[0048] In the pagination query method based on Spring and MyBatis, such as Figure 4 The methods in the mapper shown can return the defined pagination type. For example... Figure 5 As shown, the current method can directly return the `mypage` object to the caller. When only the count needs to be returned, the parameter annotated with `@PageSize` is set to 0, in which case the `count` SQL will be executed, returning only the count result. When only the List needs to be returned, the parameter annotated with `@PageSize` is set to `Integer.MAX_VALUE`, in which case the `List` SQL will be executed, returning only the List result. In this way, users can define and use their own pagination objects, and can also retrieve only the count result or the List result separately.
[0049] Figure 4 The `PageMap` method is a user-defined distribution type. It must have two parameters annotated with `@CurrentPage` and `@PageSize` to specify the current page size and the size of each page. Additionally, the pagination type defined in `PageMap` must use `@List` to receive fields that return a `List`, and `@Amount` to receive fields that return a `count`.
[0050] like Figure 6 As shown, in existing systems based on Spring and MyBatis, when Spring and MyBatis are working, Spring provides an implementation for the DAO layer interface, which is a proxy object. When a method in the interface is called, the proxy object's concrete implementation of that method is actually invoked, and this concrete implementation calls MyBatis for processing. MyBatis first finds the corresponding SQL statement through the method signature and parses the method parameters into the corresponding parameters in the SQL statement. When the call passes through MyBatis's ObjectWrapperFactory, ObjectWrapperFactory encapsulates the request parameters. Before executing the SQL, MySQL checks if any interceptors (mybatisInterceptor) are configured. If so, it executes the interceptors one by one before executing the SQL. MyBatis parses the results returned from the database into objects of the types defined in the XML file. Then, MySQL checks if any interceptors are configured. If so, it executes the interceptors one by one in reverse order of the incoming request before returning the results. When the return passes through ObjectWrapperFactory, it processes the returned results again (usually returning the results directly) and finally returns the result object to the DAO layer method.
[0051] like Figure 7 As shown, it consists of the Spring DAO interceptor layer DaoPageInterceptor, the pagination object wrapper factory PageObjectWrapperFactory, the pagination wrapper object PageObjectWrapper (not shown in the diagram), the MyBatis pagination plugin PagingInterceptor, and the SQL context SqlContext. The pagination wrapper object PageObjectWrapperFactory implements the original MyBatis ObjectWrapperFactory interface and is configured in the Spring-MyBatis configuration file, such as... Figure 8 As shown, this replaces the original ObjectWrapperFactory in MyBatis. Figure 9 As shown, the paging wrapper object `PagingInterceptor` is configured in the `mybatis.config.xml` file, thus enabling the MyBatis paging plugin. Finally, as... Figure 10 As shown, next configure the DaoPageInterceptor interceptor in the Spring configuration file. This completes the configuration of the Spring DAO interceptor layer. In this way, the configured DAO layer of the project will automatically include pagination functionality.
[0052] It is worth noting that in step S1, when a method in the Spring DAO layer is called, the call obtains the information of the currently called method in the Spring DAO layer through the parameter pj in the Object doAround(ProceedingJoinPoint pj) method in the interceptor layer DaoPageInterceptor.
[0053] Optionally, in step S1, the List field is annotated with @List and the count field is annotated with @Amount. After the interception layer DaoPageInterceptor retrieves the return type of the method, it searches for the fields annotated with @List and @Amount.
[0054] Preferably, in step S5, if the value of pageSize is equal to 0, the countsql is obtained through the original SQL statement. Taking the original SQL statement as SELECT sid, school_code, year_month, did, clcode FROM attendance_stud WHERE school_code=#{schoolCode}AND`year_month`=#{yearMonth} as an example, the obtained countsql is: SELECT count(*)WHERE school_code=#{schoolCode}AND`year_month`=#{yearMonth}. Then, only the countsql statement is executed, and the result pageCount={count result} is recorded in the SQL context SqlContext. The original SQL statement is modified to select 1 from dual where 1=0. The modified original SQL statement is executed and an empty List field is returned.
[0055] Specifically, in step S5, if the value of pageSize is equal to Integer.MAX_VALUE, the original SQL statement is executed and a List result is returned.
[0056] It is worth noting that in step S5, if the value of pageSize is not 0 and is not equal to Integer.MAX_VALUE, the results of countsql and pageSql are obtained through the original SQL statement. The pageSql statement is: SELECT sid, school_code, year_month, did, clcode FROM attendance_stud WHERE school_code=#{schoolCode} AND `year_month`=#{yearMonth}limit{currentPage-1},{pageSize}. The result of countsql, pageCount={count result}, is recorded in the SQL context SqlContext, and the result of pageSql is returned.
[0057] Specifically, in step S8, MyBatis calling the addAll method of the PageObjectWrapper object includes: using Java reflection to place the result of the passed-in List into the List field of the returned object marked with @List;
[0058] Retrieve the pageCount from the SQL context SqlContext, and use Java reflection to place it into the count field of the returned object annotated with @Amount.
[0059] The embodiments of the present invention have been described in detail above with reference to the accompanying drawings, but the present invention is not limited to the described embodiments. For those skilled in the art, various changes, modifications, substitutions, and variations can be made to these embodiments without departing from the principles and spirit of the present invention, and these variations still fall within the protection scope of the present invention.< / inserttest> < / inserttest>
Claims
1. A pagination query method based on Spring and MyBatis, characterized in that, Includes the following steps: S1: When a method in the Spring DAO layer is invoked, the invocation obtains information about the currently invoked method in the Spring DAO layer through the interception layer DaoPageInterceptor. This information includes the method's return type and parameters. The List field is annotated with @List and the count field is annotated with @Amount. The interceptor layer DaoPageInterceptor retrieves the return type of the method and then searches for the fields annotated with @List and @Amount to find the List field and the count field. S2: The interceptor layer DaoPageInterceptor searches for the two parameters marked with @CurrentPage and @PageSize in the method's parameters; S3: Record the return type returnType = {class information of the return type}, currentPage = {parameters marked with @CurrentPage} and pageSize = {parameters marked with @PageSize} in the SQL context SqlContext; S4: After the call reaches the MyBatis pagination plugin PagingInterceptor, it first retrieves the values of currentPage and pageSize from the SQL context SqlContext; S5: If the value of pageSize is 0, only the count result is returned; if the value of pageSize is equal to Integer.MAX_VALUE, only the List result is returned. S6: When the returned List result reaches the MyBatis pagination plugin PagingInterceptor, the return type returnType is retrieved from the SQL context SqlContext through the interface method boolean hasWrapperFor(Object object); S7: The MyBatis interface method ObjectWrapper getWrapperFor(MetaObject metaObject, Object object) creates a new return object based on the return type returnType, and also creates a pagination wrapper object PageObjectWrapper. The return object is encapsulated in the pagination wrapper object PageObjectWrapper, and finally the pagination wrapper object PageObjectWrapper is returned. S8: After obtaining the List result returned by the MyBatis executor, MyBatis calls the addAll method of the PageObjectWrapper object; Finally, MyBatis retrieves the returned object from the pagination wrapper object PageObjectWrapper and returns it to the method in the Spring DAO layer.
2. The pagination query method based on Spring and MyBatis according to claim 1, characterized in that: In step S1, when a method in the Spring DAO layer is called, the call obtains the information of the currently called method in the Spring DAO layer through the parameter pj in the Object doAround(ProceedingJoinPoint pj) method in the interceptor layer DaoPageInterceptor.
3. The pagination query method based on Spring and MyBatis according to claim 2, characterized in that: In step S5, if the value of pageSize is equal to 0, obtain countsql through the original SQL statement, then execute only the countsql statement, and record the result pageCount = {count result} in the SQL context SqlContext. Modify the original SQL statement to select 1 from dual where 1 = 0, execute the modified original SQL statement and return an empty List field.
4. The pagination query method based on Spring and MyBatis according to claim 3, characterized in that: In step S5, if the value of pageSize is equal to Integer.MAX_VALUE, the original SQL statement is executed and a List result is returned.
5. A pagination query method based on Spring and MyBatis according to claim 4, characterized in that: In step S5, if the value of pageSize is not 0 and the value of pageSize is not equal to Integer.MAX_VALUE, the result of countsql and the result of pageSql are obtained through the original SQL statement. The result of countsql, pageCount = {count result}, is recorded in the SQL context SqlContext, and the result of pageSql is returned.
6. A pagination query method based on Spring and MyBatis according to claim 5, characterized in that: In step S8, MyBatis calls the addAll method of the PageObjectWrapper object, which includes: using Java reflection to place the result of the passed-in List into the List field of the returned object marked with @List; Retrieve the pageCount from the SQL context SqlContext, and use Java reflection to place it into the count field of the returned object annotated with @Amount.
Citation Information
Patent Citations
Fragmentation database access method and database system
CN103853718A
Data query method and device and storage medium
CN113326302A