Data access method and device
By calling the target generic interface and interceptors in the Mybatis framework to dynamically generate database statements, the problem of high data access costs in existing technologies is solved, and the effect of modifying database statements without recompiling is achieved.
Patent Information
- Application Number
- CN202410598572.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-14
- Publication Date
- 2025-11-14
AI Technical Summary
The existing Mybatis framework requires recompilation and deployment when database statements are modified, resulting in high data access costs.
By calling the target's general interface to parse data access requests, using the target interceptor to intercept the execution of database statements, and determining the target mapping declaration object based on the statement identifier, a second database statement is dynamically generated, avoiding the need for application recompilation and deployment.
It reduces the cost of data access and enables the ability to modify database statements in real time without recompiling the application.
Smart Images

Figure CN120950133A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, specifically to a data access method and apparatus. Background Technology
[0002] Mybatis is an open-source Java persistence framework that simplifies database access. Its main goal is to decouple database operations from Java code, providing a flexible mapping method that allows developers to map database statements to Java methods using simple configuration files. However, existing Mybatis frameworks require application recompilation and deployment for every modification to a database statement, resulting in high maintenance costs.
[0003] In other words, existing data access methods are costly. Summary of the Invention
[0004] This application provides a data access method and apparatus that can reduce the cost of data access.
[0005] Firstly, the data access methods provided in this application include:
[0006] Upon receiving a data access request, the target general interface is called to parse the data access request and obtain the first database statement for data access.
[0007] When the first database statement is executed, the target interceptor is invoked to intercept the execution of the first database statement, and the corresponding target mapping declaration object is determined based on the statement identifier of the first database statement. Different database statements correspond to different mapping declaration objects, and the mapping declaration object is used to generate database statements for data access.
[0008] Based on the target mapping declaration object, a second database statement for data access is dynamically generated;
[0009] Execute the second database statement to obtain the data access results.
[0010] In an optional embodiment, the data access method further includes:
[0011] Retrieve the target source data from the pre-configured database statement;
[0012] Obtain the annotation information of the target general interface;
[0013] Parse the annotation information of the target generic interface to obtain a generic mapping declaration object;
[0014] The target source data and statement identifier in the general mapping declaration object are replaced with the target source data and statement identifier of the configuration database statement to obtain the mapping declaration object corresponding to the configuration database statement.
[0015] In an optional embodiment, obtaining the target source data of the pre-configured configuration database statement includes:
[0016] Retrieve pre-configured configuration database statements from the remote configuration system and cache them locally;
[0017] The target source data corresponding to the configuration database statement is constructed based on the configuration database statement.
[0018] In an optional embodiment, the data access method further includes:
[0019] The configuration database statements stored in the remote configuration system are cached to local storage at a preset period.
[0020] In an optional embodiment, the step of dynamically generating a second database statement for data access based on the target mapping declaration object includes:
[0021] Obtain the target source data encapsulated in the target mapping declaration object, wherein the target source data includes at least one node label, and the node label contains the variable to be operated on and the corresponding variable expression;
[0022] The second database statement is dynamically generated based on the target source data.
[0023] In an optional embodiment, the data access method further includes:
[0024] Create a target interceptor;
[0025] Add annotation information to the target interceptor.
[0026] In an optional embodiment, adding annotation information to the target interceptor includes:
[0027] The target interceptor is identified by the first annotation;
[0028] The second annotation specifies the type and method of the target interceptor.
[0029] In an optional embodiment, the data access method further includes:
[0030] The third annotation is used to write various configuration database statements in the target general interface.
[0031] In an optional embodiment, the return result of the target general interface satisfies a preset data structure, which is a list, where each element is a mapping, and the mapping stores and accesses elements through key-value pairs.
[0032] In an optional embodiment, the data access method includes:
[0033] Upon receiving a statement configuration instruction, the remote configuration system is used to obtain the input configuration database statement and its corresponding statement identifier.
[0034] The configuration database statements and corresponding statement identifiers obtained by the remote configuration system are cached locally.
[0035] In an optional embodiment, the step of obtaining the input configuration database statement and the corresponding statement identifier using the remote configuration system when a statement configuration instruction is obtained includes:
[0036] Upon receiving the statement configuration instruction, a statement configuration interface will pop up;
[0037] The remote configuration system is used to obtain the configuration database statement and corresponding statement identifier entered by the user in the statement configuration interface.
[0038] Secondly, the data access device provided in this application includes:
[0039] The parsing module is used to call the target general interface to parse the data access request when a data access request is obtained, and obtain the first database statement for data access.
[0040] The interception module is used to call the target interceptor to intercept the execution of the first database statement when the first database statement is executed, and to determine the corresponding target mapping declaration object based on the statement identifier of the first database statement. Different database statements correspond to different mapping declaration objects, and the mapping declaration object is used to generate database statements for data access.
[0041] The statement generation module is used to dynamically generate a second database statement for data access based on the target mapping declaration object;
[0042] The statement execution module is used to execute the second database statement and obtain the data access results.
[0043] In an optional embodiment, the data access device further includes a first configuration module, the first configuration module being configured to:
[0044] Retrieve the target source data from the pre-configured database statement;
[0045] Obtain the annotation information of the target general interface;
[0046] Parse the annotation information of the target generic interface to obtain a generic mapping declaration object;
[0047] The target source data and statement identifier in the general mapping declaration object are replaced with the target source data and statement identifier of the configuration database statement to obtain the mapping declaration object corresponding to the configuration database statement.
[0048] In an optional embodiment, the first configuration module is configured to: obtain pre-configured configuration database statements from a remote configuration system and cache them locally;
[0049] The target source data corresponding to the configuration database statement is constructed based on the configuration database statement.
[0050] In an optional embodiment, the data access device further includes a caching module, the caching module being used for:
[0051] The configuration database statements stored in the remote configuration system are cached to local storage at a preset period.
[0052] In an optional embodiment, the interception module includes:
[0053] The acquisition module is used to acquire the target source data encapsulated by the target mapping declaration object, wherein the target source data includes at least one node label, and the node label contains the variable to be operated on and the corresponding variable expression;
[0054] The modification module is used to dynamically generate a second database statement based on the target source data.
[0055] In an optional embodiment, the data access device further includes:
[0056] The interceptor creation module is used to create target interceptors;
[0057] The annotation module is used to add annotation information to the target interceptor.
[0058] In an optional embodiment, the annotation module is configured to:
[0059] The target interceptor is identified by the first annotation;
[0060] The second annotation specifies the type and method of the target interceptor.
[0061] In an optional embodiment, the annotation module is configured to:
[0062] The third annotation is used to write various configuration database statements in the target general interface.
[0063] In an optional embodiment, the return result of the target general interface satisfies a preset data structure, which is a list, where each element is a mapping, and the mapping stores and accesses elements through key-value pairs.
[0064] In an optional embodiment, the data access device includes a second configuration module, the second configuration module being used for:
[0065] Upon receiving a statement configuration instruction, the remote configuration system is used to obtain the input configuration database statement and its corresponding statement identifier.
[0066] The configuration database statements and corresponding statement identifiers obtained by the remote configuration system are cached locally.
[0067] In an optional embodiment, the second configuration module is used to:
[0068] Upon receiving the statement configuration instruction, a statement configuration interface will pop up;
[0069] The remote configuration system is used to obtain the configuration database statement and corresponding statement identifier entered by the user in the statement configuration interface.
[0070] Thirdly, the electronic device provided in this application includes a memory and a processor. The memory stores a computer program, and the processor is used to run the computer program in the memory to implement the steps in the data access method provided in this application.
[0071] Fourthly, the computer-readable storage medium provided in this application stores multiple instructions that are adapted for loading by a processor to implement the steps in the data access method provided in this application.
[0072] Fifthly, the computer program product provided in this application includes a computer program or instructions that, when executed by a processor, implement the steps in the data access method provided in this application.
[0073] In this application, compared to related technologies, upon receiving a data access request, a target generic interface is invoked to parse the data access request, obtaining a first database statement for data access. Upon execution of the first database statement, a target interceptor is invoked to intercept the execution of the first database statement, and a corresponding target mapping declaration object is determined based on the statement identifier of the first database statement, wherein different database statements correspond to different mapping declaration objects. A second database statement for data access is dynamically generated based on the target mapping declaration object. The second database statement is then executed to obtain the data access result. This application utilizes a pre-written target generic interface to parse user access requests, obtain and execute the first database statement, and then uses an interceptor to intercept the execution of the first SQL statement. The database statement is then dynamically generated using the target mapping declaration object corresponding to the first database statement, resulting in a new second database statement. By parsing and executing statements through a generic interface and relying on intercepted statement execution, the database statement can be modified in real time, eliminating the need for application recompilation and deployment, thereby reducing the cost of data access. Attached Figure Description
[0074] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0075] Figure 1 This is a schematic diagram of a data access system provided in an embodiment of this application;
[0076] Figure 2 This is a schematic flowchart of an embodiment of the data access method provided in this application;
[0077] Figure 3 This is a schematic diagram of the statement configuration interface in one embodiment of the data access method provided in this application;
[0078] Figure 4 This is a schematic flowchart of another embodiment of the data access method provided in this application;
[0079] Figure 5 This is a schematic flowchart of another embodiment of the data access method provided in this application;
[0080] Figure 6 This is a schematic diagram of the structure of an embodiment of the data access device provided in this application;
[0081] Figure 7 This is a schematic diagram of another embodiment of the data access device provided in this application;
[0082] Figure 8 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0083] It should be noted that the principles of this application are illustrated by example in a suitable computing environment. The following description is based on the specific embodiments of this application that are illustrated, and should not be regarded as limiting other specific embodiments not detailed herein.
[0084] In the following description of this application, "some embodiments" are referred to, which describe a subset of all possible embodiments. However, it is understood that "some embodiments" may be the same subset or different subset of all possible embodiments, and may be combined with each other without conflict.
[0085] In the following description of this application, the terms "first, second, third" are used merely to distinguish similar objects and do not represent a specific ordering of objects. It is understood that "first, second, third" may be interchanged in a specific order or sequence where permitted, so that the embodiments of this application described herein can be implemented in an order other than that illustrated or described herein.
[0086] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application belongs. The terminology used herein is for the purpose of describing embodiments of this application only and is not intended to limit this application.
[0087] Before introducing this application, some terms used in this application will be explained:
[0088] SQL (Structured Query Language) is a database language with multiple functions, including data manipulation and definition. This interactive language provides great convenience to users. Database management systems should fully utilize SQL to improve the quality and efficiency of computer application systems. SQL can not only be used independently in the terminal but also serve as a sub-language to effectively assist in other program design. In program applications, SQL can work with other programming languages to optimize program functionality, thereby providing users with more comprehensive information.
[0089] MyBatis is an excellent persistence framework that supports customized SQL, stored procedures, and advanced mapping. MyBatis can use simple XML or annotations to configure and map native information, mapping interfaces and Java POJOs (Plain Ordinary Java Objects) to records in the database. MyBatis is an excellent persistence framework that supports plain SQL queries, stored procedures, and advanced mapping. MyBatis eliminates almost all JDBC code, manual parameter setting, and result set retrieval. Each MyBatis application primarily uses a SqlSessionFactory instance, which can be obtained through a SqlSessionFactoryBuilder. A SqlSessionFactoryBuilder can be obtained from an XML configuration file or an instance of a predefined configuration class. Building a SqlSessionFactory instance using an XML file is very straightforward. Using a classpath resource in this configuration is recommended, but you can use any Reader instance, including those created using file paths or URLs starting with file: / / . MyBatis has a utility class called Resources, which has many methods that make it easy to load resources from the classpath and other locations.
[0090] MyBatis has four core objects:
[0091] (1) The SqlSession object contains all the methods for executing SQL statements. It is similar to the Connection object in JDBC.
[0092] (2) The Executor interface dynamically generates the SQL statements to be executed based on the parameters passed to the SqlSession, and is also responsible for maintaining the query cache. It is similar to Statement / PrepareStatement in JDBC.
[0093] (3) The MappedStatement object is a wrapper for the mapped SQL, used to store information such as the ID and parameters of the SQL statement to be mapped.
[0094] (4) The ResultHandler object is used to process the returned results and obtain the desired data format or type. The return type can be customized.
[0095] Dynamic SQL is a technique for constructing and executing SQL statements at runtime. Typically, static SQL consists of predefined SQL statements, while dynamic SQL allows SQL statements to be dynamically generated based on the conditions and requirements of the program at runtime. The main advantages of dynamic SQL are flexibility and adaptability. It can generate different SQL statements according to different scenarios and needs, thus providing greater flexibility and reusability. For example, in a search function, users may search based on different conditions; dynamic SQL can construct corresponding query statements based on the user's input. Dynamic SQL dynamically generates different SQL fragments within the SQL statement based on different conditions. This allows for the construction of flexible SQL statements according to different situations, thereby meeting various complex query requirements. Mybatis supports a rich set of dynamic SQL tags.
[0096] To improve the efficiency of data access, embodiments of this application provide a data access method, a data access device, an electronic device, a computer-readable storage medium, and a computer program product. The data access method can be executed by the data access device or by an electronic device integrating the data access device.
[0097] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0098] This application also provides a data access system, which includes an electronic device. The electronic device integrates the data access apparatus provided in this application.
[0099] To better understand the data access method, apparatus, electronic device, and storage medium provided in the embodiments of this application, the application environment applicable to the embodiments of this application is described below.
[0100] Please see Figure 1 , Figure 1 This diagram illustrates an application environment for a data access method provided in an embodiment of this application. As one implementation, the data access method provided in this embodiment can be applied to an electronic device. The electronic device can be, for example,... Figure 1The server 110 shown can be connected to the terminal device 120 via a network. The network serves as a medium for providing a communication link between the server 110 and the terminal device 120. The network can include various connection types, such as wired communication links, wireless communication links, etc., and this embodiment is not limited thereto. Optionally, in other embodiments, the electronic device can also be a smartphone, laptop, etc.
[0101] It should be understood that Figure 1 The server 110, network, and terminal device 120 shown are merely illustrative. Depending on the implementation requirements, any number of servers, networks, and terminal devices can be included. For example, server 110 can be a physical server or a server cluster consisting of multiple servers, and terminal device 120 can be a mobile phone, tablet, desktop computer, laptop computer, etc. It is understood that embodiments of this application can also allow multiple terminal devices 120 to access server 110 simultaneously.
[0102] In this embodiment, the data access system includes a remote configuration system 130. The remote configuration system 130 can be a mobile phone, tablet, desktop computer, laptop computer, or other device. Of course, the remote configuration system 130 can also be a database, depending on the specific situation. The remote configuration system 130 is independent of the server 110, and the remote configuration system 130 and the server 110 are connected via a network.
[0103] In addition, the data access system may also include a memory for storing raw data, intermediate data, and result data during the data processing.
[0104] In this embodiment of the application, the storage device can be a cloud storage device. Cloud storage is a new concept that is extended and developed from the concept of cloud computing. A distributed cloud storage system (hereinafter referred to as a storage system) refers to a storage system that uses cluster applications, grid technology and distributed storage file system functions to bring together a large number of storage devices of various types in the network (storage devices are also called storage nodes) through application software or application interfaces to work together to provide data storage and business access functions to the outside world.
[0105] Currently, the storage method of storage systems is as follows: Logical volumes are created. During the creation of a logical volume, physical storage space is allocated to each logical volume. This physical storage space may consist of a single storage device or the disks of several storage devices. Clients store data on a logical volume, which means storing the data on the file system. The file system divides the data into many parts, each part being an object. Each object contains not only the data but also additional information such as a data identifier (ID entity, ID). The file system writes each object to the physical storage space of that logical volume and records the storage location information of each object. Therefore, when a client requests access to data, the file system can allow the client to access the data based on the storage location information of each object.
[0106] The process by which a storage system allocates physical storage space to a logical volume is as follows: the physical storage space is pre-divided into strips according to the capacity estimate of the objects stored in the logical volume (this estimate often has a large margin relative to the actual capacity of the objects to be stored) and the grouping of Redundant Array of Independent Disks (RAID). A logical volume can be understood as a strip, thus allocating physical storage space to the logical volume.
[0107] It should be noted that, Figure 1 The schematic diagram of the data access system shown is merely an example. The data access system and scenario described in this application are intended to more clearly illustrate the technical solutions of this application and do not constitute a limitation on the technical solutions provided in this application. As those skilled in the art will know, with the evolution of data access systems and the emergence of new business scenarios, the technical solutions provided in this application are also applicable to similar technical problems.
[0108] The following sections provide detailed descriptions of each example. It should be noted that the sequence numbers of the following embodiments are not intended to limit the preferred order of the embodiments.
[0109] Please refer to Figure 2 , Figure 2 This is a schematic flowchart of an embodiment of the data access method provided in this application, as shown below. Figure 2 As shown, the data access method provided in this application follows this process:
[0110] 201. Upon receiving a data access request, call the target general interface to parse the data access request and obtain the first database statement.
[0111] In this embodiment, the data access request is a user-input data access request. The user can trigger the terminal device 120 to send a data access request to the server 110 by clicking a query control on the display page of the terminal device 120. The server 110 receives the data access request.
[0112] The database statements can be SQL statements. However, in other embodiments, the database statements can also be DDL (Data Definition Language) statements. DDL statements are used to define the structure of databases, tables, columns, etc., including statements for creating, modifying, and deleting database objects.
[0113] Data access requests can be data query requests, data insertion requests, data deletion requests, data update requests, etc., depending on the specific settings. For example, a data access request can be a data query request, which may include the target user's user identifier and business query information. The user identifier may include, but is not limited to, a user name, user code, or user login account. For instance, the business query information could be: "Query the target user's daily spending for the past week."
[0114] The target generic interface is a predefined generic interface. For example, the target generic interface is the generic Mapper interface predefined by the MyBatis framework. In MyBatis, a Mapper interface is an interface that defines database operation methods. These methods typically correspond to database statements and are used to perform CRUD operations on the database. The methods in the Mapper interface can be associated with the actual database statements through MyBatis' mapping configuration file. The purpose of the Mapper interface is to decouple the logic of database operations from the actual database statements, making the code in the data access layer clearer and more maintainable. By defining a Mapper interface, these methods can be called directly in Java code to perform database operations without having to write the database statements directly.
[0115] Different data access requests correspond to different database statements. The target general interface parses and maps different data access requests to different database statements. For example, data access request A corresponds to SQL statement A, data access request B corresponds to SQL statement B, and data access request C corresponds to SQL statement C. When data access request A is obtained, the target general interface is called to parse data access request A and map it to SQL statement A, resulting in the first SQL statement. The mapping relationship between data access requests and SQL statements can be configured according to specific circumstances, and this application does not impose any limitations on it.
[0116] 202. When executing the first database statement, call the target interceptor to intercept the execution of the first database statement, and determine the corresponding target mapping declaration object based on the statement identifier of the first database statement.
[0117] Different database statements correspond to different mapping declaration objects. These mapping declaration objects are constructed based on the database statements. In the MyBatis framework, each Mapper method corresponds to a MappedStatement object, which contains complete SQL information, input parameters, and output parameter definitions. For example, database statement A corresponds to MappedStatement object A, and database statement B corresponds to MappedStatement object B. Database statements and their corresponding mapping declaration objects are bound together by an identifier.
[0118] The statement identifier can be a string of specific characters used to distinguish different database statements.
[0119] In this embodiment, the target interceptor can be a MyBatis interceptor. MyBatis interceptors are primarily used to insert custom behavior or logic into the SQL execution process of MyBatis without directly modifying the MyBatis framework's own code. The design purpose of interceptors is to allow developers to customize specific execution flows according to their own needs without affecting the core functionality of MyBatis. Taking SQL statements as an example, MyBatis interceptors can be applied to the following aspects: Enhancing or modifying SQL: For example, SQL statements can be dynamically modified based on runtime conditions, such as adding additional WHERE conditions or dynamically changing table names. They can be used to record SQL execution time and collect SQL statistics for performance analysis of database operations. Access control: Checking user permissions before executing SQL and deciding whether to execute or modify SQL based on permissions. Parameter processing: Performing special processing on parameters before passing them to SQL, such as encrypting or decrypting sensitive data, or uniformly converting date formats. Result set processing: Processing query results before returning them to the client, such as field filtering, data anonymization, and result merging. Exception handling: Uniformly handling database operation exceptions and providing unified error messages or log records.
[0120] In this embodiment, the target interceptor can be an executor interceptor. An executor interceptor can be used to intercept the execution of MyBatis executor methods, thereby intercepting the execution of SQL statements. Executor interceptors allow for the interception and customization of the MyBatis executor's behavior. For example, caching, logging, or auditing functionality can be added to the executor. These interceptors can extend or modify their behavior at different stages of MyBatis execution. You can implement these interceptors by implementing the corresponding interfaces provided by MyBatis and configuring them in the MyBatis configuration file.
[0121] In this embodiment of the application, before obtaining the data access request, the data access method further includes:
[0122] (1) Create a target interceptor.
[0123] Specifically, define an interceptor class that implements the org.apache.ibatis.plugin.Interceptor interface, and override the intercept, plugin, and setProperties methods to create the target interceptor.
[0124] The `InnerInterceptor` interface is an interceptor interface in the MyBatis-Plus framework. It defines preprocessing methods that perform operations before querying and updating, such as before querying, updating, and preparing the SQL. By implementing this interface, developers can create their own plugins and add them to MyBatis, enabling interceptor functionality. Specifically, when these preprocessing methods are called, they are actually invoking the corresponding MyBatis plugin. Therefore, the main purpose of the `InnerInterceptor` interface is to provide a way to customize plugins to execute user-defined logic at specific points in time. The `plugin` method allows MyBatis to determine whether to intercept and then decide whether to generate a proxy. `setProperties` contains a configurable object containing variables required by the interceptor.
[0125] (2) Add annotation information to the target interceptor.
[0126] The annotation information of the target interceptor can be various annotations, such as @Intercepts, @Signature, @Slf4j and @Override, etc., and the interface can be set according to the specific situation.
[0127] The `@Intercepts` annotation identifies the current object as an interceptor, and its configuration value is a collection of `@Signature` annotations. The `@Signature` annotation identifies the interface, method, and corresponding parameter list to be intercepted. The `@Slf4j` annotation, provided by Lombok, automatically generates a log object named `log` in the class. Using `@Slf4j`, logging functionality can be easily implemented in the code without manually creating and initializing the log object. The `@Override` annotation is pseudocode used to indicate that the annotated method is an overridden method. The `@Override` annotation can only be used to mark methods, and it only takes effect at compile time and is not retained in the class file.
[0128] In this embodiment of the application, adding annotation information to the target interceptor includes: identifying the target interceptor through a first annotation; and specifying the type and method of the target interceptor through a second annotation. The first annotation is the @Intercepts annotation, and the second annotation is the @Signature annotation.
[0129] In one specific embodiment, the code for creating the target interceptor is shown below.
[0130] @Intercepts({@Signature(type=Executor.class,method="query",args={MappedStatement.class,Object.class,
[0131] RowBounds.class,ResultHandler.class}),
[0132] @Signature(type=Executor.class,method="query",args={MappedStatement.class,Object.class,
[0133] RowBounds.class,ResultHandler.class,CacheKey.class,BoundSql.class})})
[0134] @Slf4jpublic class BiSqlRewriteInterceptor implements Interceptor{
[0135] public BiSqlRewriteInterceptor(){
[0136] log.info("BiSqlRewriteInterceptor init!");
[0137] }
[0138] @Override
[0139] public Object intercept(Invocation invocation) throws Throwable {
[0140] log.info("BiSqlRewriteInterceptor intercept");
[0141] Executor executor = (Executor) invocation.getTarget();
[0142] Object[] args = invocation.getArgs();
[0143] MappedStatement mappedStatement = (MappedStatement) args[0];
[0144] / / User - passed parameter information
[0145] Object parameter = args[1];
[0146] MappedStatement realMp = CACHED_BI_MP.get(MyBatisUtil.newMpId(mappedStatement.getId(), bid));
[0147] if (realMp == null) {
[0148] / / TODO:
[0149] return invocation.proceed();
[0150] }
[0151] / / Modify to the real mp
[0152] args[0] = realMp;
[0153] return invocation.proceed();
[0154] }
[0155] In this embodiment, after the target general interface is called to parse the data access request and obtain the first database statement, the first database statement is executed. When the first database statement is executed, a target interceptor is called to intercept its execution, and the corresponding target mapping declaration object is determined based on the statement identifier of the first database statement.
[0156] Specifically, the Executor interface in the MyBatis framework is called to execute the first database statement.
[0157] The mapping declaration object includes the statement identifier of the database statement to be mapped, the target source data, and parameter information. Different database statements correspond to different mapping declaration objects.
[0158] The annotation information for the target general interface can be @Select, @Update, @delete, or @insert.
[0159] In MyBatis, the `@Select` annotation allows you to directly write database statements on interface methods, avoiding the need for XML mapping files. Using the `@Select` annotation makes writing database statements more intuitive and allows direct association with interface methods, improving development efficiency and code readability. The `@Select` annotation is used to define a select statement, which is then used...<select... / > When selecting an element, the `id` attribute must be specified. This attribute value will serve as a unique identifier for this select statement. Additionally, you can specify...<select... / > The element specifies the following attributes:
[0160] parameterType: Specifies the type of the parameter passed to this database statement. This attribute supports fully qualified class names or aliases. In fact, this attribute is optional; MyBatis' type converter can infer the parameter type through reflection.
[0161] `resultType`: Specifies the type of Java object that each record in `ResultType` needs to be mapped to. Therefore, when the query returns a collection, this attribute value should be the type of the collection elements, not the type of the collection itself. This attribute value supports fully qualified class names or aliases.
[0162] resultMap: Specifies the mapping to...<resultMap... / > The reference to the element, the value of this attribute should be another<resultMap... / > The value of the element's id attribute.<select... / > The element either uses the `resultType` attribute to specify the Java type corresponding to each row of the `ResultSet`, or uses `resultMap` to reference another `ResultSet`.<resultMap... / > For elements, these two properties cannot be used simultaneously.
[0163] flushCache: If this property is set to true, it means that the local cache and second-level cache will always be cleared whenever the statement is called. The default value is false.
[0164] The `@Insert` annotation is used to define an insert statement. In fact,<update... / > Element (or @Update annotation),<delete... / > (or @Delete annotation) and<insert... / > The usage of the element (or the @Insert annotation) is very similar. When using...<insert... / > ,<update... / > or<delete... / > Elements must also specify an id attribute, the value of which will serve as a unique identifier for the statements they define.
[0165] The `@Update` and `@Delete` annotations are typically used to define update and delete statements. They support the following attributes:<insert... / > The elements support the same attributes.
[0166] In one specific embodiment, various configuration database statements are written into the target general interface using a third annotation. This third annotation can be any one of the following: @Select, @Update, @delete, or @insert annotations, depending on the specific situation.
[0167] Furthermore, the return result of the target generic interface conforms to a preset data structure, which is a list where each element is a mapping. Maps store and access elements using key-value pairs. The preset data structure can be a List. <Map<String,Object> The `List` directive represents a list, and each `Map` represents a row of results. The keys of the `Map` correspond to the column names in the SQL query. This design allows the query results to adapt to all scenarios with two-dimensional table structures. <Map<String,Object> A List is a Java data structure that is a list where each element is a Map. In each Map, the key is a string, and the value can be any object. This data structure is typically used to store and process a set of related data records. For example, you can use a List... <Map<String,Object> The database query results are stored in a Map, where each Map represents a record, and the keys in the Map represent field names, while the values represent the values corresponding to those fields.
[0168] In a specific implementation, various configuration SQL statements are written in the target general interface using the @Select annotation. The code for the target general interface is as follows:
[0169] public interface BiQueryMapper{
[0170] @Select("select'bi'")
[0171] List <Map<String,Object> >queryData(@Param("query")Map<String,Object> (query).
[0172] The `@Select` annotation specifies the database statement corresponding to the Mapper interface. Query parameters are passed via a Map to flexibly handle various query conditions. The returned results are a List. <Map<String,Object> The format is as follows: each Map represents a row of results, and the keys of the Maps correspond to the column names in the SQL query. This design allows the query results to be adapted to all scenarios with two-dimensional table structures.
[0173] In this framework, the generic mapping declaration object can be a MappedStatement object, and the target source data can be a SqlSource object. The MappedStatement object encapsulates the mapped SQL, storing the statement identifier of the SQL statement to be mapped and the target source data. The MappedStatement class is one of the core classes of the MyBatis framework; it stores all the information corresponding to a SQL statement. MyBatis generates the MappedStatement object corresponding to the SQL statement by parsing the annotation information on the target generic interface. One SQL statement corresponds to one MappedStatement object. MyBatis describes...<select|update|insert|delete> Alternatively, SQL information configured using annotations such as @Select and @Update.
[0174] SqlSource is the core of the entire MappedStatement object. All the other fields in MappedStatement are defined to ensure its accurate execution. SqlSource is a semi-finished SQL statement because the dynamic tags within it haven't been staticated, and the parameters haven't been assigned values. SqlSource is a property of the MappedStatement object, representing a one-to-one relationship. The main purpose of creating SqlSource when creating a MappedStatement object is to create an SQL statement.
[0175] In this embodiment, the types of target source data include: dynamic source data (DynamicSqlSource), static source data (StaticSqlSource), and raw source data (RawSqlSource). The type of target source data can be determined by the attributes of different source data. For example, in the code of the MyBatch configuration file, if the body of tags such as select / update contains tag nodes, it can be considered dynamic source data; if it only contains "$", or only contains "$" and "#", it is considered static source data; if it only contains "#", it is considered raw source data.
[0176] The target source data can include at least one node label, which contains the variable to be manipulated and its corresponding expression. The target source data can contain at least one of the following node labels: trim, choose, foreach, and if. The node labels can be elements from a predefined tag set, such as if, choose, and foreach. Taking SqlSource as the target source data as an example, the SqlSource code contains sqlnode, which is the node label. sqlnode can be predefined; for example, if and foreach in SqlSource can be predefined as sqlnode. This allows for accurate location of the node labels during configuration file parsing, extracting the N node labels contained in the target source data. Furthermore, by iterating through each node label, the variable and its expression for each node label are obtained.
[0177] Different database statements correspond to different mapping declaration objects. These mapping declaration objects are constructed based on the database statements. In the MyBatis framework, each Mapper method corresponds to a MappedStatement object, which contains complete SQL information, input parameters, and output parameter definitions. For example, database statement A corresponds to MappedStatement object A, and database statement B corresponds to MappedStatement object B. Database statements and their corresponding mapping declaration objects are bound together by an identifier.
[0178] 203. Dynamically generate a second database statement based on the target mapping declaration object.
[0179] In this embodiment, the target mapping declaration object may include the target data source, statement identifier, and parameter information. The target mapping declaration object can be a MappedStatement object, and the target data source can be a SqlSource object.
[0180] In this embodiment of the application, dynamically generating a second database statement based on a target mapping declaration object may include:
[0181] (1) Obtain the target source data encapsulated in the target mapping declaration object.
[0182] The target source data includes at least one node label, which contains the variable to be operated on and the corresponding variable expression.
[0183] (2) Dynamically generate second database statements based on target source data.
[0184] Specifically, you can locate the node labels line by line in the target source data encapsulated by the target mapping declaration object, and then determine the variables and their expressions under each node label. For example, by examining the target source data line by line, first locate the WHERE node label and obtain the variables under it, namely dimType, startDate, and endDate. Next, locate the foreach node label and obtain the variable biz_code from the list array. Then, locate the if node label and obtain the variable offset. In this way, you can obtain all the variables contained in the target source data.
[0185] Furthermore, a variable node is constructed for each variable. In MyBatis, "#{}" is handled by treating all passed parameters as a single string. For variables whose expressions contain "#{}", a mapping type node is constructed. Since the list in the foreach loop is an array, an array type node is created for list, and a mapping type node is created for biz_code. Next, an assignment node is constructed for each variable and values are assigned. In the example above, assignment nodes need to be created for the variables dimType, startDate, endDate, biz_code, and offset. When assigning values to assignment nodes, the variable value can be obtained based on the expression corresponding to each variable. For example, the variable dimType is passed a string as its value via "#{}". The variable value can be selected from a pre-defined database. For example, if the string AA is retrieved from the pre-defined database and AA meets the condition of the variable dimType, then AA is used as the value of dimType. After obtaining the variable value, it is added to the assignment node corresponding to dimType. In this way, each variable node and its corresponding assignment node can be regarded as a set of variable mappings. By associating the variable nodes in each set of variable mappings with the root node, we can obtain the variable mapping hierarchy tree.
[0186] After obtaining the variable mapping hierarchy tree, a complete database statement is generated based on it. Specifically, a target data table is generated based on the variable mapping hierarchy tree. This target data table stores each variable in the target source data and its corresponding value in key-value pairs. A second database statement is then generated based on this target data table. After constructing the variable mapping hierarchy tree, the values in the assignment nodes are uploaded layer by layer, forming the target data table at the root node. This target data table stores each variable and its value in key-value pairs. By constructing the variable mapping hierarchy tree, the target data table required for building the second database statement can be quickly obtained from the root node, improving the efficiency of database statement generation.
[0187] In this embodiment of the application, the different mapping declaration objects corresponding to each database statement can be pre-configured one by one.
[0188] To improve the configuration efficiency of different mapping declaration objects corresponding to various database statements, the data access methods also include:
[0189] (1) Obtain the target source data of the pre-configured configuration database statement.
[0190] In this embodiment of the application, the pre-configured configuration database statement is pre-configured and input by the user, and the target source data of the configuration database statement can be created based on the configuration database statement using the createSqlSource method.
[0191] (2) Obtain the annotation information of the target general interface.
[0192] The annotation information for the target general interface can be @Select, @Update, @delete, or @insert.
[0193] (3) Parse the annotation information of the target general interface to obtain the general mapping declaration object.
[0194] (4) Replace the target source data and statement identifier in the general mapping declaration object with the target source data and statement identifier of the configuration database statement to obtain the mapping declaration object corresponding to the configuration database statement.
[0195] In this embodiment, the target source data and statement identifier of the configuration database statement are replaced with the target source data and statement identifier in the general mapping declaration object to obtain the mapping declaration object corresponding to the configuration database statement. Since the SqlSource object is the core of the entire MappedStatement object, and the parameter information and other fields of the MappedStatement object are defined for accurate execution, they can be reused. After generating the general mapping declaration object using a target general interface, the parameter information defined in the general mapping declaration object for accurate execution of the target source data is reused. Only the SqlSource object and statement identifier need to be replaced, enabling the rapid generation of mapping declaration objects for various configuration database statements.
[0196] Furthermore, by replacing the target source data and statement identifier in the general mapping declaration object with the target source data and statement identifier based on the configuration database statement, the mapping declaration object corresponding to the configuration database statement is obtained. The mapping declaration objects corresponding to each configuration database statement are then cached in a local cache database or internal storage. The local cache database can be a Redis database, an SQLite database, etc., depending on the specific situation. SQLite is a lightweight, ACID-compliant relational database management system contained in a relatively small C library. It is a public domain project created by D. Richard Hipp. Designed for embedded systems, it is already used in many embedded products. It has very low resource consumption; in embedded devices, it may only require a few hundred kilobytes of memory. It supports mainstream operating systems such as Windows, Linux, and Unix, and can be integrated with many programming languages, such as C#, PHP, and Java, and also has an ODBC interface. Redis (Remote Dictionary Server) is an open-source in-memory database that provides a high-performance key-value store system, commonly used in caching, message queues, session storage, and other application scenarios. Redis is renowned for its extremely high performance, supporting hundreds of thousands of read and write operations per second. This makes Redis an ideal choice for handling high-concurrency requests, especially in scenarios requiring fast response times, such as caching, session management, and leaderboards. Redis not only supports basic key-value storage but also provides a rich set of data types, including strings, lists, sets, hash tables, and sorted sets. These data types offer developers flexible data manipulation capabilities, allowing Redis to adapt to various application scenarios. Redis supports data persistence, saving data in memory to disk for recovery after a system restart. This provides Redis with data security, ensuring that data is not lost due to system failures.
[0197] Furthermore, when executing the first database statement, the target interceptor is invoked to intercept the execution of the first database statement, and a target mapping declaration object corresponding to the statement identifier is searched locally based on the statement identifier of the first database statement. Specifically, the target mapping declaration object corresponding to the statement identifier is searched locally in the cached database based on the statement identifier of the first database statement.
[0198] Furthermore, to reduce the amount of data stored in the local cache database, in one specific embodiment, the target source data and statement identifier in the general mapping declaration object are replaced based on the target source data and statement identifier of the configuration database statement to obtain the mapping declaration object corresponding to the configuration database statement. Then, the mapping declaration objects corresponding to each configuration database statement are cached in a first local cache database. When the amount of mapping declaration objects cached in the first local cache database exceeds a preset value, a portion of the cached mapping declaration objects are transferred to a second local cache database. The read / write speed of the first local cache database is greater than that of the second local cache database, and the maximum data storage capacity of the first local cache database is less than that of the second local cache database. When the statement identifier of the first database statement is obtained, the first local cache database is first searched for the target mapping declaration object corresponding to the statement identifier. If the first local cache database does not contain a target mapping declaration object corresponding to the statement identifier, then the second local cache database is searched for the same target mapping declaration object. By using the first local cache database with its faster read / write speed and the second local cache database with its larger storage capacity in conjunction with each other to store data, the speed of finding the target mapping declaration object can be improved.
[0199] Furthermore, to further improve the speed of finding target mapping declaration objects, the cache time of each mapping declaration object in the first local cache database is obtained. The transfer priority of each mapping declaration object is determined based on its cache time, with a higher transfer priority for a longer cache time. When the amount of mapping declaration objects cached in the first local cache database exceeds a preset value, the mapping declaration object with the highest transfer priority is transferred from the first local cache database to the second local cache database.
[0200] Furthermore, to further improve the speed of finding target mapping declaration objects, the cache time of each mapping declaration object in the first local cache database and the number of times each mapping declaration object is used are obtained. Each modification statement using a mapping declaration object is recorded as one usage count. The transfer priority of each mapping declaration object is determined based on its cache time and usage count; the shorter the cache time and the shorter the cache time of usage counts, the higher the transfer priority. When the amount of mapping declaration objects cached in the first local cache database exceeds a preset value, the mapping declaration object with the highest transfer priority is transferred from the first local cache database to the second local cache database.
[0201] 204. Execute the second database statement to obtain the data access results.
[0202] In this embodiment of the application, the target interceptor is invoked to execute the second database statement and perform a database query to obtain the data access result.
[0203] The data access results can be the results corresponding to the data access request. For example, if the data access request is "Query the target user's daily spending last week", the data access results could be "Monday, 1000 yuan; Tuesday, 2000 yuan; Wednesday, 2000 yuan; Thursday, 5000 yuan; Friday, 9000 yuan; Saturday, 2000 yuan; Sunday, 2000 yuan". The data access results are presented as a List. <Map<String,Object> The preset data structure is returned.
[0204] Furthermore, to achieve efficient management and real-time updates of database statements, in a specific embodiment, obtaining the target source data of pre-configured database statements may include:
[0205] (1) Obtain pre-configured configuration database statements from the remote configuration system and cache them locally.
[0206] In this embodiment of the application, the remote configuration system is independent of the server 110 that executes the data access method. Developers can pre-configure database statements in the remote configuration information to obtain various configuration database statements.
[0207] In one specific embodiment, pre-configured configuration database statements are obtained from a remote configuration system and cached in a local cache database or internal storage.
[0208] Furthermore, to ensure timely updates to the configuration database statements stored locally, the configuration database statements stored in the remote configuration system are cached to local storage at preset intervals. These preset intervals can be 30 seconds, 100 seconds, etc., and can be set according to specific needs.
[0209] Furthermore, when the remote configuration system detects a new configuration database statement, it sends a notification to the server. Upon receiving the notification, the server caches the new configuration database statement locally.
[0210] (2) Construct the target source data corresponding to the configuration database statement based on the configuration database statement.
[0211] In this embodiment of the application, the pre-configured configuration database statement is pre-configured and input by the user, and the target source data of the configuration database statement can be created based on the configuration database statement using the createSqlSource method.
[0212] This application aims to achieve efficient management and real-time updates of database statements. It separates database statement management from application code and migrates it to a centralized remote configuration system. This allows developers to maintain database statements in the remote configuration system without directly modifying the application code, without affecting the execution of local database statements.
[0213] Furthermore, to facilitate users in updating and maintaining configuration database statements in real time, data access methods include:
[0214] (1) When the statement configuration instruction is obtained, the input configuration database statement and the corresponding statement identifier are obtained by using the remote configuration system.
[0215] In one specific embodiment, upon receiving a statement configuration instruction, a statement configuration interface is popped up; the configuration database statement and corresponding statement identifier entered by the user in the statement configuration interface are obtained using a remote configuration system.
[0216] like Figure 3 As shown, the statement configuration interface includes input boxes for entering configuration database statements and their corresponding statement identifiers. The input box for entering statement identifiers can be identified using a Key, and the input box for entering configuration database statements can be identified using a Value.
[0217] Furthermore, the statement configuration interface also includes configuring the grouping and type of the database statements. Database statements can be grouped according to their characteristics, and different groups can be defined. It also provides options for configuring the SQL type, which developers can select based on the type of the database statement. The database statement type can be SRING, TEXT, NUMBER, JSON, or XML.
[0218] (2) Cache the configuration database statements and corresponding statement identifiers obtained by the remote configuration system to the local machine.
[0219] Specifically, the configuration database statements and corresponding statement identifiers obtained by the remote configuration system are cached in a local cache database or internal storage.
[0220] Please refer to Figure 4 , Figure 4 This is a schematic flowchart of another embodiment of the data access method provided in this application, as shown below. Figure 4 As shown, the data access method provided in this application follows this process:
[0221] 301. When a statement configuration instruction is obtained, use the remote configuration system to obtain the input configuration database statement and the corresponding statement identifier.
[0222] In this embodiment of the application, the remote configuration system is independent of the server that executes the data access method. Developers can pre-configure database statements in the remote configuration information and obtain each configuration database statement and its corresponding statement identifier.
[0223] In one specific embodiment, upon receiving a statement configuration instruction, a statement configuration interface pops up; the remote configuration system is used to obtain the configuration database statement and its corresponding statement identifier entered by the user in the statement configuration interface. The statement identifier can be a string of specific characters used to distinguish different database statements.
[0224] like Figure 3 As shown, the statement configuration interface includes input boxes for entering configuration database statements and their corresponding statement identifiers. The input box for entering statement identifiers can be identified using a Key, and the input box for entering configuration database statements can be identified using a Value.
[0225] Furthermore, the statement configuration interface also includes configuring the grouping and type of the database statements. Database statements can be grouped according to their characteristics, and different groups can be defined. It also provides options for configuring the SQL type, which developers can select based on the type of the database statement. The database statement type can be SRING, TEXT, NUMBER, JSON, or XML.
[0226] 302. Cache the configuration database statements and corresponding statement identifiers obtained from the remote configuration system to the local machine.
[0227] Specifically, the configuration database statements and corresponding statement identifiers obtained by the remote configuration system are cached in a local cache database or internal storage.
[0228] In one specific embodiment, pre-configured configuration database statements are obtained from a remote configuration system and cached in a local cache database or internal storage.
[0229] Furthermore, to ensure timely updates to the configuration database statements stored locally, the configuration database statements stored in the remote configuration system are cached to local storage at preset intervals. These preset intervals can be 30 seconds, 100 seconds, etc., and can be set according to specific needs.
[0230] 303. Create a target general interface and write various configuration database statements in the target general interface using third annotations.
[0231] The target generic interface is a predefined generic interface. For example, the target generic interface is the generic Mapper interface predefined by the MyBatis framework. In MyBatis, a Mapper interface is an interface that defines database operation methods. These methods typically correspond to database statements and are used to perform CRUD operations on the database. The methods in the Mapper interface can be associated with the actual database statements through MyBatis' mapping configuration file. The purpose of the Mapper interface is to decouple the logic of database operations from the actual database statements, making the code in the data access layer clearer and more maintainable. By defining a Mapper interface, these methods can be called directly in Java code to perform database operations without having to write the database statements directly.
[0232] In this embodiment, a target general interface is created, and various configuration database statements are written into the target general interface using a third annotation. The third annotation can be any one of the @Select, @Update, @delete, or @insert annotations, and can be set according to specific circumstances.
[0233] Furthermore, the return result of the target generic interface conforms to a preset data structure, which is a list where each element is a mapping. Maps store and access elements using key-value pairs. The preset data structure can be a List. <Map<String,Object> The `List` directive represents a list, and each `Map` represents a row of results. The keys of the `Map` correspond to the column names in the SQL query. This design allows the query results to adapt to all scenarios with two-dimensional table structures. <Map<String,Object> A List is a Java data structure that is a list where each element is a Map. In each Map, the key is a string, and the value can be any object. This data structure is typically used to store and process a set of related data records. For example, you can use a List... <Map<String,Object> The database query results are stored in a Map, where each Map represents a record, and the keys in the Map represent field names, while the values represent the values corresponding to those fields.
[0234] 304. Construct the target source data corresponding to the configuration database statement based on the configuration database statement.
[0235] The target source data includes at least one node label, which contains the variable to be manipulated and its corresponding expression. The target source data includes at least one of the following node labels: trim, choose, foreach, and if. The node labels can be elements from a predefined tag set, such as if, choose, and foreach. Taking SqlSource as the target source data as an example, the SqlSource code contains sqlnode, which is the node label. sqlnode can be predefined; for example, if and foreach in SqlSource can be predefined as sqlnode. This allows for accurate location of the node labels during configuration file parsing, extracting the N node labels contained in the target source data. Furthermore, by iterating through each node label, the variable and its expression for each node label are obtained.
[0236] In this embodiment of the application, the pre-configured configuration database statement is pre-configured and input by the user, and the target source data of the configuration database statement can be created based on the configuration database statement using the createSqlSource method.
[0237] This application aims to achieve efficient management and real-time updates of database statements. It separates database statement management from application code and migrates it to a centralized remote configuration system. This allows developers to maintain database statements in the remote configuration system without directly modifying the application code, without affecting the execution of local database statements.
[0238] 305. Parse the annotation information of the target general interface to obtain the general mapping declaration object.
[0239] The annotation information for the target general interface can be @Select, @Update, @delete, or @insert.
[0240] 306. Replace the target source data and statement identifier in the general mapping declaration object with the target source data and statement identifier of the configuration database statement to obtain the mapping declaration object corresponding to the configuration database statement.
[0241] The mapping declaration object includes the statement identifier of the database statement to be mapped, the target source data, and parameter information. Different database statements correspond to different mapping declaration objects.
[0242] In this framework, the generic mapping declaration object can be a MappedStatement object, and the target source data can be a SqlSource object. The MappedStatement object encapsulates the mapped SQL, storing the statement identifier of the database statement to be mapped and the target source data. The MappedStatement class is one of the core classes of the MyBatis framework; it stores all the information corresponding to a database statement. MyBatis generates the MappedStatement object corresponding to the database statement by parsing the annotation information on the target generic interface. One database statement corresponds to one MappedStatement object. MyBatis describes...<select|update|insert|delete> Alternatively, SQL information configured using annotations such as @Select and @Update.
[0243] In this embodiment, the target source data and statement identifier of the configuration database statement are replaced with the target source data and statement identifier in the general mapping declaration object to obtain the mapping declaration object corresponding to the configuration database statement. Since the SqlSource object is the core of the entire MappedStatement object, and the parameter information and other fields of the MappedStatement object are defined for accurate execution, they can be reused. After generating the general mapping declaration object using a target general interface, the parameter information defined in the general mapping declaration object for accurate execution of the target source data is reused. Only the SqlSource object and statement identifier need to be replaced, enabling the rapid generation of mapping declaration objects for various configuration database statements.
[0244] Different database statements correspond to different mapping declaration objects. These mapping declaration objects are constructed based on the database statements. In the MyBatis framework, each Mapper method corresponds to a MappedStatement object, which contains complete SQL information, input parameters, and output parameter definitions. For example, database statement A corresponds to MappedStatement object A, and database statement B corresponds to MappedStatement object B. Database statements and their corresponding mapping declaration objects are bound together by an identifier.
[0245] 307. Cache each configuration database statement and its corresponding mapping declaration object locally.
[0246] In this embodiment, the target source data and statement identifier in the general mapping declaration object are replaced with the target source data and statement identifier based on the configuration database statement to obtain the mapping declaration object corresponding to the configuration database statement. The mapping declaration objects corresponding to each configuration database statement are then cached in a local cache database or internal storage. The local cache database can be a Redis database, an SQLite database, etc., depending on the specific situation.
[0247] Furthermore, by replacing the target source data and statement identifier in the general mapping declaration object with the target source data and statement identifier based on the configuration database statement, the mapping declaration object corresponding to the configuration database statement is obtained. Then, the mapping declaration objects corresponding to each configuration database statement are cached in a first local cache database. When the amount of mapping declaration objects cached in the first local cache database exceeds a preset value, a portion of the cached mapping declaration objects are transferred to a second local cache database. The read / write speed of the first local cache database is greater than that of the second local cache database, and the maximum data storage capacity of the first local cache database is less than that of the second local cache database. When the statement identifier of the first database statement is obtained, the first local cache database is first searched for the target mapping declaration object corresponding to the statement identifier. If the first local cache database does not contain a target mapping declaration object corresponding to the statement identifier, then the second local cache database is searched for the same target mapping declaration object. By using the first local cache database with its faster read / write speed and the second local cache database with its larger storage capacity in conjunction with each other, the speed of finding the target mapping declaration object can be improved.
[0248] To further improve the speed of finding target mapping declaration objects, the cache time of each mapping declaration object in the first local cache database is obtained. The transfer priority of each mapping declaration object is determined based on its cache time; the longer the cache time, the higher the transfer priority. When the amount of mapping declaration objects cached in the first local cache database exceeds a preset value, the mapping declaration object with the highest transfer priority is transferred from the first local cache database to the second local cache database.
[0249] Furthermore, to further improve the speed of finding target mapping declaration objects, the cache time of each mapping declaration object in the first local cache database and the number of times each mapping declaration object is used are obtained. Each modification statement using a mapping declaration object is recorded as one usage count. The transfer priority of each mapping declaration object is determined based on its cache time and usage count; the shorter the cache time and the shorter the cache time of usage counts, the higher the transfer priority. When the amount of mapping declaration objects cached in the first local cache database exceeds a preset value, the mapping declaration object with the highest transfer priority is transferred from the first local cache database to the second local cache database.
[0250] 308. Create a target interceptor.
[0251] In this embodiment, the target interceptor is an executor interceptor. The executor interceptor is used to intercept the execution of MyBatis executor methods, thereby intercepting the execution of database statements. Executor interceptors allow for the interception and customization of the MyBatis executor's behavior. For example, caching, logging, or auditing functionality can be added to the executor. These interceptors can extend or modify their behavior at different stages of MyBatis execution. You can implement these interceptors by implementing the corresponding interfaces provided by MyBatis and configuring them in the MyBatis configuration file.
[0252] Specifically, define an interceptor class that implements the org.apache.ibatis.plugin.Interceptor interface, and override the intercept, plugin, and setProperties methods to create the target interceptor.
[0253] 309. Add annotation information to the target interceptor.
[0254] The annotation information for the target interceptor can be various annotations, such as @Intercepts, @Signature, @Slf4j, and @Override, etc., with the interface set according to specific circumstances. This annotation information is input by the developers.
[0255] In this embodiment of the application, adding annotation information to the target interceptor includes: identifying the target interceptor through a first annotation; and specifying the type and method of the target interceptor through a second annotation. The first annotation is the @Intercepts annotation, and the second annotation is the @Signature annotation.
[0256] 310. Upon receiving a data access request, call the target general interface to parse the data access request and obtain the first database statement.
[0257] In this embodiment, the data access request is a user-input data access request. The user can trigger the client to send a data access request to the server by clicking a query control on the client's display page. The server then receives the data access request.
[0258] Data access requests can be data query requests, data insertion requests, data deletion requests, data update requests, etc., depending on the specific settings. For example, a data access request can be a data query request, which may include the target user's user identifier and business query information. The user identifier may include, but is not limited to, a user name, user code, or user login account. For instance, the business query information could be: "Query the target user's daily spending for the past week."
[0259] Different data access requests correspond to different database statements. The target general interface parses and maps different data access requests to different database statements. For example, data access request A corresponds to database statement A, data access request B corresponds to database statement B, and data access request C corresponds to database statement C. When data access request A is obtained, the target general interface is called to parse data access request A and map it to database statement A, thus obtaining the first database statement. The mapping relationship between data access requests and database statements can be configured according to specific circumstances, and this application does not impose any limitations on it.
[0260] 311. Execute the first database statement.
[0261] Specifically, the Executor interface in the MyBatis framework is called to execute the first database statement.
[0262] 312. When executing the first database statement, call the target interceptor to intercept the execution of the first database statement, and determine the corresponding target mapping declaration object based on the statement identifier of the first database statement.
[0263] In this embodiment, after the target general interface is called to parse the data access request and obtain the first database statement, the first database statement is executed. When the first database statement is executed, a target interceptor is called to intercept its execution, and the corresponding target mapping declaration object is determined based on the statement identifier of the first database statement.
[0264] Furthermore, when executing the first database statement, the target interceptor is invoked to intercept the execution of the first database statement, and a target mapping declaration object corresponding to the statement identifier is searched locally based on the statement identifier of the first database statement. Specifically, the target mapping declaration object corresponding to the statement identifier is searched locally in the cached database based on the statement identifier of the first database statement.
[0265] 313. Obtain the target source data encapsulated in the target mapping declaration object.
[0266] In this embodiment, the target mapping declaration object includes the target data source, statement identifier, and parameter information. Specifically, the target mapping declaration object is a MappedStatement object, and the target data source is a SqlSource object.
[0267] The `SqlSource` object is used to describe SQL information configured through annotations such as `@Select`, `@Insert`, `@Delete`, `@Update`, `@SelectProvider`, `@InsertProvider`, `@DeleteProvider`, and `@UpdateProvider`, or SQL information configured through an XML configuration file. The `SqlSource` interface further defines the `BoundSqlgetBoundSql` method to obtain a `BoundSql` instance that encapsulates the SQL information, resulting in dynamically generated database statements.
[0268] The target source data includes at least one node label, which contains the variable to be operated on and the corresponding variable expression.
[0269] 314. Dynamically generate second database statements based on target source data.
[0270] Specifically, you can locate the node labels line by line in the target source data encapsulated by the target mapping declaration object, and then determine the variables and variable expressions under that node label.
[0271] Furthermore, a variable node is constructed for each variable. In MyBatis, "#{}" is handled by treating all passed parameters as a single string. For variables whose expressions contain "#{}", a mapping type node is constructed. Since the list in the foreach loop is an array, an array type node is created for list, and a mapping type node is created for biz_code. Next, an assignment node is constructed for each variable and values are assigned. In the example above, assignment nodes need to be created for the variables dimType, startDate, endDate, biz_code, and offset. When assigning values to assignment nodes, the variable value can be obtained based on the expression corresponding to each variable. For example, the variable dimType is passed a string as its value via "#{}". The variable value can be selected from a pre-defined database. For example, if the string AA is retrieved from the pre-defined database and AA meets the condition of the variable dimType, then AA is used as the value of dimType. After obtaining the variable value, it is added to the assignment node corresponding to dimType. In this way, each variable node and its corresponding assignment node can be regarded as a set of variable mappings. By associating the variable nodes in each set of variable mappings with the root node, we can obtain the variable mapping hierarchy tree.
[0272] After obtaining the variable mapping hierarchy tree, a complete database statement is generated based on the tree. Specifically, a target data table is generated based on the variable mapping hierarchy tree. The target data table stores each variable contained in the target source data and its corresponding value in key-value pairs. A second database statement is then generated based on the target data table.
[0273] 315. Execute the second database statement to obtain the data access results.
[0274] In this embodiment of the application, the target interceptor is invoked to execute the second database statement and perform a database query to obtain the data access result.
[0275] The data access results are the results corresponding to the data access request. For example, if the data access request is "Query the target user's daily spending last week", the data access results would be "Monday, 1000 yuan; Tuesday, 2000 yuan; Wednesday, 2000 yuan; Thursday, 5000 yuan; Friday, 9000 yuan; Saturday, 2000 yuan; Sunday, 2000 yuan". The data access results are presented as a List. <Map<String,Object> The preset data structure is returned.
[0276] Please refer to Figure 5 , Figure 5 This is a schematic flowchart of another embodiment of the data access method provided in this application, as shown below. Figure 5 As shown, the data access method provided in this application follows this process:
[0277] 401. Obtain the input configuration database statement and its corresponding statement identifier.
[0278] In this embodiment of the application, the remote configuration system is independent of the server that executes the data access method. Developers can pre-configure database statements in the remote configuration information, input the configuration database statements and the corresponding statement identifiers, and the remote configuration system obtains each configuration database statement and the corresponding statement identifier.
[0279] In one specific embodiment, upon receiving a statement configuration instruction, a statement configuration interface pops up; the remote configuration system is used to obtain the configuration database statement and its corresponding statement identifier entered by the user in the statement configuration interface. The statement identifier can be a string of specific characters used to distinguish different database statements.
[0280] 402. Cache the configuration database statements and corresponding statement identifiers obtained from the remote configuration system to the local machine.
[0281] Specifically, electronic devices cache the configuration database statements and corresponding statement identifiers obtained by the remote configuration system to a local cache database or internal storage.
[0282] In one specific embodiment, pre-configured configuration database statements are obtained from a remote configuration system and cached in a local cache database or internal storage.
[0283] Furthermore, to ensure timely updates to the configuration database statements stored locally, the configuration database statements stored in the remote configuration system are cached to local storage at preset intervals. These preset intervals can be 40 seconds, 100 seconds, etc., and can be set according to specific needs.
[0284] 403. Create a target general interface and write various configuration database statements in the target general interface using third annotations.
[0285] The target generic interface is a predefined generic interface. For example, the target generic interface is the generic Mapper interface predefined by the MyBatis framework.
[0286] In this embodiment, the developer inputs annotation information of the target general interface into the electronic device. The electronic device creates the target general interface based on the annotation information of the target general interface and writes various configuration database statements into the target general interface through a third annotation.
[0287] In this embodiment, a target general interface is created, and various configuration database statements are written into the target general interface using a third annotation. The third annotation can be any one of the @Select, @Update, @delete, or @insert annotations, and can be set according to specific circumstances.
[0288] Furthermore, the return result of the target generic interface conforms to a preset data structure, which is a list where each element is a mapping. Maps store and access elements using key-value pairs. The preset data structure can be a List. <Map<String,Object> The `List` directive represents a list, and each `Map` represents a row of results. The keys of the `Map` correspond to the column names in the SQL query. This design allows the query results to adapt to all scenarios with two-dimensional table structures. <Map<String,Object> A List is a Java data structure that is a list where each element is a Map. In each Map, the key is a string, and the value can be any object. This data structure is typically used to store and process a set of related data records. For example, you can use a List... <Map<String,Object> The database query results are stored in a Map, where each Map represents a record, and the keys in the Map represent field names, while the values represent the values corresponding to those fields.
[0289] 404. Construct the target source data corresponding to the configuration database statement based on the configuration database statement.
[0290] The target source data includes at least one node label, which contains the variable to be manipulated and its corresponding expression. The target source data includes at least one of the following node labels: trim, choose, foreach, and if. The node labels can be elements from a predefined tag set, such as if, choose, and foreach. Taking SqlSource as the target source data as an example, the SqlSource code contains sqlnode, which is the node label. sqlnode can be predefined; for example, if and foreach in SqlSource can be predefined as sqlnode. This allows for accurate location of the node labels during configuration file parsing, extracting the N node labels contained in the target source data. Furthermore, by iterating through each node label, the variable and its expression for each node label are obtained.
[0291] In this embodiment of the application, the pre-configured configuration database statement is pre-configured and input by the user, and the target source data of the configuration database statement can be created based on the configuration database statement using the createSqlSource method.
[0292] This application aims to achieve efficient management and real-time updates of database statements. It separates database statement management from application code and migrates it to a centralized remote configuration system. This allows developers to maintain database statements in the remote configuration system without directly modifying the application code, without affecting the execution of local database statements.
[0293] 405. Parse the annotation information of the target general interface to obtain the general mapping declaration object.
[0294] The annotation information for the target general interface can be @Select, @Update, @delete, or @insert.
[0295] 406. Replace the target source data and statement identifier in the general mapping declaration object with the target source data and statement identifier of the configuration database statement to obtain the mapping declaration object corresponding to the configuration database statement.
[0296] The mapping declaration object includes the statement identifier of the database statement to be mapped, the target source data, and parameter information. Different database statements correspond to different mapping declaration objects.
[0297] The general mapping declaration object can be a MappedStatement object, and the target source data can be a SqlSource object.
[0298] In this embodiment, the target source data and statement identifier of the configuration database statement are replaced with the target source data and statement identifier in the general mapping declaration object to obtain the mapping declaration object corresponding to the configuration database statement. Since the SqlSource object is the core of the entire MappedStatement object, and the parameter information and other fields of the MappedStatement object are defined for accurate execution, they can be reused. After generating the general mapping declaration object using a target general interface, the parameter information defined in the general mapping declaration object for accurate execution of the target source data is reused. Only the SqlSource object and statement identifier need to be replaced, enabling the rapid generation of mapping declaration objects for various configuration database statements.
[0299] Different database statements correspond to different mapping declaration objects. These mapping declaration objects are constructed based on the database statements. In the MyBatis framework, each Mapper method corresponds to a MappedStatement object, which contains complete SQL information, input parameters, and output parameter definitions. For example, database statement A corresponds to MappedStatement object A, and database statement B corresponds to MappedStatement object B. Database statements and their corresponding mapping declaration objects are bound together by an identifier.
[0300] 407. Cache each configuration database statement and its corresponding mapping declaration object locally.
[0301] In this embodiment, the target source data and statement identifier in the general mapping declaration object are replaced with the target source data and statement identifier based on the configuration database statement to obtain the mapping declaration object corresponding to the configuration database statement. The mapping declaration objects corresponding to each configuration database statement are then cached in a local cache database or internal storage. The local cache database can be a Redis database, an SQLite database, etc., depending on the specific situation.
[0302] 408. Create a target interceptor and add annotation information to the target interceptor.
[0303] In this embodiment, the target interceptor is an executor interceptor. The executor interceptor is used to intercept the execution of MyBatis executor methods, thereby intercepting the execution of database statements.
[0304] In this embodiment of the application, the developer inputs the annotation information of the target interceptor into the electronic device, and the electronic device creates the target interceptor and adds annotation information to the target interceptor based on the annotation information of the target interceptor.
[0305] Specifically, define an interceptor class that implements the org.apache.ibatis.plugin.Interceptor interface, and override the intercept, plugin, and setProperties methods to create the target interceptor.
[0306] The annotation information for the target interceptor can be various annotations, such as @Intercepts, @Signature, @Slf4j, and @Override, etc., with the interface set according to specific circumstances. This annotation information is input by the developers.
[0307] In this embodiment of the application, adding annotation information to the target interceptor includes: identifying the target interceptor through a first annotation; and specifying the type and method of the target interceptor through a second annotation. The first annotation is the @Intercepts annotation, and the second annotation is the @Signature annotation.
[0308] 409. Upon receiving a data access request, call the target general interface to parse the data access request and obtain the first database statement.
[0309] In this embodiment, the data access request is a user-input data access request. The user can trigger the client to send a data access request to the server by clicking a query control on the client's display page. The server then receives the data access request.
[0310] Data access requests can be data query requests, data insertion requests, data deletion requests, data update requests, etc., depending on the specific settings. For example, a data access request can be a data query request, which may include the target user's user identifier and business query information. The user identifier may include, but is not limited to, a user name, user code, or user login account. For instance, the business query information could be: "Query the target user's daily spending for the past week."
[0311] Different data access requests correspond to different database statements. The target general interface parses and maps different data access requests to different database statements. For example, data access request A corresponds to database statement A, data access request B corresponds to database statement B, and data access request C corresponds to database statement C. When data access request A is obtained, the target general interface is called to parse data access request A and map it to database statement A, thus obtaining the first database statement. The mapping relationship between data access requests and database statements can be configured according to specific circumstances, and this application does not impose any limitations on it.
[0312] 410. Execute the first database statement.
[0313] Specifically, the Executor interface in the MyBatis framework is called to execute the first database statement.
[0314] 411. When executing the first database statement, call the target interceptor to intercept the execution of the first database statement, and determine the corresponding target mapping declaration object based on the statement identifier of the first database statement.
[0315] In this embodiment, after the target general interface is called to parse the data access request and obtain the first database statement, the first database statement is executed. When the first database statement is executed, a target interceptor is called to intercept its execution, and the corresponding target mapping declaration object is determined based on the statement identifier of the first database statement.
[0316] Furthermore, when executing the first database statement, the target interceptor is invoked to intercept the execution of the first database statement, and a target mapping declaration object corresponding to the statement identifier is searched locally based on the statement identifier of the first database statement. Specifically, the target mapping declaration object corresponding to the statement identifier is searched locally in the cached database based on the statement identifier of the first database statement.
[0317] 412. Obtain the target source data encapsulated in the target mapping declaration object.
[0318] In this embodiment, the target mapping declaration object includes the target data source, statement identifier, and parameter information. Specifically, the target mapping declaration object is a MappedStatement object, and the target data source is a SqlSource object.
[0319] The target source data includes at least one node label, which contains the variable to be operated on and the corresponding variable expression.
[0320] 413. Dynamically generate second database statements based on target source data.
[0321] Specifically, you can locate the node labels line by line in the target source data encapsulated by the target mapping declaration object, and then determine the variables and variable expressions under that node label.
[0322] Furthermore, a variable node and an assignment node are constructed for each variable, and values are assigned to them. In the example above, assignment nodes need to be created for the variables dimType, startDate, endDate, biz_code, and offset. When assigning values to the assignment nodes, the variable values can be obtained based on their respective expressions. In this way, each variable node and its corresponding assignment node can be regarded as a set of variable mappings. By associating the variable nodes in each set of variable mappings with the root node, a variable mapping hierarchy tree can be obtained. After obtaining the variable mapping hierarchy tree, a complete database statement is generated based on the variable mapping hierarchy tree. Specifically, a target data table is generated based on the variable mapping hierarchy tree. The target data table stores each variable contained in the target source data and its corresponding variable value in key-value format; a second database statement is generated based on the target data table.
[0323] 414. Execute the second database statement to obtain the data access results.
[0324] In this embodiment of the application, the target interceptor is invoked to execute the second database statement and perform a database query to obtain the data access result.
[0325] The data access results are the results corresponding to the data access request. For example, if the data access request is "Query the target user's daily spending last week", the data access results would be "Monday, 1000 yuan; Tuesday, 2000 yuan; Wednesday, 2000 yuan; Thursday, 5000 yuan; Friday, 9000 yuan; Saturday, 2000 yuan; Sunday, 2000 yuan". The data access results are presented as a List. <Map<String,Object> The preset data structure is returned.
[0326] The data access method in this application embodiment is described below with reference to a specific application scenario.
[0327] (1) The remote configuration system on the server obtains the input configuration SQL statement and the corresponding statement identifier.
[0328] In this embodiment of the application, the remote configuration system is independent of the server that executes the data access method. Developers can pre-configure SQL statements in the remote configuration information, input the configuration SQL statements and the corresponding statement identifiers, and the remote configuration system obtains each configuration SQL statement and the corresponding statement identifier.
[0329] (2) Electronic devices cache the configuration SQL statements and corresponding statement identifiers obtained by the remote configuration system to the local device.
[0330] Specifically, electronic devices cache the configuration SQL statements and corresponding statement identifiers obtained from the remote configuration system to a local cache database or internal storage.
[0331] In one specific embodiment, pre-configured configuration SQL statements are obtained from a remote configuration system and cached in a local cache database or internal storage.
[0332] Furthermore, to ensure timely updates to the configuration SQL statements stored locally, the configuration SQL statements stored in the remote configuration system are cached to local storage at preset intervals. These preset intervals can be 40 seconds, 100 seconds, etc., and can be set according to specific needs.
[0333] (3) Electronic devices create a general Mapper interface and write various configuration SQL statements in the general Mapper interface through a third annotation.
[0334] In this embodiment of the application, the developer inputs annotation information of a general Mapper interface into the electronic device. The electronic device creates a general Mapper interface based on the annotation information of the general Mapper interface and writes various configuration SQL statements into the general Mapper interface through a third annotation.
[0335] In this embodiment, a generic Mapper interface is created, and various configuration SQL statements are written into the generic Mapper interface using a third annotation. The third annotation can be any one of the @Select, @Update, @delete, or @insert annotations, and can be set according to specific circumstances.
[0336] Furthermore, the return result of the general Mapper interface conforms to a preset data structure, which is a list where each element is a mapping. Maps store and access elements using key-value pairs. The preset data structure can be a List. <Map<String,Object> The `List` directive represents a list, and each `Map` represents a row of results. The keys of the `Map` correspond to the column names in the SQL query. This design allows the query results to adapt to all scenarios with two-dimensional table structures. <Map<String,Object> A List is a Java data structure that is a list where each element is a Map. In each Map, the key is a string, and the value can be any object. This data structure is typically used to store and process a set of related data records. For example, you can use a List... <Map<String,Object> The database query results are stored in a Map, where each Map represents a record, and the keys in the Map represent field names, while the values represent the values corresponding to those fields.
[0337] (4) Electronic devices construct the SqlSource corresponding to the configuration SQL statement based on the configuration SQL statement.
[0338] The `SqlSource` contains at least one node tag, which includes the variable to be manipulated and its corresponding expression. `SqlSource` includes at least one of the following node tags: `trim`, `choose`, `foreach`, and `if`. The node tags can be elements from a predefined tag set, such as `if`, `choose`, and `foreach`. Taking `SqlSource` as an example, the `SqlSource` code contains `sqlnode`, which is the node tag. `sqlnode` can be predefined; for example, `if` and `foreach` in `SqlSource` can be predefined as `sqlnode`. This allows for accurate location of the node tags during configuration file parsing, enabling the parsing of the N node tags contained in `SqlSource`. Furthermore, by iterating through each node tag, the variable and its expression for each node tag are obtained.
[0339] In this embodiment of the application, the pre-configured configuration SQL statement is pre-configured and input by the user, and the SqlSource of the configuration SQL statement can be created based on the configuration SQL statement using the createSqlSource method.
[0340] This application aims to achieve efficient management and real-time updates of SQL statements. It separates SQL statement management from application code and migrates it to a centralized remote configuration system. This allows developers to maintain SQL statements in the remote configuration system without directly modifying the application code, without affecting the execution of local SQL statements.
[0341] (5) The electronic device parses the annotation information of the general Mapper interface to obtain the general MappedStatement.
[0342] The annotation information for the general Mapper interface can be @Select, @Update, @delete, and @insert.
[0343] (6) The electronic device replaces the SqlSource and statement identifier in the general MappedStatement with the SqlSource and statement identifier of the configuration SQL statement to obtain the MappedStatement corresponding to the configuration SQL statement.
[0344] The MappedStatement includes the statement identifier of the SQL statement to be mapped, the SqlSource, and parameter information. Different SQL statements correspond to different MappedStatements.
[0345] In this embodiment of the application, the SqlSource and statement identifier of the configuration SQL statement are replaced with the SqlSource and statement identifier of the general MappedStatement to obtain the MappedStatement corresponding to the configuration SQL statement.
[0346] Different SQL statements correspond to different MappedStatements. A MappedStatement is constructed based on an SQL statement. In the MyBatis framework, each Mapper method corresponds to a MappedStatement object, which contains complete SQL information, input parameters, and output parameter definitions. For example, SQL statement A corresponds to MappedStatement object A, and SQL statement B corresponds to MappedStatement object B. SQL statements and their corresponding MappedStatements are bound together by an identifier.
[0347] (7) Electronic devices cache each configuration SQL statement and the corresponding MappedStatement locally.
[0348] In this embodiment, the SqlSource and statement identifier in the general MappedStatement are replaced with the SqlSource and statement identifier based on the configuration SQL statement to obtain the MappedStatement corresponding to the configuration SQL statement. Each MappedStatement corresponding to a configuration SQL statement is then cached in a local cache database or internal storage. The local cache database can be a Redis database, an SQLite database, etc., depending on the specific situation.
[0349] (8) Electronic devices create target interceptors and add annotation information to target interceptors.
[0350] In this embodiment, the target interceptor is an executor interceptor. The executor interceptor is used to intercept the execution of MyBatis executor methods, thereby intercepting the execution of SQL statements.
[0351] In this embodiment of the application, the developer inputs the annotation information of the target interceptor into the electronic device, and the electronic device creates the target interceptor and adds annotation information to the target interceptor based on the annotation information of the target interceptor.
[0352] Specifically, define an interceptor class that implements the org.apache.ibatis.plugin.Interceptor interface, and override the intercept, plugin, and setProperties methods to create the target interceptor.
[0353] The annotation information for the target interceptor can be various annotations, such as @Intercepts, @Signature, @Slf4j, and @Override, etc., with the interface set according to specific circumstances. This annotation information is input by the developers.
[0354] In this embodiment of the application, adding annotation information to the target interceptor includes: identifying the target interceptor through a first annotation; and specifying the type and method of the target interceptor through a second annotation. The first annotation is the @Intercepts annotation, and the second annotation is the @Signature annotation.
[0355] (9) When the electronic device receives a data access request, it calls the general Mapper interface to parse the data access request and obtain the first SQL statement.
[0356] In this embodiment, the data access request is a user-input data access request. The user can trigger the client to send a data access request to the server by clicking a query control on the client's display page. The server then receives the data access request.
[0357] Data access requests can be data query requests, data insertion requests, data deletion requests, data update requests, etc., depending on the specific settings. For example, a data access request can be a data query request, which may include the target user's user identifier and business query information. The user identifier may include, but is not limited to, a user name, user code, or user login account. For instance, the business query information could be: "Query the target user's daily spending for the past week."
[0358] Different data access requests correspond to different SQL statements. A generic Mapper interface parses and maps different data access requests to different SQL statements. For example, data access request A corresponds to SQL statement A, data access request B corresponds to SQL statement B, and data access request C corresponds to SQL statement C. When data access request A is obtained, the generic Mapper interface is called to parse data access request A and map it to SQL statement A, resulting in the first SQL statement. The mapping relationship between data access requests and SQL statements can be configured according to specific circumstances; this application does not impose any limitations on it.
[0359] (10) The electronic device executes the first SQL statement.
[0360] Specifically, the Executor interface in the MyBatis framework is called to execute the first SQL statement.
[0361] (11) When the electronic device executes the first SQL statement, it calls the target interceptor to intercept the execution of the first SQL statement and determines the corresponding target MappedStatement based on the statement identifier of the first SQL statement.
[0362] In this embodiment, after parsing the data access request using the generic Mapper interface to obtain the first SQL statement, the first SQL statement is executed. When the first SQL statement is executed, a target interceptor is invoked to intercept its execution, and the corresponding target MappedStatement is determined based on the statement identifier of the first SQL statement.
[0363] Furthermore, upon executing the first SQL statement, the target interceptor is invoked to intercept the execution of the first SQL statement, and a local search is conducted for the target MappedStatement corresponding to the statement identifier of the first SQL statement. Specifically, the local cache database is searched for the target MappedStatement corresponding to the statement identifier of the first SQL statement.
[0364] (12) The electronic device obtains the SqlSource encapsulated by the target MappedStatement.
[0365] In this embodiment, the target MappedStatement includes the target data source, statement identifier, and parameter information. Specifically, the target MappedStatement is a MappedStatement object, and the target data source is a SqlSource object.
[0366] SqlSource includes at least one node label, which contains the variable to be operated on and the corresponding variable expression.
[0367] (13) Electronic devices dynamically generate a second SQL statement based on SqlSource.
[0368] Specifically, you can locate the node labels line by line in the SqlSource encapsulated by the target MappedStatement, and then determine the variables and variable expressions under that node label.
[0369] Furthermore, a variable node and an assignment node are constructed for each variable, and values are assigned to them. In the example above, assignment nodes need to be created for the variables dimType, startDate, endDate, biz_code, and offset. When assigning values to the assignment nodes, the variable values can be obtained based on the expressions corresponding to each variable. In this way, each variable node and its corresponding assignment node can be regarded as a set of variable mappings. By associating the variable nodes in each set of variable mappings with the root node, a variable mapping hierarchy tree can be obtained. After obtaining the variable mapping hierarchy tree, a complete SQL statement is generated based on the variable mapping hierarchy tree. Specifically, a target data table is generated based on the variable mapping hierarchy tree. The target data table stores each variable contained in SqlSource and its corresponding variable value in key-value format; a second SQL statement is generated based on the target data table.
[0370] (14) The electronic device executes the second SQL statement and obtains the data access result.
[0371] In this embodiment of the application, the target interceptor is invoked to execute the second SQL statement to perform a database query and obtain the data access result.
[0372] The data access results are the results corresponding to the data access request. For example, if the data access request is "Query the target user's daily spending last week", the data access results would be "Monday, 1000 yuan; Tuesday, 2000 yuan; Wednesday, 2000 yuan; Thursday, 5000 yuan; Friday, 9000 yuan; Saturday, 2000 yuan; Sunday, 2000 yuan". The data access results are presented as a List. <Map<String,Object> The preset data structure is returned.
[0373] To facilitate better implementation of the data access method provided in the embodiments of this application, the embodiments of this application also provide a data access device based on the above data access method. The meanings of the terms used are the same as in the above data access method, and for specific implementation details, please refer to the descriptions in the above method embodiments.
[0374] Please refer to Figure 6 , Figure 6 This is a schematic diagram of the structure of an embodiment of the data access device provided in this application. The data access device may include a parsing module 701, an interception module 702, a statement generation module 703, and a statement execution module 704, wherein...
[0375] The parsing module 701 is used to call the target general interface to parse the data access request when a data access request is obtained, and to obtain the first database statement for data access.
[0376] The interception module 702 is used to call the target interceptor to intercept the execution of the first database statement when the first database statement is executed, and to determine the corresponding target mapping declaration object based on the statement identifier of the first database statement, wherein different database statements correspond to different mapping declaration objects;
[0377] Statement generation module 703 is used to dynamically generate a second database statement for data access based on the target mapping declaration object;
[0378] Statement execution module 704 is used to execute the second database statement and obtain the data access results.
[0379] In this embodiment, the parsing module 701 first calls the target general interface to parse the data access request upon receiving it, obtaining a first database statement. Then, the interception module 702, while executing the first database statement, calls the target interceptor to intercept its execution and determines the corresponding target mapping declaration object based on the statement identifier of the first database statement. Different database statements correspond to different mapping declaration objects. Next, the statement generation module 703 dynamically generates a second database statement based on the target mapping declaration object. Finally, the statement execution module 704 executes the second database statement to obtain the data access result. This application utilizes a pre-written target general interface to parse user access requests, obtain and execute the first database statement, and then uses an interceptor to intercept the execution of the first SQL statement. It dynamically generates a new second database statement using the target mapping declaration object corresponding to the first database statement. By parsing and executing statements through a general interface and relying on intercepted statement execution, database statements can be modified in real time without relying on application recompilation and deployment, thereby reducing the cost of data access.
[0380] Please refer to Figure 7 , Figure 7 This is a schematic diagram of another embodiment of the data access device provided in this application. The data access device may include:
[0381] The parsing module 801 is used to call the target general interface to parse the data access request when a data access request is obtained, and to obtain the first database statement for data access.
[0382] The interception module 802 is used to call the target interceptor to intercept the execution of the first database statement when the first database statement is executed, and to determine the corresponding target mapping declaration object based on the statement identifier of the first database statement, wherein different database statements correspond to different mapping declaration objects;
[0383] Statement generation module 803 is used to dynamically generate a second database statement for data access based on the target mapping declaration object;
[0384] Statement execution module 804 is used to execute the second database statement and obtain the data access results.
[0385] In an optional embodiment, the data access device further includes a first configuration module 805, the first configuration module 805 being configured to:
[0386] Retrieve the target source data from the pre-configured database statement;
[0387] Obtain annotation information for the target generic interface;
[0388] Parse the annotation information of the target generic interface to obtain a generic mapping declaration object;
[0389] By replacing the target source data and statement identifier in the general mapping declaration object with the target source data and statement identifier of the configuration database statement, the mapping declaration object corresponding to the configuration database statement is obtained.
[0390] In an optional embodiment, the first configuration module 805 is configured to: obtain pre-configured configuration database statements from a remote configuration system and cache them locally;
[0391] The target source data corresponding to the configuration database statement is constructed based on the configuration database statement.
[0392] In an optional embodiment, the data access device further includes a caching module 806, which is used for:
[0393] The configuration database statements stored in the remote configuration system are cached to local storage at a preset period.
[0394] In an optional embodiment, the interception module 802 includes:
[0395] The acquisition module 8021 is used to acquire the target source data encapsulated in the target mapping declaration object, wherein the target source data includes at least one node label, and the node label contains the variable to be operated on and the corresponding variable expression;
[0396] Modify module 8022 to dynamically generate a second database statement based on the target source data.
[0397] In an optional embodiment, the target source data includes at least one of the following: trim node label, choose node label, foreach node label, and if node label.
[0398] In an optional embodiment, the data access device further includes:
[0399] Interceptor creation module 807 is used to create target interceptors;
[0400] Annotation module 808 is used to add annotation information to the target interceptor.
[0401] In an optional embodiment, the annotation module 808 is configured to:
[0402] The target interceptor is identified by the first annotation;
[0403] The second annotation specifies the type and method of the target interceptor.
[0404] In an optional embodiment, the annotation module 808 is configured to:
[0405] The third annotation is used to write various configuration database statements in the target general interface.
[0406] In an optional embodiment, the return result of the target general interface satisfies a preset data structure, which is a list, where each element is a mapping, and the mapping stores and accesses the elements through key-value pairs.
[0407] In an optional embodiment, the data access device includes a second configuration module 809, which is configured to:
[0408] Once the statement configuration instruction is obtained, the remote configuration system is used to obtain the input configuration database statement and the corresponding statement identifier.
[0409] The configuration database statements and corresponding statement identifiers obtained from the remote configuration system are cached locally.
[0410] In an optional embodiment, the second configuration module 809 is used to:
[0411] Upon receiving the statement configuration instruction, the statement configuration interface will pop up.
[0412] Use a remote configuration system to obtain the configuration database statements and corresponding statement identifiers entered by the user in the statement configuration interface.
[0413] For details on the implementation of each of the above modules, please refer to the previous examples, which will not be repeated here.
[0414] Please refer to Figure 8 , Figure 8 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application.
[0415] The electronic device may include a radio frequency (RF) circuit 901, a memory 902 including one or more computer-readable storage media, an input unit 903, a display unit 904, a sensor 905, an audio circuit 906, a wireless fidelity (WiFi) module 907, a processor 908 including one or more processing cores, and a power supply 909, etc. Those skilled in the art will understand that... Figure 8 The electronic device structure shown does not constitute a limitation on the electronic device and may include more or fewer components than shown, or combine certain components, or have different component arrangements.
[0416] in:
[0417] RF circuit 901 can be used for receiving and transmitting signals during information transmission or calls. Specifically, it receives downlink information from the base station and hands it over to one or more processors 908 for processing; additionally, it transmits uplink data to the base station. Typically, RF circuit 901 includes, but is not limited to, an antenna, at least one amplifier, a tuner, one or more oscillators, a Subscriber Identity Module (SIM) card, a transceiver, a coupler, a low-noise amplifier (LNA), a duplexer, etc. Furthermore, RF circuit 901 can also communicate wirelessly with networks and other devices. Wireless communication can use any communication standard or protocol, including but not limited to GSM, GPRS, CDMA, WCDMA, LTE, email, and SMS.
[0418] The memory 902 can be used to store software programs and modules. The processor 908 executes various functional applications and data processing by running the software programs and modules stored in the memory 902. The memory 902 may mainly include a program storage area and a data storage area. The program storage area may store the operating system, application programs required for at least one function (such as sound playback function, image playback function, etc.), etc.; the data storage area may store data created according to the use of the electronic device (such as audio data, telephone directory, etc.). In addition, the memory 902 may include high-speed random access memory, and may also include non-volatile memory, such as at least one disk storage device, flash memory device, or other volatile solid-state storage device. Accordingly, the memory 902 may also include a memory controller to provide access to the memory 902 for the processor 908 and the input unit 903.
[0419] Input unit 903 can be used to receive input digital or character information, and to generate keyboard, mouse, joystick, optical, or trackball signal inputs related to user settings and function control. Specifically, in one embodiment, input unit 903 may include a touch-sensitive surface and other input devices. A touch-sensitive surface, also known as a touch display or touchpad, can collect user touch operations on or near it (e.g., user operations using fingers, styluses, or any suitable object or accessory on or near the touch-sensitive surface) and drive corresponding connection devices according to a pre-set program. Optionally, the touch-sensitive surface may include a touch detection device and a touch controller. The touch detection device detects the user's touch position and the signal generated by the touch operation, transmitting the signal to the touch controller; the touch controller receives touch information from the touch detection device, converts it into touch point coordinates, sends it to the processor 908, and can receive and execute commands from the processor 908. Furthermore, various types of touch-sensitive surfaces, such as resistive, capacitive, infrared, and surface acoustic wave, can be used. In addition to the touch-sensitive surface, input unit 903 may also include other input devices. Specifically, other input devices may include, but are not limited to, one or more of the following: physical keyboard, function keys (such as volume control buttons, power buttons, etc.), trackball, mouse, joystick, etc.
[0420] Display unit 904 can be used to display information input by the user or information provided to the user, as well as various graphical user interfaces of electronic devices. These graphical user interfaces can be composed of graphics, text, icons, video, and any combination thereof. Display unit 904 may include a display panel, optionally configured as a liquid crystal display (LCD), organic light-emitting diode (OLED), or similar form. Furthermore, a touch-sensitive surface may cover the display panel. When the touch-sensitive surface detects a touch operation on or near it, it transmits the information to processor 908 to determine the type of touch event. Subsequently, processor 908 provides corresponding visual output on the display panel according to the type of touch event. Although in Figure 8 In this context, the touch-sensitive surface and the display panel are two separate components for implementing input and output functions. However, in some embodiments, the touch-sensitive surface and the display panel can be integrated to achieve both input and output functions.
[0421] Electronic devices may also include at least one sensor 905, such as a light sensor, a motion sensor, and other sensors. Specifically, the light sensor may include an ambient light sensor and a proximity sensor, wherein the ambient light sensor can adjust the brightness of the display panel according to the ambient light level, and the proximity sensor can turn off the display panel and / or backlight when the electronic device is moved to the ear. As a type of motion sensor, a gravity acceleration sensor can detect the magnitude of acceleration in various directions (generally three axes), and can detect the magnitude and direction of gravity when stationary. It can be used for applications that recognize the phone's posture (such as landscape / portrait switching, related games, magnetometer posture calibration), vibration recognition related functions (such as pedometers, taps), etc. Other sensors that may be configured in electronic devices, such as gyroscopes, barometers, hygrometers, thermometers, and infrared sensors, will not be described in detail here.
[0422] Audio circuitry 906, a speaker, and a microphone provide an audio interface between the user and the electronic device. Audio circuitry 906 converts received audio data into electrical signals, transmits them to the speaker, and the speaker converts them into sound signals for output. Conversely, the microphone converts collected sound signals into electrical signals, which are then received by audio circuitry 906, converted back into audio data, and processed by processor 908. The processed data is then transmitted via RF circuitry 901 to, for example, another electronic device, or output to memory 902 for further processing. Audio circuitry 906 may also include an earphone jack to facilitate communication between external headphones and the electronic device.
[0423] WiFi is a short-range wireless transmission technology. Electronic devices using the WiFi module 907 can help users send and receive emails, browse web pages, and access streaming media, providing users with wireless broadband internet access. Although Figure 8 WiFi module 907 is shown, but it is understood that it is not a necessary component of the electronic device and can be omitted as needed without changing the nature of the invention.
[0424] The processor 908 is the control center of the electronic device. It connects various parts of the phone via various interfaces and lines, and performs various functions and processes data by running or executing software programs and / or modules stored in the memory 902, and by calling data stored in the memory 902, thereby performing overall detection of the phone. Optionally, the processor 908 may include one or more processing cores; preferably, the processor 908 may integrate an application processor and a modem processor, wherein the application processor mainly handles the operating system, user interface, and applications, and the modem processor mainly handles wireless communication. It is understood that the modem processor may also not be integrated into the processor 908.
[0425] The electronic device also includes a power supply 909 (such as a battery) that supplies power to various components. Preferably, the power supply can be logically connected to the processor 908 through a power management system, thereby enabling functions such as charging, discharging, and power consumption management through the power management system. The power supply 909 may also include one or more DC or AC power supplies, recharging systems, power fault detection circuits, power converters or inverters, power status indicators, and other arbitrary components.
[0426] Although not shown, the electronic device may also include a camera, Bluetooth module, etc., which will not be described in detail here. Specifically, in this embodiment, the processor 908 in the electronic device loads the executable files corresponding to the processes of one or more applications into the memory 902 according to the following instructions, and the processor 908 runs the applications stored in the memory 902 to realize various functions:
[0427] Upon receiving a data access request, the target general interface is called to parse the data access request and obtain the first database statement. If the first database statement is executed, the target interceptor is called to intercept the execution of the first database statement and to determine the corresponding target mapping declaration object based on the statement identifier of the first database statement. Different database statements correspond to different mapping declaration objects. The second database statement is dynamically generated based on the target mapping declaration object. The second database statement is executed to obtain the data access result.
[0428] It should be noted that the electronic device provided in this application embodiment and the data access method in the above embodiment belong to the same concept, and the specific implementation process can be found in the above related embodiments, which will not be repeated here.
[0429] This application also provides a computer-readable storage medium storing a computer program thereon. When the computer program stored thereon is executed on the processor of the electronic device provided in the embodiments of this application, the processor of the electronic device performs the steps in the data access method provided in this application. The storage medium may be a magnetic disk, an optical disk, a read-only memory (ROM), or a random access memory (RAM), etc.
[0430] This application also provides a computer program product or computer program that includes computer instructions stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the computer device to perform various optional implementations of the data access method described above.
[0431] The above provides a detailed description of a data access method and apparatus provided in this application. Specific examples have been used to illustrate the principles and implementation methods of this application. The description of the above embodiments is only for the purpose of helping to understand the method and core ideas of this application. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of this application. Therefore, the content of this specification should not be construed as a limitation of this application.
[0432] It should be noted that when the above embodiments of this application are applied to specific products or technologies, and user-related data is involved, user permission or consent is required, and the collection, use and processing of related data must comply with the relevant laws, regulations and standards of the relevant countries and regions.
Claims
1. A data access method, characterized in that, The data access method includes: Upon receiving a data access request, the target general interface is called to parse the data access request and obtain the first database statement for data access. When the first database statement is executed, the target interceptor is invoked to intercept the execution of the first database statement, and the corresponding target mapping declaration object is determined based on the statement identifier of the first database statement. Different database statements correspond to different mapping declaration objects, and the mapping declaration object is used to generate database statements for data access. Based on the target mapping declaration object, a second database statement for data access is dynamically generated; Execute the second database statement to obtain the data access results.
2. The data access method according to claim 1, characterized in that, The data access method further includes: Retrieve the target source data from the pre-configured database statement; Obtain the annotation information of the target general interface; Parse the annotation information of the target generic interface to obtain a generic mapping declaration object; The target source data and statement identifier in the general mapping declaration object are replaced with the target source data and statement identifier of the configuration database statement to obtain the mapping declaration object corresponding to the configuration database statement.
3. The data access method according to claim 2, characterized in that, The step of obtaining the target source data for the pre-configured configuration database statement includes: Retrieve pre-configured configuration database statements from the remote configuration system and cache them locally; The target source data corresponding to the configuration database statement is constructed based on the configuration database statement.
4. The data access method according to claim 3, characterized in that, The data access method further includes: The configuration database statements stored in the remote configuration system are cached to local storage at a preset period.
5. The data access method according to claim 1, characterized in that, The dynamic generation of a second database statement for data access based on the target mapping declaration object includes: Obtain the target source data encapsulated in the target mapping declaration object, wherein the target source data includes at least one node label, and the node label contains the variable to be operated on and the corresponding variable expression; The second database statement is dynamically generated based on the target source data.
6. The data access method according to claim 1, characterized in that, The data access method further includes: Create a target interceptor; Add annotation information to the target interceptor.
7. The data access method according to claim 6, characterized in that, Adding annotation information to the target interceptor includes: The target interceptor is identified by the first annotation; The second annotation specifies the type and method of the target interceptor.
8. The data access method according to claim 1, characterized in that, The data access method further includes: The third annotation is used to write various configuration database statements in the target general interface.
9. The data access method according to claim 1, characterized in that, The data access method further includes: Create a target general interface. The return result of the target general interface satisfies a preset data structure. The preset data structure is a list, where each element is a mapping. The mapping stores and accesses elements through key-value pairs.
10. The data access method according to claim 1, characterized in that, The data access method further includes: Upon receiving a statement configuration instruction, the remote configuration system is used to obtain the input configuration database statement and its corresponding statement identifier. The configuration database statements and corresponding statement identifiers obtained by the remote configuration system are cached locally.
11. The data access method according to claim 10, characterized in that, Upon receiving a statement configuration instruction, the step of using the remote configuration system to obtain the input configuration database statement and its corresponding statement identifier includes: Upon receiving the statement configuration instruction, a statement configuration interface will pop up; The remote configuration system is used to obtain the configuration database statement and corresponding statement identifier entered by the user in the statement configuration interface.
12. A data access device, characterized in that, The data access device includes: The parsing module is used to call the target general interface to parse the data access request when a data access request is obtained, and obtain the first database statement for data access. The interception module is used to call the target interceptor to intercept the execution of the first database statement when the first database statement is executed, and to determine the corresponding target mapping declaration object based on the statement identifier of the first database statement. Different database statements correspond to different mapping declaration objects, and the mapping declaration object is used to generate database statements for data access. The statement generation module is used to dynamically generate a second database statement for data access based on the target mapping declaration object; The statement execution module is used to execute the second database statement and obtain the data access results.
13. An electronic device, characterized in that, It includes a memory and a processor, the memory storing a computer program, and the processor running the computer program in the memory to perform the steps of the data access method according to any one of claims 1 to 12.
14. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a plurality of instructions adapted for loading by a processor to perform the steps of the data access method according to any one of claims 1 to 12.
15. A computer program product, comprising a computer program or instructions, characterized in that, When the computer program or instructions are executed by a processor, they implement the steps of the data access method according to any one of claims 1 to 12.