A method and apparatus for returning streaming results based on the MyBatis framework
By checking and converting the return type of the Mapper interface method to a streaming type in the MyBatis framework, obtaining the cursor object and converting it to a streaming object, the lack of streaming result support in the MyBatis framework is solved, achieving efficient and stable streaming data processing, reducing migration difficulty and the risk of resource leakage.
Patent Information
- Application Number
- CN202511232396.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-01
- Publication Date
- 2025-10-31
- Estimated Expiration
- 2045-09-01
AI Technical Summary
The MyBatis framework lacks support for streaming results, which limits its data processing flexibility and sophistication. This makes it difficult for developers to migrate JPA-based Stream processing logic and may lead to resource leaks.
By checking whether the return type of the Mapper interface method in the MyBatis framework is a stream type of the Java standard library, obtaining the cursor object and converting it into a stream object, registering a close callback function on the stream object, and using the StreamSupport and Spliterators classes of the Java standard library to convert the iterator into a stream object, the streaming result is returned.
It enables MyBatis framework to support streaming results, reduces development workload, ensures performance consistency with native implementation, avoids resource leaks, reduces technology migration costs and learning difficulty, and improves system stability and development efficiency.
Smart Images

Figure CN120743263B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a method and apparatus for returning streaming results based on the MyBatis framework. Background Technology
[0002] In modern enterprise application development, data persistence is a crucial step. The MyBatis framework, with its powerful flexibility and efficiency, has become a favored persistence layer framework among many developers. When processing data queries, the MyBatis framework provides two main modes for returning multiple rows of data: the traditional mode based on Java's standard collection interface, and the cursor return type mode based on its own Cursor interface.
[0003] Stream processing, with its concise and elegant chained operation 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 MyBatis's flexibility and sophistication in data processing but also poses a significant obstacle for developers migrating JPA-based code with 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 that can 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 apparatus for returning streaming results based on the MyBatis framework, which solves the problem that the MyBatis framework does not support returning streaming type results in the prior art.
[0006] To address the aforementioned technical problems, this invention provides a streaming result return method based on the MyBatis framework, comprising:
[0007] If the current SQL operation is a query operation, then check whether the return type of the Mapper interface method in the MyBatis framework is a stream type from the Java standard library;
[0008] 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.
[0009] Optionally, before checking whether the return type of the Mapper interface method in the MyBatis framework is a stream type from the Java standard library if the current SQL operation is a query operation, the following is also included:
[0010] In the inner class MethodSignature of the MapperMethod class, a new boolean attribute returnsStream is added, and the returnsStream attribute is initialized. The returnsStream attribute is used to determine whether the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library.
[0011] Optionally, if so, obtain the cursor object, convert the cursor object to a stream object using the Java standard library, and register a close callback function on the stream object, including:
[0012] If the return type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library, then the 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 using the stream support utility class and the splittable iterator utility 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 automatically close data resources through the resource auto-management syntax.
[0019] Optionally, before checking whether the return type of the Mapper interface method in the MyBatis framework is a stream type from the Java standard library if the current SQL operation is a query operation, the following is also included:
[0020] The return value type resolver of the MyBatis framework is extended with logic for recognizing the streaming type; the extended MyBatis framework return value type resolver includes execution paths for cursor queries, streaming queries, and regular queries.
[0021] Optionally, check whether the return type of the Mapper interface methods in the MyBatis framework is a Java standard library stream type, including:
[0022] In the mapper method execution entry point of the MyBatis framework, the return type of the method is obtained through reflection and compared with the Stream type of the Java standard library to determine the check 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 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 the cursor object if the condition is met, convert the cursor object into a stream object using the Java standard library, and register a close callback function on the stream object.
[0026] This invention also provides a streaming result return device based on the MyBatis framework, comprising:
[0027] Memory, used to store computer programs;
[0028] A processor, used to implement the streaming result return method based on the MyBatis framework as described above when executing the computer program.
[0029] The present invention also provides a computer-readable storage medium storing computer-executable instructions, which, when loaded and executed by a processor, implement the streaming result return method based on the MyBatis framework as described above.
[0030] As can be seen, this invention checks whether the return type of the Mapper interface method in the MyBatis framework is a streaming type from the Java standard library if the current SQL operation is a query operation. If so, it obtains a cursor object, converts the cursor object to a streaming object using the Java standard library, and registers a close callback function on the streaming object. This method extends the MyBatis framework by utilizing its existing cursor object, enabling it to return streaming results, reducing development workload while ensuring performance consistent with the native MyBatis framework. Furthermore, it does not modify the core MyBatis code, avoiding the introduction of risks, and the original functionality remains unaffected. By binding the cursor object's close function to the streaming object, users do not need to manually manage resources, preventing resource leaks. Finally, it uses a standard interface externally, eliminating the need to learn MyBatis's proprietary interfaces.
[0031] In addition, the present invention also provides a streaming result return device, apparatus and readable storage medium based on the MyBatis framework, which also has the above-mentioned beneficial effects. Attached Figure Description
[0032] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on the provided drawings without creative effort.
[0033] Figure 1 A flowchart illustrating a streaming result return method based on the MyBatis framework, provided as an embodiment of the present invention;
[0034] Figure 2 A schematic diagram of a streaming result return method device based on the MyBatis framework provided in an embodiment of the present invention;
[0035] Figure 3 This is a schematic diagram of the structure of a streaming result return method device based on the MyBatis framework, provided in an embodiment of the present invention. Detailed Implementation
[0036] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0037] MyBatis is an open-source Java persistence framework whose core function is to simplify database operations by mapping Java objects to data in relational databases. In modern enterprise application development, data persistence is a crucial step, and MyBatis, with its powerful flexibility and efficiency, has become a popular persistence framework among developers. When handling data queries, MyBatis provides two main modes for returning multiple rows of data. The first mode uses standard Java collection interfaces, such as List (an ordered list collection), Set (a collection that guarantees no duplicate elements), and Collection (a key-value mapping collection). This traditional mode loads the entire result set from the database query into memory at once. In scenarios involving small amounts of data, this approach, with its simple and intuitive operation, can quickly complete data acquisition and processing. Developers can easily iterate, filter, and analyze data using collection methods. However, when faced with large result sets, especially when processing massive amounts of data, this one-time loading mechanism forces all data into memory, easily leading to rapid memory exhaustion and OutOfMemory (OOM) errors. This severely impacts application stability and performance, and can even cause system crashes. Secondly, MyBatis has its own Cursor interface. During data processing, the Cursor interface does not read all data at once. Instead, it reads a row from the JDBC (an application programming interface for Java programs to access relational databases) result set only during iteration. This "row-by-row reading" mechanism keeps memory usage at a constant level when processing millions or even larger datasets, effectively avoiding performance bottlenecks caused by large amounts of data flooding into memory. It provides developers with a more efficient and resource-friendly way to process large data sets, demonstrating significant performance advantages when handling large datasets.
[0038] With the continuous evolution of Java technology, the introduction of the Stream processing mechanism in JDK 8 has brought about a revolutionary change in data processing methods. Stream processing, with its concise and elegant chained operation 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-based results. This not only limits the flexibility and sophistication of MyBatis in data processing but also poses a significant obstacle for developers migrating JPA (Java Continuous Application Interface) code that uses Stream processing logic to the MyBatis framework, preventing them from fully leveraging the advantages of Stream processing in MyBatis application scenarios.
[0039] In current MyBatis applications, the lack of native support for returning streaming results, compared to similar frameworks like Spring JDBC and JPA, significantly increases development costs and technical barriers during code migration. Furthermore, developers implementing this functionality must manually modify and encapsulate it; however, encapsulation involves more than just type conversion, and errors can easily lead to resource leaks, severely impacting system stability.
[0040] Based on this, the present invention addresses this critical deficiency in the data processing capabilities of the existing MyBatis framework, providing a complete and efficient method for supporting the return of streaming results. Through the technical solution of this invention, the powerful data persistence capabilities of the MyBatis framework and the superior advantages of Stream processing can be fully integrated, providing developers with more flexible and efficient data processing methods. It also provides a feasible path for JPA-based code migration, greatly enhancing the adaptability and competitiveness of the MyBatis framework in modern enterprise application development.
[0041] Please refer to the details. Figure 1 , Figure 1 This is a flowchart illustrating a streaming result return method based on the MyBatis framework, provided as 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 type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library; if so, execute S102.
[0043] In this embodiment, the execution entity is a processor within the MyBatis framework. Specifically, in the `execute` method of the `MapperMethod` class (which manages the mapping between SQL in XML and Java interface methods), it first determines whether the currently executed SQL operation is a `Select` operation, and simultaneously checks whether the return type of the SQL mapping method is a `Stream`. This is because in `MapperMethod`, the `execute` method routes different processing logic based on the SQL type. For example, for `Update`, the `executeUpdate` method is called, while for `Select`, `executeForResultSet` or `executeForCursor` is called. Therefore, before determining whether the return type is a `Stream`, it is necessary to confirm that this is a `Select` operation; otherwise, the subsequent cursor processing logic cannot be applied.
[0044] It's important to clarify that in the MyBatis framework, methods in the Mapper interface define database operations, with each method corresponding to a SQL statement (configured via XML or annotations). The SQL mapping mentioned above refers to the SQL operation configuration corresponding to the Mapper interface method (such as the SQL statement defined in XML or annotations). In the MyBatis framework specification, the return type of the Mapper interface method must be logically compatible with the return type of the SQL mapping configuration (such as resultType / resultMap in XML). Therefore, checking whether the return type of the MyBatis framework's Mapper interface method is a Java standard library stream type and checking whether the return type of the SQL mapping method is a Stream type have the same meaning. They are at different levels: whether the return type of the Mapper interface method is a Java standard library stream type refers to the return type declared in the Java interface method (type declaration at the code level); whether the return type of the SQL mapping method is a Stream type refers to the expected return type of the SQL operation configuration (type declaration at the configuration level).
[0045] The above-mentioned check to see if the return type of a Mapper interface method in the MyBatis framework is a literal type from the Java standard library can be determined by parsing the MyBatis method signature. The "method" in the method signature refers to the Java method defined in the Mapper interface of the MyBatis framework.
[0046] As can be seen from step S101, the prerequisites for conversion include two: (1) the current SQL operation is a query operation; (2) the return type of the Mapper interface method of the MyBatis framework is a 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 Select operations will return a result set, while other operations usually return the number of rows affected or void. Therefore, even if the return 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 lead to errors. If a non-Select operation uses a cursor incorrectly, it may lead to resource leaks or unexpected behavior. Therefore, MyBatis must restrict the use of cursors to only Select operations. The return type judgment determines whether to convert to Stream, while 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, thus ensuring functional correctness and system stability.
[0047] The execution code for specific 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;
[0055] / / ...
[0056] } else if (method.returnsStream()) {
[0057] / / The return type of the SQL mapping method is Stream;
[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, before checking whether the return type of the Mapper interface method in the MyBatis framework is a stream type from the Java standard library if the current SQL operation is a query operation, the following steps may also be included:
[0068] In the inner class MethodSignature of the MapperMethod class, a new boolean property returnsStream is added, and the returnsStream property is initialized. The returnsStream property is used to determine whether the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library.
[0069] Specifically, in step S101, the return type of the Mapper interface method in the MyBatis framework is checked to see if it is a stream type from the Java standard library. This is achieved by adding a boolean attribute `returnsStream` (with a stream return type) to the inner class `MethodSignatur` (method signature) of the `MapperMethod` class. This attribute is used to determine whether the return type of the Mapper interface method in the MyBatis framework is a stream type from the Java standard library. Simultaneously, the `returnsStream` attribute is initialized in the constructor of `MethodSignatur`.
[0070] The specific code is as follows:
[0071] public static class MethodSignature {
[0072] / / ...
[0073] / / Creates a returnsStream to specify that the method's return type is Stream;
[0074] private final boolean returnsStream;
[0075] / / ...
[0076] public MethodSignature(Configuration configuration, Class<?> mapperInterface, Method method) {
[0077] / / ...
[0078] / / In the constructor, a value is assigned to returnSStream to determine whether the return value type is Stream;
[0079] this.returnsStream = Stream.class.equals(this.returnType);
[0080] / / ...
[0081] }
[0082] }
[0083] S102: Obtain the cursor object, convert the cursor object to 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 avoid resource leaks, a close callback function needs to be registered on the stream object. When the operation on the stream object is complete, the close callback function will be automatically called, thus ensuring that the cursor object is properly closed and the database connection resources are released.
[0085] Furthermore, if the above is true, then obtain the cursor object, convert the cursor object to a stream object using the Java standard library, and register a close callback function on the stream object. Specifically, this may include the following steps:
[0086] If the return type of the Mapper interface method in the MyBatis framework is a stream type from the Java standard library, then the MyBatis framework's execute cursor query method is called to obtain the cursor object; cursor.iterator() is called 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 cursor object's closing operation to the stream object.
[0087] Specifically, the `executeForCursor` method provided by the MyBatis framework is called to obtain the cursor object of the query results. After obtaining the cursor object, 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 the use of the rich operations provided by the Java Stream API, such as filtering, mapping, and aggregation, to process the query results more conveniently and efficiently. It should be noted that the `onClose` method registers a callback function for closing the stream object. This function is automatically executed when the stream terminates (e.g., when `close()` is called or the `try-with-resources` block ends). By combining the `onClose` method, the MyBatis framework's Cursor resource closing logic is deeply bound to the Stream lifecycle. When the Stream operation is complete, 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 improves system stability and security, and provides reliable protection for large-scale data query scenarios.
[0088] Furthermore, the above-mentioned conversion of iterators into stream objects can specifically include the following steps: using the stream support utility class (StreamSupport class) and the splittable iterator utility class (Spliterators class) in the Java standard library to convert the iterator into a stream object.
[0089] Furthermore, the aforementioned stream objects can implement the java.util.stream.Stream standard interface, support stream operations such as filter, map, and collect, and can automatically close data resources through the resource auto-management syntax.
[0090] Specifically, (1) Java's Stream interface (java.util.stream.Stream) is the standard contract for stream operations, and any stream implementation must follow its defined methods (such as filter, map, collect). The stream objects generated by the MyBatis framework through StreamSupport.stream (the stream generation method of the stream support utility class) are essentially ReferencePipeline (the implementation class of Java standard streams), which naturally meets this condition. This unifies interface compatibility, and developers do not need to learn the Cursor API specific to the MyBatis framework. They can directly use familiar Stream methods (such as forEach, 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. Stream objects converted from cursors inherit these capabilities because they are still standard Streams in essence. By chaining filter / map / reduce operations, 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. While Stream itself does not implement AutoCloseable, similar behavior can be simulated through the callback function bound to onClose. When the stream is explicitly closed (such as in a Stream subclass wrapped in try-with-resources), the callback is triggered to release the cursor. By combining try-with-resources, it ensures that the cursor and database connection are always closed, avoiding connection pool leaks caused by forgetting to manually call cursor.close().
[0091] Furthermore, before checking whether the return type of the Mapper interface method in the MyBatis framework is a stream type from the Java standard library if the current SQL operation is a query operation, it may also include:
[0092] The return value type resolver of the MyBatis framework is extended with logic for recognizing streaming types; the extended MyBatis framework return value type resolver includes execution paths for cursor queries, streaming queries, and regular queries.
[0093] This embodiment pre-extends the logic for recognizing streaming types in the MyBatis framework's return value type resolver, allowing the resolver to parse the data and determine the return value type. Of course, this resolver supports execution paths for cursor queries, streaming queries, and regular queries.
[0094] Furthermore, the aforementioned check of whether the return type of the Mapper interface method in the MyBatis framework is a stream type from the Java standard library can specifically include:
[0095] In the MyBatis framework's mapper method execution entry point, the method return type is obtained through reflection and compared with the Java standard library's Stream type to determine the check result.
[0096] Specifically, this embodiment provides a specific method for determining the return value type. The return value type determination is implemented using reflection comparison class definitions.
[0097] The streaming result return method based on the MyBatis framework provided in this embodiment of the invention, when the current SQL operation is a query operation and the return value type is detected as Stream, reuses proprietary APIs such as MyBatis executeForCursor to obtain a Cursor object, avoiding the need to redevelop the underlying database query logic. Subsequently, using Java's standard StreamSupport and Spliterators, the Cursor iterator is converted to a Stream, and the onClose method is used to associate the Cursor's closing operation. This hides the complex details of MyBatis Cursor resource management while exposing the result externally in the form of a standard Java Stream API. By determining whether the return type is Stream, Cursor, etc., the existing MyBatis framework's type resolution logic is extended without modifying the core underlying code, conforming to the open / closed principle. This solves the problem of resource leakage from manual encapsulation and facilitates code migration to other frameworks based on standard APIs. It has the following beneficial effects: (1) Seamlessly connects to the standard stream processing ecosystem and reduces the cost of technology migration: It supports the use of the standard Java Stream interface as the return type of the query result, so that the MyBatis framework can natively support the standard Stream type. 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 in Stream support and provides developers with a safer and more convenient development experience. In addition, through this method, the smooth migration of data processing logic is realized, which greatly improves development efficiency. At the same time, it allows developers to use the familiar stream programming paradigm to easily deal with complex business needs such as data filtering, aggregation, and mapping, effectively reducing the learning cost and technical adaptation threshold of cross-framework development. (2) Efficiently reuse core code and realize low-intrusion upgrade: Deeply explore the existing functions of the MyBatis framework and realize the extension of the Stream return function without changing the core architecture of the framework by flexibly reusing key APIs such as executeForCursor. Not only were the return type judgment and result conversion logic optimized, ensuring 100% backward compatibility with older versions and avoiding the risk of system crashes due to upgrades, but the development cycle was also significantly shortened and maintenance costs were reduced by minimizing code modifications. This fully embodies the "open-closed principle" and provides a low-risk, 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 the traditional Cursor is encapsulated as a Stream, this method uses StreamSupport.stream in conjunction with the onClose method to deeply bind the Cursor resource closing logic of the MyBatis framework with the Stream lifecycle. When the Stream operation is completed, the Cursor resource is automatically released, 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 data query scenarios. (4) Improve code maintainability and extensibility: Based on the standard Java API design pattern, the code structure is clearer and more concise, and highly compatible with the mainstream Java ecosystem. Whether adding new business functions or connecting to third-party components, integration can be carried out in a more natural way, reducing code coupling. At the same time, the implementation method that follows the "open-closed principle" reserves sufficient space for the continuous expansion of future framework functions, making it easy for the development team to quickly respond to changes in business needs and achieve iterative upgrades of the system without affecting existing functions.
[0098] The following describes the streaming result return device based on the MyBatis framework provided in the embodiments of the present invention. The streaming result return device based on the MyBatis framework described below can be referred to in correspondence with the streaming result return method based on the MyBatis framework described above.
[0099] Please refer to the details. Figure 2 , Figure 2 A schematic diagram of a streaming result return device based on the MyBatis framework provided in this embodiment of the 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 used to obtain a cursor object if the condition is met, convert the cursor object into a stream object using the Java standard library, and register a close callback function on the stream object.
[0102] Based on the above embodiments, the streaming result return device based on the MyBatis framework may further include:
[0103] Add a module to add a boolean attribute `returnsStream` to the inner class `MethodSignature` of the `MapperMethod` class, and initialize the `returnsStream` attribute. The `returnsStream` attribute is used to determine whether the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library.
[0104] Based on the above embodiments, the conversion module 200 may include:
[0105] The cursor object acquisition unit is used to call the cursor query execution method of the MyBatis framework to obtain 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 retrieval unit is used to call cursor.iterator() to retrieve an iterator from the cursor object; the iterator is used to traverse the query result set row by row;
[0107] A conversion unit is used to convert the iterator into the stream object;
[0108] A binding unit is used to bind the close operation of the cursor object to the stream object using the onClose method.
[0109] Based on the above embodiments, the conversion unit may include:
[0110] The conversion subunit is used to convert the iterator into the stream object using the stream support utility class and the splittable iterator utility class in the Java standard library.
[0111] Based on the above embodiments, 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 the resource auto-management syntax.
[0112] Based on the above embodiments, the streaming result return device based on the MyBatis framework may further include:
[0113] An extension module is used to extend the recognition logic for the streaming type in the return value type resolver of the MyBatis framework; the extended return value type resolver of the MyBatis framework includes execution paths for cursor queries, streaming queries, and regular queries.
[0114] Based on the above embodiments, the determination module 100 may include:
[0115] The comparison unit is used to obtain the return type of the method through reflection in the execution entry point of the mapper method in the MyBatis framework, and compare it with the Stream type of the Java standard library to determine the check result.
[0116] It should be noted that the order of the modules and units in the above-mentioned MyBatis-based streaming result return device can be changed without affecting the logic.
[0117] This invention provides a streaming result return device based on the MyBatis framework. A judgment module 100 checks whether the return type of the Mapper interface method in the MyBatis framework is a streaming type from the Java standard library if the current SQL operation is a query operation. A conversion module 200 obtains a cursor object, converts it to a stream object using the Java standard library, and registers a close callback function on the stream object. This device extends the MyBatis framework to enable streaming result return, reducing development workload while maintaining performance consistent with the native MyBatis framework. Furthermore, it does not modify the core MyBatis code, avoiding risks and ensuring existing functionality remains unaffected. By binding the cursor object's close function to the stream object, users do not need to manually manage resources, preventing resource leaks.
[0118] The following describes the streaming result return device based on the MyBatis framework provided in the embodiments 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 be referred to and correspond to each other.
[0119] Please refer to Figure 3 , Figure 3 A schematic diagram of a streaming result return device based on the MyBatis framework, provided as an embodiment of the present invention, may include:
[0120] Memory 10 is used to store computer programs;
[0121] Processor 20 is used to execute computer programs to implement the above-described MyBatis framework-based streaming result return method.
[0122] The memory 10, processor 20, and communication interface 31 all communicate with each other through the communication bus 32.
[0123] In this embodiment of the invention, the memory 10 is used to store one or more programs. The programs may include program code, which includes computer operation instructions. In this embodiment of the invention, the memory 10 may store programs for implementing the following functions:
[0124] If the current SQL operation is a query operation, then check whether the return type of the Mapper interface method in the MyBatis framework is a stream type from the Java standard library;
[0125] If so, obtain the cursor object, convert the cursor object to 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 the operating system and applications required for at least one function; and the data storage area may store data created during use.
[0127] Furthermore, memory 10 may include read-only memory and random access memory, providing instructions and data to the processor. A portion of the memory may also include NVRAM. The memory stores operating systems and operating instructions, executable modules, or data structures, or subsets thereof, or extended sets thereof, wherein 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 handling hardware-based tasks.
[0128] Processor 20 can 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. Processor 20 can be a microprocessor or any conventional processor. Processor 20 can call programs stored in memory 10.
[0129] Communication interface 31 can be an interface for the communication module, used to connect with other devices or systems.
[0130] Of course, it should be noted that, Figure 3 The structure shown does not constitute a limitation on the streaming result return device based on the MyBatis framework in the embodiments of the present invention. In practical applications, the streaming result return device based on the MyBatis framework may include more than Figure 3 More or fewer components as shown, or combinations of certain components.
[0131] The following describes the computer-readable storage medium provided in the embodiments of the present invention. The computer-readable storage medium described below can be referred to in correspondence with the streaming result return method based on the MyBatis framework described above.
[0132] The present invention also provides a computer-readable storage medium storing a computer program, wherein when the computer program is executed by a processor, it implements the above-described steps of the streaming result return method based on the MyBatis framework.
[0133] The computer-readable storage medium may include various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0134] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For the apparatus disclosed in the embodiments, since it corresponds to the method disclosed in the embodiments, the description is relatively simple; relevant parts can be referred to in the method section.
[0135] Those skilled in the art will further recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementations should not be considered beyond the scope of this invention.
[0136] Finally, it should be noted that in this document, relationships such as "first" and "second" are used merely to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises 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 foregoing has provided a detailed description of a streaming result return method, apparatus, device, and computer-readable storage medium based on the MyBatis framework provided by the present invention. Specific examples have been used to illustrate the principles and implementation methods of the present invention. The descriptions of the above embodiments are only for the purpose of helping to understand the method and core ideas of the present invention. At the same time, for those skilled in the art, there will be changes in specific implementation methods and application scope based on the ideas of the present invention. Therefore, the content of this specification should not be construed as a limitation of 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, then check whether the return type of the Mapper interface method in the MyBatis framework is a stream type from the Java standard library; If so, obtain the cursor object, use the Java standard library to convert the cursor object into a stream object, and register a close callback function on the stream object; Before checking whether the return type of the Mapper interface method in the MyBatis framework is a stream type from the Java standard library if the current SQL operation is a query operation, the following steps are also included: In the inner class MethodSignature of the MapperMethod class, a new boolean attribute returnsStream is added, and the returnsStream attribute is initialized. The returnsStream attribute is used to determine 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, obtain the cursor object, convert the cursor object to a stream object using the Java standard library, and register a close callback function on the stream object, including: If the return type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library, then the 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.
2. The streaming result return method based on the MyBatis framework according to claim 1, characterized in that, Converting the iterator into the stream object includes: The iterator is converted into the stream object using the stream support utility class and the splittable iterator utility class in the Java standard library.
3. 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 the resource auto-management syntax.
4. The streaming result return method based on the MyBatis framework according to claim 1, characterized in that, Before checking whether the return type of the Mapper interface method in the MyBatis framework is a stream type from the Java standard library if the current SQL operation is a query operation, the following steps are also included: The return value type resolver of the MyBatis framework is extended with logic for recognizing the streaming type; the extended MyBatis framework return value type resolver includes execution paths for cursor queries, streaming queries, and regular queries.
5. The streaming result return method based on the MyBatis framework according to claim 1, characterized in that, Check whether the return type of the Mapper interface methods in the MyBatis framework is a Java standard library stream type, including: In the mapper method execution entry point of the MyBatis framework, the return type of the method is obtained through reflection and compared with the Stream type of the Java standard library to determine the check result.
6. A streaming result return device based on the MyBatis framework, characterized in that, include: The judgment module is used to check whether the return 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 the cursor object if yes, convert the cursor object into a stream object using the Java standard library, and register a close callback function on the stream object; Add a module to add a boolean attribute `returnsStream` to the inner class `MethodSignature` of the `MapperMethod` class, and initialize the `returnsStream` attribute. The `returnsStream` attribute is used to determine whether the return value type of the Mapper interface method of the MyBatis framework is a stream type of the Java standard library. The conversion module includes: The cursor object acquisition unit is used to call the cursor query execution method of the MyBatis framework to obtain 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; An iterator retrieval unit is used to call cursor.iterator() to retrieve an iterator from the cursor object; the iterator is used to traverse the query result set row by row; A conversion unit is used to convert the iterator into the stream object; A binding unit is used to bind the close operation of the cursor object to the stream object using the onClose method.
7. A streaming result return device based on the MyBatis framework, characterized in that, include: Memory, used to store computer programs; A processor for executing the computer program to implement the streaming result return method based on the MyBatis framework as described in any one of claims 1 to 5.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, which, when loaded and executed by a processor, implement the streaming result return method based on the MyBatis framework as described in any one of claims 1 to 5.
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