Streaming result returning method and device based on MyBatis framework
By checking and converting the return value type of the Mapper interface method to a streaming type in the MyBatis framework, obtaining the cursor object and converting it into a streaming object, the problem of the MyBatis framework's lack of support for streaming results is solved, efficient and stable streaming data processing is achieved, and the threshold for technology migration and the risk of resource leakage are reduced.
Patent Information
- Application Number
- CN202511232396.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-01
- Publication Date
- 2025-10-03
- Estimated Expiration
- 2045-09-01
AI Technical Summary
The MyBatis framework lacks support for streaming results, which limits its data processing flexibility and advancement. This causes developers to face huge obstacles when migrating JPA-based Stream processing logic to the MyBatis framework, and they are unable to fully utilize the advantages of Stream processing.
By checking whether the return value type of the Mapper interface method in the MyBatis framework is the streaming type of the Java standard library, obtaining the cursor object and converting it into a stream object, and registering a close callback function on the stream object, the streaming result return is achieved.
The MyBatis framework supports streaming results, reducing development workload, ensuring performance and native consistency, avoiding resource leakage, reducing technology migration costs, and improving development efficiency and system stability.
Smart Images

Figure CN120743263A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a streaming result returning method and device based on a MyBatis framework. Background Art
[0002] Data persistence is a critical component of modern enterprise application development. The MyBatis framework, with its powerful flexibility and efficiency, has become a popular persistence framework for many developers. When processing data queries, the MyBatis framework offers two primary modes for returning multi-row data: a traditional mode based on standard Java collection interfaces, and a cursor return type mode based on its own Cursor interface.
[0003] Stream processing, with its elegant chaining syntax, powerful parallel processing capabilities, and efficient combination of intermediate and terminal operations, provides developers with a more intuitive, flexible, and high-performance data processing paradigm. However, the current MyBatis framework lacks support for returning stream results. This not only limits the flexibility and advancement of MyBatis in data processing, but also presents significant obstacles for developers migrating JPA-based code that uses stream processing logic to the MyBatis framework, preventing them from fully leveraging the advantages of stream processing in MyBatis application scenarios.
[0004] Therefore, how to provide an extension to the MyBatis framework to enable it to support returning streaming results is a technical problem that urgently needs to be solved. Summary of the Invention
[0005] In view of this, the purpose of the present invention is to provide a method and device for returning streaming results based on the MyBatis framework, which solves the problem in the prior art that the MyBatis framework does not support returning streaming type results.
[0006] To solve the above technical problems, the present invention provides a streaming result return method based on the MyBatis framework, comprising:
[0007] If the current SQL operation is a query operation, check whether the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library;
[0008] If so, a cursor object is obtained, the cursor object is converted into a stream object using the Java standard library, and a close callback function is registered on the stream object.
[0009] Optionally, if the current SQL operation is a query operation, before checking whether the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library, it also includes:
[0010] A new Boolean type attribute returnsStream is added to the inner class MethodSignature of the MapperMethod class, and the attribute returnsStream is initialized; the attribute returnsStream is used to determine whether the return value type of the Mapper interface method of the MyBatis framework is a streaming type of the Java standard library.
[0011] Optionally, if yes, obtain a cursor object, convert the cursor object into a stream object using the Java standard library, and register a close callback function on the stream object, including:
[0012] If the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library, the execute cursor query method of the MyBatis framework is called to obtain the cursor object;
[0013] Call cursor.iterator() to obtain an iterator from the cursor object; the iterator is used to traverse the query result set row by row;
[0014] Convert the iterator into the stream object;
[0015] The close operation of the cursor object is bound to the stream object using onClose.
[0016] Optionally, converting the iterator into the stream object includes:
[0017] The iterator is converted into the stream object by using the stream support tool class and the detachable iterator tool class in the Java standard library.
[0018] Optionally, the stream object can implement the java.util.stream.Stream standard interface, support filter, map, and collect streaming operations, and can automatically close data resources through resource automatic management syntax.
[0019] Optionally, if the current SQL operation is a query operation, before checking whether the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library, it also includes:
[0020] The recognition logic of the streaming type is expanded in the return value type parser of the MyBatis framework; the return value type parser of the expanded MyBatis framework includes execution paths of cursor query, streaming query and conventional query.
[0021] Optionally, check whether the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library, including:
[0022] In the mapper method execution entry of the MyBatis framework, the method return value type is obtained by reflection and compared with the stream type of the Java standard library to determine the inspection result.
[0023] The present invention also provides a streaming result return device based on the MyBatis framework, comprising:
[0024] The judgment module is used to check whether the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library if the current SQL operation is a query operation;
[0025] The conversion module is used to obtain a cursor object if so, convert the cursor object into a stream object using a Java standard library, and register a close callback function on the stream object.
[0026] The present invention also provides a streaming result return device based on the MyBatis framework, comprising:
[0027] Memory for storing computer programs;
[0028] The processor is used to implement the above-mentioned streaming result return method based on the MyBatis framework when executing the computer program.
[0029] The present invention also provides a computer-readable storage medium, in which computer-executable instructions are stored. When the computer-executable instructions are loaded and executed by a processor, the streaming result return method based on the MyBatis framework as described above is implemented.
[0030] It can be seen that the present invention checks whether the return value type of the Mapper interface method of the MyBatis framework is a streaming type of the Java standard library if the current SQL operation is a query operation; if so, obtains a cursor object, converts the cursor object into a streaming object using the Java standard library, and registers a close callback function on the streaming object. This method uses the original cursor object of the MyBatis framework to expand the MyBatis framework, so that it has a streaming result return function, reduces the development workload, and ensures that the performance is consistent with the native MyBatis framework; and does not modify the MyBatis core code implementation function to avoid introducing risks, and the original function is not affected; and the cursor object closing is bound to the stream object, so the user does not need to manually manage resources to avoid resource leakage; and it is a standard interface to the outside world, and there is no need to learn the unique interface of the MyBatis framework.
[0031] In addition, the present invention also provides a streaming result return device, equipment and readable storage medium based on the MyBatis framework, which also have the above-mentioned beneficial effects. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are merely embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying any creative work.
[0033] Figure 1 A flowchart of a streaming result return method based on the MyBatis framework provided in an embodiment of the present invention;
[0034] Figure 2 A schematic diagram of the structure of a streaming result return method device based on the MyBatis framework provided by an embodiment of the present invention;
[0035] Figure 3 The present invention provides a structural diagram of a streaming result return method device based on the MyBatis framework. DETAILED DESCRIPTION
[0036] To make the objectives, technical solutions, and advantages of the embodiments of the present invention more clear, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts shall fall within the scope of protection of the present invention.
[0037] The MyBatis framework is an open-source Java persistence framework. Its core function is to map Java objects to relational database data by simplifying database operations. Data persistence is a critical component of modern enterprise application development. MyBatis, with its powerful flexibility and efficiency, has become a popular persistence framework for many developers. When processing data queries, MyBatis offers two primary modes for returning multiple rows of data. The first utilizes standard Java collection interfaces, such as List (an ordered list), Set (a collection guaranteed to have no duplicate elements), and Collection (a mapping table for key-value lookup). This traditional mode loads the entire database query result set into memory all at once when reading data. When processing smaller data volumes, this approach, with its simple and intuitive operations, enables rapid data acquisition and processing. Developers can easily traverse, filter, and analyze data using collection methods. However, when faced with large result sets, especially when processing massive amounts of data, this all-at-once loading mechanism forces all data into memory, which can easily lead to rapid memory depletion and, in turn, Out-of-Memory (OOM) errors. This can severely impact application stability and performance, and even cause system crashes. Secondly, MyBatis features its own Cursor interface. During data processing, the Cursor interface doesn't read all data at once. Instead, it retrieves a row from the JDBC (an application programming interface for Java programs to access relational databases) result set each time an iteration is performed. This "row-by-row read" mechanism ensures that memory usage remains constant even when processing millions or even larger amounts of data, effectively avoiding performance bottlenecks caused by the influx of large amounts of data into memory. It provides developers with a more efficient and resource-friendly approach to big data processing, demonstrating significant performance advantages when processing large datasets.
[0038] The continuous evolution of Java technology, and the introduction of stream processing in JDK 8, has revolutionized data processing. Stream processing, with its elegant chaining syntax, powerful parallel processing capabilities, and efficient combination of intermediate and terminal operations, provides developers with a more intuitive, flexible, and high-performance data processing paradigm. However, the current MyBatis framework lacks support for returning stream results. This not only limits the flexibility and sophistication of MyBatis in data processing, but also presents significant obstacles for developers migrating JPA-based (Java Persistence Application Programming Interface) code that utilizes stream processing logic to MyBatis, preventing them from fully leveraging the advantages of stream processing in MyBatis applications.
[0039] In current MyBatis applications, the framework lacks native support for returning streaming results, while similar frameworks like Spring JDBC and JPA do. This undoubtedly increases development costs and technical migration barriers during code migration. Furthermore, developers must manually modify and encapsulate this functionality. However, since encapsulation is not simply a type conversion, errors can easily lead to resource leaks, seriously impacting system stability.
[0040] Based on this, the present invention solves this key deficiency in the existing MyBatis framework's data processing capabilities and provides a complete and efficient implementation method that supports the return of streaming results. Through the technical solution of the present invention, it is possible to fully integrate the powerful data persistence capabilities of the MyBatis framework with the outstanding advantages of Stream processing, providing developers with a more flexible and efficient data processing method. It also provides a feasible path for JPA-based code migration, greatly improving the adaptability and competitiveness of the MyBatis framework in modern enterprise-level application development.
[0041] Please refer to Figure 1 , Figure 1 A flowchart of a method for returning streaming results based on the MyBatis framework is provided in an embodiment of the present invention. The method may include:
[0042] S101: If the current SQL operation is a query operation, check whether the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library; if so, execute S102.
[0043] The execution entity of this embodiment is the processor in the MyBatis framework. Specifically, in the SQL (Structured Query Language) execution method execute of the MapperMethod class (used to manage the mapping between SQL in XML and Java interface methods), it first determines whether the currently executed SQL operation is a Select operation, and also checks whether the return value type of the SQL-mapped method is a Stream type. This is because in MapperMethod, the execute method routes to different processing logic based on the SQL type. For example, for an Update, the executeUpdate method is called, while for a Select, executeForResultSet or executeForCursor is called. Therefore, before determining whether the return type is a Stream, it is necessary to confirm that it is a Select operation; otherwise, the subsequent cursor processing logic cannot be applied.
[0044] It's important to note that in the MyBatis framework, methods in the Mapper interface are where database operations are defined. Each method corresponds to a SQL statement (configured via XML or annotations). The SQL-mapped method mentioned above refers to the SQL operation configuration corresponding to the Mapper interface method (e.g., the SQL statement defined in XML or annotations). The MyBatis framework specification stipulates that the return type of a Mapper interface method must be logically compatible with the return type of the SQL mapping configuration (e.g., resultType / resultMap in XML). Therefore, checking whether the return type of a MyBatis framework Mapper interface method is a stream type from the Java standard library is equivalent to checking whether the return type of a SQL-mapped method is a stream type. These two checks operate on different levels: Whether the return type of a Mapper interface method is a stream type from the Java standard library refers to the declared return type of the Java interface method (the type declaration at the code level); whether the return type of a SQL-mapped method is a stream type refers to the expected return type of the SQL operation configuration (the type declaration at the configuration level).
[0045] The above check to see if the return type of the MyBatis framework's Mapper interface method is a Java standard library stream type can be determined by parsing the MyBatis method signature. The method in the method signature refers to the Java method defined in the MyBatis framework's Mapper interface.
[0046] As can be seen from step S101, the conversion prerequisites include two: (1) the current SQL operation is a query operation; (2) the return value type of the Mapper interface method of the MyBatis framework is the stream type of the Java standard library. The reason for this setting is that in MyBatis, different SQL operation types (such as Insert, Update, Delete, Select) have different execution logic. Only the Select operation will return a result set, while other operations usually return the number of affected rows or void. Therefore, even if the return value type of the method is Stream, if Update is executed, there will be no result set. At this time, using a cursor is meaningless and may even cause errors. If a non-Select operation uses a cursor incorrectly, it may cause resource leakage or unexpected behavior. Therefore, MyBatis must limit the use of cursors to only Select operations. The return type judgment determines whether it needs to be converted to Stream, and the SQL operation type judgment ensures that only Select operations can perform this conversion, avoiding logical errors and resource problems. Filtering non-SELECT operations in advance can avoid executing redundant cursor query logic in error scenarios, thereby ensuring functional correctness and system stability.
[0047] The specific execution code of step S101 is as follows:
[0048] public class MapperMethod {
[0049] / / ...
[0050] public Object execute(SqlSession sqlSession, Object[] args) {
[0051] Object result;
[0052] switch (command.getType()) {
[0053] / / ...
[0054] case SELECT: / / SQL type is Select type;
[0055] / / ...
[0056] } else if (method.returnsStream()) {
[0057] / / The return value type of the SQL mapping method is Stream type;
[0058] Cursor<?> cursor = executeForCursor(sqlSession, args);
[0059] result = StreamSupport
[0060] .stream(Spliterators.spliteratorUnknownSize(cursor.iterator(), Spliterator.ORDERED), false)
[0061] .onClose(cursor::close);
[0062] }
[0063] / / ...
[0064] }
[0065] / / ...
[0066] }.
[0067] Furthermore, if the current SQL operation is a query operation, before checking whether the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library, the following steps may also be included:
[0068] Add a Boolean type attribute returnsStream to the inner class MethodSignature of the MapperMethod class and initialize the attribute returnsStream; the attribute returnsStream is used to determine whether the return value type of the Mapper interface method of the MyBatis framework is the streaming type of the Java standard library.
[0069] Specifically, step S101 checks whether the return value type of the MyBatis framework's Mapper interface method is a stream type defined by the Java standard library. This is accomplished by adding a Boolean property, returnsStream (return value is a stream type), to the MethodSignature (method signature) inner class of the MapperMethod class. This property is used to determine whether the return value type of the MyBatis framework's Mapper interface method is a stream type defined by the Java standard library. At the same time, the returnsStream property is initialized and set in the MethodSignature constructor.
[0070] The specific code is as follows:
[0071] public static class MethodSignature {
[0072] / / ...
[0073] / / Create returnsStream to indicate that the return value type of the method is Stream type;
[0074] private final boolean returnsStream;
[0075] / / ...
[0076] public MethodSignature(Configuration configuration, Class<?> mapperInterface, Method method) {
[0077] / / ...
[0078] / / In the construction method, assign a value to returnsStream to determine whether the return value type is a Stream type;
[0079] this.returnsStream = Stream.class.equals(this.returnType);
[0080] / / ...
[0081] }
[0082] }.
[0083] S102: Obtain a cursor object, convert the cursor object into a stream object using the Java standard library, and register a close callback function on the stream object.
[0084] Specifically, when the above conditions are met, a cursor object is obtained and converted into a stream object using the Java standard library. Finally, to prevent resource leaks, a close callback function is registered with the stream object. When the stream object operation is completed, the close callback function is automatically called, ensuring that the cursor object is properly closed and the database connection resources are released.
[0085] Furthermore, if the above is true, then obtain a cursor object, convert the cursor object into a stream object using the Java standard library, and register a close callback function on the stream object. Specifically, the following steps may be included:
[0086] If the return value type of the Mapper interface method of the MyBatis framework is the stream type of the Java standard library, the execute cursor query method of the MyBatis framework is called to obtain the cursor object; call cursor.iterator() to obtain the iterator from the cursor object; the iterator is used to traverse the query result set row by row; the iterator is converted into a stream object; and the onClose method is used to bind the close operation of the cursor object to the stream object.
[0087] Specifically, the executeForCursor method provided by the MyBatis framework is called to obtain a cursor object for the query results. Once the cursor object is obtained, the corresponding Iterator can be retrieved from it, which can be used to traverse the query result set. Using the Java standard library, this iterator is converted into a stream object. This allows for more convenient and efficient processing of query results using the rich operations provided by the Java Stream API, such as filtering, mapping, and aggregation. It is important to note that the onClose method registers a close callback function with the stream object. This function is automatically executed when the stream terminates (e.g., by calling close() or exiting a try-with-resources block). By combining this method with the onClose method, the MyBatis framework's Cursor resource closing logic is deeply integrated with the stream lifecycle. Upon completion of the stream operation, the Cursor resource is automatically released, eliminating the need for developers to manually write complex closing code. This fundamentally eliminates the risk of resource leaks, significantly improving system stability and security, and providing reliable protection for large-scale data query scenarios.
[0088] Furthermore, the above conversion of the iterator into a stream object may specifically include the following steps: using the stream support tool class (StreamSupport class) and the splittable iterator tool class (Spliterators class) in the Java standard library to convert the iterator into a stream object.
[0089] Furthermore, the stream object can implement the java.util.stream.Stream standard interface, support filter, map, and collect streaming operations, and automatically close data resources through resource automatic management syntax.
[0090] Specifically, (1) Java's Stream interface (java.util.stream.Stream) is the standard contract for stream operations. Any stream implementation must follow its defined methods (such as filter, map, and collect). The stream object generated by the MyBatis framework through StreamSupport.stream (the stream generation method of the stream support tool class) is essentially a ReferencePipeline (the implementation class of the Java standard stream), which naturally meets this condition. This unifies the interface compatibility. Developers do not need to learn the Cursor API unique to the MyBatis framework and can directly use familiar Stream methods (such as forEach and collect), reducing the learning cost. (2) Java's Stream interface has built-in methods such as filter, map, and reduce. These operations are implemented through lazy evaluation and do not require additional development. The stream object converted from the cursor inherits these capabilities because its essence is still a standard Stream. By chaining operations such as filter / map / reduce, complex data processing logic is simplified and the code is more concise. (3) Java's try-with-resources syntax requires resource objects to implement the AutoCloseable interface. Stream itself doesn't implement AutoCloseable, but similar behavior can be simulated by binding a callback function to onClose. When the stream is explicitly closed (for example, by wrapping a Stream subclass with try-with-resources), the callback is triggered to release the cursor. By combining try-with-resources, the cursor and database connection are guaranteed to be closed, preventing connection pool leaks caused by forgetting to manually call cursor.close().
[0091] Furthermore, if the current SQL operation is a query operation, before checking whether the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library, the following steps may also be included:
[0092] The recognition logic of streaming types is expanded in the return value type parser of the MyBatis framework; the expanded return value type parser of the MyBatis framework includes the execution paths of cursor queries, streaming queries and regular queries.
[0093] This embodiment expands the recognition logic of the streaming type in the return value type parser of the MyBatis framework in advance, so that it can be parsed by the parser and the return value type can be judged. Of course, the parser satisfies the execution path of cursor query, streaming query and conventional query.
[0094] Furthermore, the above check on whether the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library may include:
[0095] In the execution entry of the mapper method of the MyBatis framework, the method return value type is obtained through reflection and compared with the stream type of the Java standard library to determine the inspection result.
[0096] Specifically, this embodiment provides a specific method for determining the type of a return value, which utilizes reflection comparison class definitions to determine the type of a return value.
[0097] By applying the streaming result return method based on the MyBatis framework provided by an embodiment of the present invention, when the current SQL operation is a query operation and the return value type is detected to be a Stream, proprietary APIs such as MyBatis executeForCursor are reused to obtain the Cursor object, avoiding the need to redevelop the underlying database query logic; then, using Java standard StreamSupport and Spliterators, the Cursor iterator is converted to a Stream, and the Cursor close operation is associated with the onClose method, thereby hiding the complex details of MyBatis Cursor resource management and exposing the results to the outside in the form of a standard JavaStream API. By determining whether the return type is a Stream, Cursor, etc., the type resolution logic based on the existing MyBatis framework is expanded without modifying the core underlying code, complying with the open-closed principle, and solving the problem of manually encapsulated resource leakage. It also facilitates developers to implement code migration with other frameworks based on standard APIs. The invention has the following beneficial effects: (1) Seamlessly connect the standard stream processing ecosystem and reduce the cost of technology migration: support the use of standard Java Stream interface as the return type of query results, so that the MyBatis framework can natively support standard Stream types. Developers can directly reuse the Stream operation experience accumulated in similar frameworks such as Spring JDBC and JPA without having to relearn the MyBatis proprietary API. Based on this, the present invention fills the technical gap in the MyBatis framework's Stream support and provides developers with a safer and more convenient development experience. In addition, in this way, the smooth migration of data processing logic is achieved, which greatly improves development efficiency. At the same time, it allows developers to use the familiar stream programming paradigm and easily cope with complex business requirements such as data filtering, aggregation, and mapping, effectively reducing the learning cost and technology adaptation threshold of cross-framework development. (2) Efficiently reuse core code to achieve low-intrusion upgrades: deeply explore the existing functions of the MyBatis framework, and through the flexible reuse of key APIs such as executeForCursor, the expansion of Stream return functions is achieved without changing the core architecture of the framework. It not only optimizes the return type judgment and result conversion logic, ensuring 100% backward compatibility with the old version and avoiding the risk of system crashes due to upgrades, but also greatly shortens the development cycle and reduces maintenance costs by minimizing code modifications, fully practicing the "open and closed principle" and providing a low-risk and sustainable solution for the functional iteration of the MyBatis framework.(3) Intelligent resource management to eliminate potential security risks: To address the resource leakage problem that is prone to occur when traditional Cursor is encapsulated as a Stream, this method uses StreamSupport.stream combined with the onClose method to deeply bind the Cursor resource closing logic of the MyBatis framework to the Stream life cycle. When the Stream operation is completed, the Cursor resource release is automatically triggered, eliminating the need for developers to manually write complex closing code, fundamentally eliminating the risk of resource leakage, significantly improving the stability and security of system operation, and providing reliable protection for large-scale data query scenarios. (4) Improve code maintainability and scalability: Based on the design pattern of the standard Java API, the code structure is clearer and more concise, and highly compatible with the mainstream Java ecosystem. Whether it is adding new business functions or connecting to third-party components, they can be integrated in a more natural way, reducing the degree of code coupling. At the same time, following the "open and closed principle" implementation method, sufficient space is reserved for the continuous expansion of future framework functions, making it easier for the development team to quickly respond to changes in business needs without affecting existing functions and realize iterative upgrades of the system.
[0098] The following is an introduction to a streaming result return device based on the MyBatis framework provided by an embodiment of the present invention. The streaming result return device based on the MyBatis framework described below and the streaming result return method based on the MyBatis framework described above can refer to each other.
[0099] Please refer to Figure 2 , Figure 2 A schematic structural diagram of a streaming result return device based on the MyBatis framework provided in an embodiment of the present invention may include:
[0100] The judgment module 100 is used to check whether the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library if the current SQL operation is a query operation;
[0101] The conversion module 200 is configured to obtain a cursor object, convert the cursor object into a stream object using a Java standard library, and register a close callback function on the stream object.
[0102] Based on the above embodiment, the streaming result returning device based on the MyBatis framework may further include:
[0103] Add a module to add a Boolean type attribute returnsStream to the inner class MethodSignature of the MapperMethod class and initialize the attribute returnsStream; the attribute returnsStream is used to determine whether the return value type of the Mapper interface method of the MyBatis framework is the streaming type of the Java standard library.
[0104] Based on the above embodiment, the conversion module 200 may include:
[0105] A cursor object acquisition unit is configured to call an execute cursor query method of the MyBatis framework to acquire the cursor object if the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library;
[0106] An iterator acquisition unit, used to call cursor.iterator() to obtain an iterator from the cursor object; the iterator is used to traverse the query result set row by row;
[0107] A conversion unit, configured to convert the iterator into the stream object;
[0108] The binding unit is used to bind the close operation of the cursor object to the stream object by using the onClose method.
[0109] Based on the above embodiment, the conversion unit may include:
[0110] The conversion subunit is used to convert the iterator into the stream object by using the stream support tool class and the detachable iterator tool class in the Java standard library.
[0111] Based on the above embodiment, the stream object can implement the java.util.stream.Stream standard interface, support filter, map, and collect streaming operations, and can automatically close data resources through resource automatic management syntax.
[0112] Based on the above embodiment, the streaming result return device based on the MyBatis framework may further include:
[0113] An extension module is used to extend the recognition logic of the streaming type in the return value type parser of the MyBatis framework; the return value type parser of the extended MyBatis framework includes execution paths for cursor query, streaming query and regular query.
[0114] Based on the above embodiment, the determination module 100 may include:
[0115] The comparison unit is used to obtain the method return value type through reflection in the mapper method execution entry of the MyBatis framework, and compare it with the stream type of the Java standard library to determine the inspection result.
[0116] It should be noted that the order of the modules and units in the above-mentioned streaming result return device based on the MyBatis framework can be changed without affecting the logic.
[0117] The embodiment of the present invention provides a streaming result return device based on the MyBatis framework. The judgment module 100 is used to check whether the return value type of the Mapper interface method of the MyBatis framework is a streaming type of the Java standard library if the current SQL operation is a query operation; the conversion module 200 is used to obtain a cursor object if so, convert the cursor object into a stream object using the Java standard library, and register a close callback function on the stream object. This device expands the MyBatis framework to provide a streaming result return function, reducing the development workload while ensuring performance consistent with the native MyBatis framework; and does not modify the MyBatis core code implementation function to avoid introducing risks and the original function is not affected; and the cursor object closing is bound to the stream object, so users do not need to manually manage resources, avoiding resource leakage.
[0118] The following is an introduction to a streaming result return device based on the MyBatis framework provided by an embodiment of the present invention. The streaming result return device based on the MyBatis framework described below and the streaming result return method based on the MyBatis framework described above can refer to each other.
[0119] Please refer to Figure 3 , Figure 3 A schematic structural diagram of a streaming result return device based on the MyBatis framework provided in an embodiment of the present invention may include:
[0120] Memory 10, for storing computer programs;
[0121] The processor 20 is used to execute a computer program to implement the above-mentioned streaming result return method based on the MyBatis framework.
[0122] The memory 10 , the processor 20 , and the communication interface 31 all communicate with each other via the communication bus 32 .
[0123] In the embodiment of the present invention, the memory 10 is used to store one or more programs. The program may include program code, and the program code includes computer operation instructions. In the embodiment of the present invention, the memory 10 may store programs for implementing the following functions:
[0124] If the current SQL operation is a query operation, check whether the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library;
[0125] If so, obtain the cursor object, convert the cursor object into a stream object using the Java standard library, and register a close callback function on the stream object.
[0126] In one possible implementation, the memory 10 may include a program storage area and a data storage area, wherein the program storage area may store an operating system and applications required for at least one function, etc.; the data storage area may store data created during use.
[0127] In addition, the memory 10 may include a read-only memory and a random access memory, and provides instructions and data to the processor. A portion of the memory may also include NVRAM. The memory stores an operating system and operating instructions, executable modules or data structures, or a subset or an extended set thereof. The operating instructions may include various operating instructions for implementing various operations. The operating system may include various system programs for implementing various basic tasks and processing hardware-based tasks.
[0128] The processor 20 may be a central processing unit (CPU), an application-specific integrated circuit, a digital signal processor, a field programmable gate array, or other programmable logic device. The processor 20 may be a microprocessor or any conventional processor. The processor 20 may call a program stored in the memory 10 .
[0129] The communication interface 31 may be an interface of a communication module, used for connecting to other devices or systems.
[0130] Of course, it needs to be explained that Figure 3 The structure shown does not constitute a limitation on the streaming result return device based on the MyBatis framework in the embodiment of the present invention. In actual applications, the streaming result return device based on the MyBatis framework may include Figure 3 More or fewer components than shown, or combinations of certain components.
[0131] The computer-readable storage medium provided in an embodiment of the present invention is introduced below. The computer-readable storage medium described below and the streaming result return method based on the MyBatis framework described above can be referenced to each other.
[0132] The present invention also provides a computer-readable storage medium having a computer program stored thereon, which implements the steps of the above-mentioned streaming result return method based on the MyBatis framework when the computer program is executed by a processor.
[0133] The computer-readable storage medium may include: a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk, etc., which can store program codes.
[0134] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from the other embodiments. Reference can be made to the descriptions of the identical or similar parts between the various embodiments. For the devices disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the descriptions are relatively simple, and the relevant parts can be referred to the descriptions of the methods.
[0135] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the above description has generally described the components and steps of each example according to their functions. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.
[0136] Finally, it should be noted that, in this document, relationships such as first and second, etc., are used solely to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the terms "comprises," "comprising," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that includes a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article, or apparatus.
[0137] The above is a detailed introduction to a streaming result return method, device, equipment and computer-readable storage medium based on the MyBatis framework provided by the present invention. Specific examples are used in this article to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea. At the same time, for those skilled in the art, according to the ideas of the present invention, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as limiting the present invention.
Claims
1. A streaming result return method based on the MyBatis framework, characterized in that: include: If the current SQL operation is a query operation, check whether the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library; If so, a cursor object is obtained, the cursor object is converted into a stream object using the Java standard library, and a close callback function is registered on the stream object.
2. The streaming result return method based on the MyBatis framework according to claim 1, characterized in that, If the current SQL operation is a query operation, before checking whether the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library, it also includes: A new Boolean type attribute returnsStream is added to the inner class MethodSignature of the MapperMethod class, and the attribute returnsStream is initialized; the attribute returnsStream is used to determine whether the return value type of the Mapper interface method of the MyBatis framework is a streaming type of the Java standard library.
3. The streaming result return method based on the MyBatis framework according to claim 1, characterized in that, If so, obtain a cursor object, convert the cursor object into a stream object using the Java standard library, and register a close callback function on the stream object, including: If the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library, the execute cursor query method of the MyBatis framework is called to obtain the cursor object; Call cursor.iterator() to obtain an iterator from the cursor object; the iterator is used to traverse the query result set row by row; Convert the iterator into the stream object; The onClose method is used to bind the close operation of the cursor object to the stream object.
4. The streaming result return method based on the MyBatis framework according to claim 3, characterized in that, Converting the iterator into the stream object includes: The iterator is converted into the stream object by using the stream support tool class and the detachable iterator tool class in the Java standard library.
5. The streaming result return method based on the MyBatis framework according to claim 1, characterized in that, The stream object can implement the java.util.stream.Stream standard interface, support filter, map, and collect streaming operations, and can automatically close data resources through resource automatic management syntax.
6. The streaming result return method based on the MyBatis framework according to claim 1, characterized in that, If the current SQL operation is a query operation, before checking whether the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library, it also includes: The recognition logic of the streaming type is expanded in the return value type parser of the MyBatis framework; the return value type parser of the expanded MyBatis framework includes execution paths of cursor query, streaming query and conventional query.
7. The streaming result return method based on the MyBatis framework according to claim 1, characterized in that, Check whether the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library, including: In the mapper method execution entry of the MyBatis framework, the method return value type is obtained by reflection and compared with the stream type of the Java standard library to determine the inspection result.
8. A streaming result return device based on the MyBatis framework, characterized in that: include: The judgment module is used to check whether the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library if the current SQL operation is a query operation; The conversion module is used to obtain a cursor object if so, convert the cursor object into a stream object using a Java standard library, and register a close callback function on the stream object.
9. A streaming result return device based on the MyBatis framework, characterized in that: include: Memory for storing computer programs; A processor, configured to implement the streaming result return method based on the MyBatis framework as described in any one of claims 1 to 7 when executing the computer program.
10. A computer-readable storage medium, characterized in that The computer-readable storage medium stores computer-executable instructions. When the computer-executable instructions are loaded and executed by the processor, the streaming result return method based on the MyBatis framework as described in any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Method, apparatus, computer device and storage medium for scanning static code
CN109144852A
Data query processing method based on MyBatis framework
CN114756554A
Query method, system and equipment of multi-component data stream and storage medium
CN115878675A