Primary key management method and device, equipment and storage medium
By determining the primary key strategy and generating primary key values based on business type during the data addition process, the problem of primary key generation strategies being limited by application scenarios in existing technologies is solved, achieving efficient data processing and system scalability.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- RICHFIT INFORMATION TECH
- Filing Date
- 2024-12-04
- Publication Date
- 2026-06-05
AI Technical Summary
Existing primary key generation strategies are limited by application scenarios, lack scalability, and result in low data processing efficiency.
When a business request type of data addition is detected, the target primary key strategy is determined based on a pre-defined primary key strategy table, the target primary key value is generated, and it is populated into the business data to ensure the uniqueness and continuity of the primary key. The persistence framework is used to intercept and generate the primary key before insertion into the database.
It improves data processing efficiency, enhances system scalability and flexibility, ensures the uniqueness and integrity of data in the database, and reduces the workload of developers.
Smart Images

Figure CN122152812A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of data processing technology, and in particular to a primary key management method, apparatus, device and storage medium. Background Technology
[0002] With the rapid advancement of information technology and the arrival of the big data era, the amount of data processed by business systems is growing exponentially, and the data types are becoming increasingly complex and diverse. Against this technological backdrop, the primary key, as a unique identifier for each record in a database, is directly related to data consistency, integrity, query efficiency, and the overall performance and scalability of the system through its design and management.
[0003] In the existing technology, primary key generation strategies mainly include primary key strategies based on database characteristics and primary key generation strategies based on business systems.
[0004] However, both of these gradual strategies are limited by the application scenarios and lack scalability, resulting in low data processing efficiency. Summary of the Invention
[0005] This application provides a primary key management method, apparatus, device, and storage medium to address the problem that existing technologies are limited by application scenarios, lack scalability, and thus result in low data processing efficiency.
[0006] In a first aspect, embodiments of this application provide a primary key management method, including:
[0007] When the business type of the received business request is detected to be data addition, a target primary key strategy is determined in a pre-defined primary key strategy table based on the business type. The primary key strategy table includes at least one mapping relationship between business types and corresponding primary key strategies.
[0008] Based on the target primary key strategy, generate the target primary key value corresponding to the business request;
[0009] The target primary key value is filled into the business data corresponding to the requested business, and the filled business data is inserted into the database.
[0010] In one possible implementation, each primary key strategy includes the business type, strategy name, current value, and step size corresponding to the primary key strategy. Then, generating the target primary key value corresponding to the requested business based on the target primary key strategy includes:
[0011] After detecting that the primary key policy is enabled, the target primary key policy is locked.
[0012] The target primary key value is calculated based on the current value and step size corresponding to the target primary key strategy.
[0013] In one possible implementation, the method further includes:
[0014] Based on the target primary key value, update the current value of the target primary key policy in the primary key policy table.
[0015] In one possible implementation, before determining the target primary key strategy in a pre-defined primary key strategy table based on the business type, the method further includes:
[0016] Receive the service request sent by the user;
[0017] Establish a connection with the database based on the business request;
[0018] After successfully establishing a connection with the database, the primary key strategy table is created.
[0019] In one possible implementation, establishing the primary key strategy table includes:
[0020] When the application starts, the business entity objects in the application are scanned to obtain the primary key strategy corresponding to each business entity object;
[0021] The primary key strategy table is established based on the primary key strategy corresponding to each business entity object.
[0022] In one possible implementation, establishing the primary key strategy table includes:
[0023] When the application starts, the business entity objects in the application are scanned to obtain the primary key strategy corresponding to each business entity object;
[0024] Based on the primary key strategy corresponding to each business entity object, the pre-obtained initial primary key strategy table is updated to obtain the primary key strategy table.
[0025] In one possible implementation, the method further includes:
[0026] When a business anomaly is detected, the business data and the target primary key value are rolled back.
[0027] Secondly, embodiments of this application provide a primary key management device, comprising:
[0028] The determination module is used to determine the target primary key strategy in a pre-set primary key strategy table based on the business type when the business type of the received business request is detected to be data addition. The primary key strategy table includes at least one mapping relationship between business type and corresponding primary key strategy.
[0029] The generation module is used to generate the target primary key value corresponding to the business request based on the target primary key strategy;
[0030] The insertion module is used to fill the target primary key value into the business data corresponding to the requested business, and then insert the filled business data into the database.
[0031] In one possible implementation, each primary key strategy includes the business type, strategy name, current value, and step size corresponding to the primary key strategy. The generation module is specifically used for:
[0032] After detecting that the primary key policy is enabled, the target primary key policy is locked.
[0033] The target primary key value is calculated based on the current value and step size corresponding to the target primary key strategy.
[0034] In one possible implementation, the device further includes:
[0035] The update module is used to update the current value of the target primary key policy in the primary key policy table based on the target primary key value.
[0036] In one possible implementation, the device further includes:
[0037] A receiving module is used to receive the service request sent by the user;
[0038] The connection module is used to establish a connection with the database based on the business request;
[0039] A module is established to create the primary key strategy table after a successful connection is established with the database.
[0040] In one possible implementation, the establishment module is specifically used for:
[0041] When the application starts, the business entity objects in the application are scanned to obtain the primary key strategy corresponding to each business entity object;
[0042] The primary key strategy table is established based on the primary key strategy corresponding to each business entity object.
[0043] In one possible implementation, the establishment module is further specifically used for:
[0044] When the application starts, the business entity objects in the application are scanned to obtain the primary key strategy corresponding to each business entity object;
[0045] Based on the primary key strategy corresponding to each business entity object, the pre-obtained initial primary key strategy table is updated to obtain the primary key strategy table.
[0046] In one possible implementation, the device further includes:
[0047] The rollback module is used to roll back the business data and the target primary key value when a business anomaly is detected.
[0048] Thirdly, embodiments of this application provide an electronic device, including: a processor, and a memory communicatively connected to the processor;
[0049] The memory stores computer-executed instructions;
[0050] The processor executes computer execution instructions stored in the memory, causing the processor to perform the first aspect and / or various possible implementations of the first aspect as described above.
[0051] Fourthly, embodiments of this application provide a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, are used to implement the first aspect and / or various possible implementations of the first aspect.
[0052] Fifthly, embodiments of this application provide a computer program product, including a computer program that, when executed by a processor, implements the first aspect and / or various possible implementations of the first aspect.
[0053] The primary key management method, apparatus, device, and storage medium provided in this application, when detecting that the received business request's business type is data addition, determines a target primary key strategy in a pre-defined primary key strategy table based on the business type, generates a target primary key value corresponding to the business request based on the target primary key strategy, fills the target primary key value into the business data corresponding to the requested business, and inserts the filled business data into the database. The above method determines the primary key strategy and generates a primary key value based on the business type during the data addition process, ensuring primary key uniqueness, guaranteeing the uniqueness and integrity of data in the database, improving data processing efficiency, and enhancing system scalability and flexibility. Attached Figure Description
[0054] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.
[0055] Figure 1 A schematic diagram of the application architecture for the primary key management method provided in this application;
[0056] Figure 2 Flowchart of the primary key management method provided in this application Figure 1 ;
[0057] Figure 3 Flowchart of the primary key management method provided in this application Figure 2 ;
[0058] Figure 4 Flowchart of the primary key management method provided in this application Figure 3 ;
[0059] Figure 5 Flowchart of the primary key management method provided in this application Figure 4 ;
[0060] Figure 6 A schematic diagram illustrating a specific example of the primary key management method provided in this application;
[0061] Figure 7 A schematic diagram of the primary key management device provided in this application;
[0062] Figure 8 A schematic diagram of the structure of the electronic device provided in this application.
[0063] The accompanying drawings illustrate specific embodiments of this application, which will be described in more detail below. These drawings and descriptions are not intended to limit the scope of the concept in any way, but rather to illustrate the concept of this application to those skilled in the art through reference to particular embodiments. Detailed Implementation
[0064] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims.
[0065] With the rapid advancement of information technology and the arrival of the big data era, the amount of data processed by business systems is growing exponentially, and the data types are becoming increasingly complex and diverse. In this technological context, the primary key, as a unique identifier for each record in a database, is crucial in its design and management, directly impacting data consistency, integrity, query efficiency, and the overall performance and scalability of the system. Existing primary key generation strategies mainly include those based on database characteristics and those based on business systems. However, both of these strategies are limited by application scenarios and lack scalability, resulting in low data processing efficiency.
[0066] To address the aforementioned problems, this application provides a primary key management method, apparatus, device, and storage medium. This maintains data continuity and consistency while reducing developer workload and improving application efficiency, maintainability, and scalability. Specifically, existing technologies for primary key generation strategies fall into two categories. The first category is based on database characteristics, such as each record in the database having a unique primary key, with the database automatically incrementing the primary key value each time a new record is inserted. This strategy is simple and easy to use, but may cause problems across databases or servers because the auto-increment starting value may differ for each database or server. Alternatively, a sequence is an object in the database that can generate a sequence number, which can be used as a primary key. This strategy can solve the problem of auto-incrementing primary keys across databases or servers, but may introduce additional complexity and performance overhead. Another approach is to generate primary keys using triggers in the database. Before inserting a new record, the trigger first generates a unique primary key and then inserts this primary key into the record. This strategy offers greater flexibility, but requires careful handling of conflicts during concurrent insertions. The second category is primary key generation strategies based on business systems. For example, a Universally Unique Identifier (UUID) is a globally unique identifier, typically used to generate unique primary keys. UUIDs are usually quite long, but guarantee global uniqueness. This strategy is suitable for distributed systems, but may increase storage and network transmission overhead. Alternatively, an algorithm for generating globally unique primary keys in a distributed system can be used. The Snowflake algorithm can generate a 64-bit integer, with some bits used for a timestamp, some for a machine identifier, and some for a counter. This strategy guarantees unique primary keys in a distributed system, but requires careful handling of algorithm implementation and configuration issues. Considering these issues, the inventors explored whether a unique architecture could be adopted to pre-define the primary key strategy table within the persistence framework to enhance functionality. When a new record addition operation is detected, data interception is performed, and the data record is populated with a business-continuous and unique primary key before persisting the data to the database. This provides application developers with an efficient and reliable primary key maintenance strategy, ensuring gradual uniqueness and continuity, while overcoming the limitations of application scenarios.
[0067] Figure 1 A schematic diagram of the application architecture for the primary key management method provided in this application is shown below. Figure 1As shown, this architecture is a Model-View-Controller (MVC) architecture. The MVC architecture separates different parts of an application to improve code maintainability, scalability, and reusability. Specifically, it includes a model, view, and controller. In the application scenario of this embodiment, the model layer can receive user business requests and then process the business through the Service layer. The Service layer can call the DAO layer to access the database and can also call other Services to complete complex business logic. The DAO layer is responsible for data persistence operations, i.e., interaction with the database. It typically includes CRUD (Create, Read, Update, Delete) operations. Persistence frameworks are used to simplify database operations, such as Hibernate and JPA, making the implementation of the DAO layer simpler and more efficient. The DB (database) is used to store the application's data.
[0068] Optionally, the persistence layer framework can be enhanced to intercept database insert operations when they are detected. Then, the data can be generated and inserted using a pre-defined primary key strategy table, and the original persistence operation can continue to be performed to insert the data record into the database.
[0069] The technical solution of this application and how the technical solution of this application solves the above-mentioned technical problems are described in detail below with specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments. The embodiments of this application will now be described with reference to the accompanying drawings.
[0070] Figure 2 Flowchart of the primary key management method provided in this application Figure 1 ,like Figure 2 As shown, the method includes:
[0071] S201: When the business type of the received business request is detected to be data addition, the target primary key strategy is determined in the pre-defined primary key strategy table based on the business type.
[0072] In this step, to maintain data continuity and consistency while reducing developer workload, improving application efficiency, maintainability, and scalability, and overcoming application scenario limitations, when a received business request is detected as involving data addition, the business data is intercepted, thereby determining the target primary key strategy in a pre-defined primary key strategy table.
[0073] Specifically, the first step is to extract or identify the business type from the received business request. This can be done by parsing the parameters or message body in the request, which may contain identifiers representing the business type (such as a business type code or name). The identified business type is then used as a query condition to retrieve a pre-defined primary key strategy table. This table contains the mapping relationship between various business types and their corresponding primary key strategies, as well as the current status of each primary key strategy. Once a strategy matching the business type is found in the primary key strategy table, it is used as the target primary key strategy. The target strategy will guide the system on how to generate a unique primary key for new data records.
[0074] S202: Generate the target primary key value corresponding to the business request based on the target primary key strategy.
[0075] In this step, after the target primary key strategy is determined through the above steps, the primary key strategy includes the business type, strategy name, current value, and step size corresponding to the primary key strategy. Then, the target primary key value corresponding to the business request is generated based on the information in the primary key strategy.
[0076] Specifically, after detecting that the primary key policy is enabled, the target primary key policy is locked, and the target primary key value is calculated based on the current value and step size corresponding to the target primary key policy.
[0077] S203: Populate the target primary key value into the business data corresponding to the requested business, and insert the populated business data into the database.
[0078] In this step, after obtaining the target primary key value, it is populated into the corresponding field of the business data, and then the business request is processed, and finally the data is persisted to the database.
[0079] For example, a database insert operation can be constructed using a persistence layer framework (such as Hibernate, JPA, MyBatis, etc.). A database table record corresponding to the business data object is created. The API provided by the persistence layer framework is called to perform the insert operation, inserting the populated business data into the database. This step may trigger multiple sub-steps within the framework, such as generating SQL statements, executing SQL statements, and processing the result set.
[0080] Optionally, check if the database operation was successful. If successful, it means the new record has been successfully inserted into the database table and can be retrieved using the primary key value. If it fails, you may need to handle the exception, such as retrying the operation, rolling back the transaction, or reporting the error to the user.
[0081] In one possible implementation, to ensure the validity and continuity of the primary key strategy, maintain data consistency and integrity, and support the implementation of business logic, after inserting business data with added primary key values into the database, the current value corresponding to the target primary key strategy in the primary key strategy table is modified to the target primary key value.
[0082] For example, the first step is to define the update logic for the current value field of the target primary key strategy in the primary key strategy table. For instance, if an auto-increment strategy is used, the current value should increment after each new record is inserted; if a timestamp-based strategy is used, it may need to be updated to the next available timestamp or sequence number. Before performing the update, the current value of the current value field in the primary key strategy table needs to be retrieved. This is typically to calculate the next primary key value or verify whether the current value needs updating. For example, in some strategies, if the generated primary key value exceeds the range of the current value, the current value may need to be updated. The new current value is calculated based on the primary key generation strategy and the primary key values of the inserted data. Using the APIs provided by persistence layer frameworks (such as Hibernate, JPA, MyBatis, etc.), the SQL statement to update the current value field in the primary key strategy table is constructed and executed. This operation must be ensured to be atomic to avoid data inconsistencies caused by concurrent updates.
[0083] It's important to note that updating the current value of the primary key strategy table ensures that previously assigned primary key values are not reused in subsequent primary key generation, thus maintaining primary key uniqueness. For certain primary key strategies (such as auto-incrementing sequences, timestamp + sequence number, etc.), updating the current value ensures that newly generated primary key values are sequential in business logic, which facilitates data sorting, pagination, and retrieval operations. In distributed systems or high-concurrency environments, multiple business requests may simultaneously request primary key generation. By updating the current value of the primary key strategy table, these requests can be coordinated to prevent them from generating the same primary key value, thereby avoiding primary key conflicts.
[0084] In one possible implementation, if a business exception occurs during business operations, the business data and the target primary key value are rolled back.
[0085] If both the business operation and the generation of the target primary key value are performed within a single transaction, then the entire transaction will be rolled back if a business exception occurs.
[0086] Optionally, business exceptions may include, but are not limited to, the following types:
[0087] 1. Data validation error:
[0088] The input data does not conform to the specified format or range; required fields are empty; data types do not match; data length exceeds the limit.
[0089] 2. Insufficient permissions:
[0090] The user attempted to perform an operation outside their authorized scope; no valid authentication was provided when accessing the restricted resource.
[0091] 3. Uniqueness constraint violation:
[0092] The database's uniqueness constraints (such as primary key conflicts or unique index conflicts) were violated when attempting to insert or update data.
[0093] 4. Related data does not exist:
[0094] Try referencing or modifying non-existent related data (e.g., try to update deleted data).
[0095] 5. External service call failed:
[0096] An error occurred while calling an external API or web service (such as timeout, service unavailable, or invalid data returned).
[0097] 6. Transaction conflicts:
[0098] In a high-concurrency environment, conflicts occur when multiple transactions attempt to modify the same data simultaneously.
[0099] 7. Resource limitations:
[0100] Insufficient system resources (such as memory, disk space, or database connection pool exhaustion).
[0101] 8. Business process anomaly:
[0102] A failure in a step of a business process prevents the entire process from continuing (e.g., an approver in an approval process does not respond).
[0103] 9. System status abnormality:
[0104] Internal system errors or abnormal states prevent the continued processing of business requests (such as database connection failures or service crashes).
[0105] The primary key management method provided in this embodiment detects that the received business request's business type is data addition. Based on the business type, it determines the target primary key strategy in a pre-defined primary key strategy table. Based on the target primary key strategy, it generates the target primary key value corresponding to the business request, fills the target primary key value into the business data corresponding to the requested business, and inserts the filled business data into the database. This method determines the primary key strategy and generates the primary key value based on the business type during the data addition process, ensuring primary key uniqueness, guaranteeing the uniqueness and integrity of data in the database, improving data processing efficiency, and enhancing system scalability and flexibility.
[0106] Figure 3 Flowchart of the primary key management method provided in this application Figure 2 ,like Figure 3 As shown, based on the above embodiments, step S201 specifically includes:
[0107] S301: After detecting that the primary key policy is enabled, the target primary key policy is locked.
[0108] In this step, after determining the target primary key strategy, the target primary key strategy is locked to ensure the accuracy and consistency of primary key generation.
[0109] Specifically, if the primary key strategy table is stored in a database, database-provided locking mechanisms (such as row locks and table locks) can be used to lock records under the target primary key strategy. This prevents other concurrent transactions from modifying the record simultaneously, thus avoiding inconsistencies in the primary key strategy.
[0110] If the system is a multi-node distributed system, distributed locks can be used to ensure locking across nodes. Common distributed lock implementations include Redis-based locks and Zookeeper-based locks. In some cases, a global locking of the primary key strategy may be necessary to prevent any modifications. This is typically used in critical business scenarios. Local locking of the primary key strategy can also be applied to specific business types or time periods to reduce the impact on system performance. The lock is maintained during the primary key value generation process. Once the primary key value is generated and populated into the business data, the lock is released.
[0111] Optionally, in certain special circumstances, such as during system maintenance or data migration, the primary key policy can be locked for an extended period. This typically needs to be done during off-peak business hours to minimize the impact on the system.
[0112] S302: Calculate the target primary key value based on the current value and step size corresponding to the target primary key strategy.
[0113] In this step, after the target primary key strategy is locked, the target primary key value is generated according to the strategy.
[0114] Specifically, each primary key strategy includes the corresponding business type, strategy name, current value, and step size. The current value refers to the current state of the primary key strategy. The step size is the increment unit when retrieving the next primary key value. The target primary key value can then be obtained by adding the current value and the step size.
[0115] The primary key management method provided in this embodiment locks the target primary key policy after detecting that the primary key policy is enabled, and calculates the target primary key value based on the current value and step size corresponding to the target primary key policy. This method ensures the uniqueness and continuity of the primary key, improves the efficiency and reliability of data processing, simplifies the complexity of primary key management, supports high-concurrency and distributed environments, and provides flexible configuration and scalability.
[0116] Figure 4 Flowchart of the primary key management method provided in this application Figure 3 ,like Figure 4 As shown, based on the above embodiments, the method further includes:
[0117] S401: Receive service requests sent by users.
[0118] S402: Establish a connection with the database based on the business request.
[0119] S403: After successfully establishing a connection with the database, create the primary key strategy table.
[0120] Users send business requests via a front-end interface or other means (such as API calls). The back-end server receives the request and performs initial verification and parsing. Based on the information provided in the business request (such as database type, address, username, password, etc.), the back-end server attempts to establish a connection with the database. If the connection is successful, subsequent operations are performed; if the connection fails, an error message is returned to the user, and retrying or logging may be performed.
[0121] Optionally, applications can obtain database connections through database connection pools (such as HikariCP, C3P0, etc.). Connection pools maintain a certain number of database connections and provide them to applications as needed. This improves the reuse rate of database connections and reduces the overhead of connection establishment and release.
[0122] Another approach is to establish a database connection directly through a database driver (such as JDBC). However, this approach is generally less efficient than using a connection pool.
[0123] After successfully establishing a database connection, the application server uses the database connection object to begin a new transaction. This is used to initiate the execution of business requests. In SQL, this is typically achieved by executing a `BEGIN TRANSACTION` (or a similar command such as `START TRANSACTION`, depending on the database system). In some database access frameworks or ORM (Object-Relational Mapping) tools, the start of a transaction may be achieved by calling a specific method or function, such as `beginTransaction()`. Inside the transaction, the application server performs the aforementioned primary key value generation and the database operations required by the business requests.
[0124] Before starting transaction execution, a primary key strategy table needs to be created.
[0125] Specifically, when the application starts, it scans the business entity objects in the application to obtain the primary key strategy corresponding to each business entity object. If no initial primary key strategy table is detected, a primary key strategy table is created based on the primary key strategy corresponding to each business entity object. If an initial primary key strategy table is detected, the pre-obtained initial primary key strategy table is updated based on the primary key strategy corresponding to each business entity object to obtain the final primary key strategy table.
[0126] The primary key management method provided in this embodiment receives business requests sent by users, establishes a connection with the database based on the business requests, and creates a primary key strategy table after successfully establishing a connection with the database. This method improves data integrity and consistency, optimizes data access performance, and enhances system scalability and maintainability.
[0127] Figure 5 Flowchart of the primary key management method provided in this application Figure 4 ,like Figure 5 As shown, based on the above embodiment, step S403 specifically includes:
[0128] S501: When the application starts, scan the business entity objects in the application to obtain the primary key strategy corresponding to each business entity object.
[0129] In this step, when the application starts, it automatically identifies and extracts the primary key strategy for each object by scanning business entity objects. This reduces the workload of manually configuring primary key strategies and improves development efficiency.
[0130] Optionally, the primary key strategy can be flexibly configured according to the different needs of business entities. For example, an auto-incrementing primary key can be configured for some entities, while a UUID or a primary key generated by the snowflake algorithm can be configured for others. This flexibility helps to meet diverse business needs.
[0131] S502: Establish a primary key strategy table based on the primary key strategy corresponding to each business entity object.
[0132] or,
[0133] S503: Based on the primary key strategy corresponding to each business entity object, update the pre-obtained initial primary key strategy table to obtain the primary key strategy table.
[0134] First, determine if the primary key strategy table has been created. If not, create the primary key strategy table. If it exists, use the existing strategy table as the initial primary key strategy table. Then, incorporate the primary key strategies obtained from the scan into the primary key strategy table. For primary key strategies of business entities that are already managed by the primary key strategy table, update them based on the specific configuration information of the scanned primary key strategies.
[0135] The primary key management method provided in this embodiment scans the business entity objects in the application upon startup to obtain the primary key strategy corresponding to each business entity object. Based on the primary key strategy corresponding to each business entity object, a primary key strategy table is established. Alternatively, based on the primary key strategy corresponding to each business entity object, a pre-obtained initial primary key strategy table is updated to obtain the final primary key strategy table. This method achieves more efficient and flexible data management, helps improve data consistency and integrity, optimizes performance, and simplifies maintenance and upgrades.
[0136] Figure 6 A schematic diagram illustrating a specific example of the primary key management method provided in this application is shown below. Figure 6 As shown, it includes:
[0137] S601: Obtain a connection.
[0138] S602: Start a transaction.
[0139] S603: Business Operations.
[0140] S604: Insertion interception.
[0141] S605: Obtain primary key strategy.
[0142] S606: Generate a primary key based on the primary key strategy.
[0143] S607: Add the newly generated primary key to the business entity.
[0144] S608: Business insertion into the database.
[0145] S609: Real-time transaction monitoring; if a business anomaly is detected, the transaction is rolled back.
[0146] The above steps are implemented in the same way as those in the previous embodiments, and will not be repeated here.
[0147] The primary key management method provided in this embodiment divides the application through a unique software development structure and paradigm, and enhances the functionality of the persistence framework. It then generates business primary keys based on a preset primary key strategy table, inserting the business data with the added primary keys into the database. This ensures the uniqueness and continuity of the primary keys and guarantees the continuity of business processes. This means that even within a single application, efficient primary key generation and maintenance can be achieved. It does not intrude on the application's code and does not disrupt its business framework design. It can be integrated into the application without affecting existing business logic. This means that developers can continue to use familiar technologies and languages to write the application's business logic while benefiting from the primary key maintenance functionality provided by this embodiment.
[0148] Optionally, the primary key management method provided in this application is designed to be implemented based on the Java programming language and the Spring and MyBatis-Plus frameworks. Before implementation, it is essential to ensure that a Java runtime environment and data source configuration are available, while also complying with the relevant development regulations of MyBatis-Plus.
[0149] Java runtime environment:
[0150] The Java runtime environment is fundamental and must be correctly installed and configured. The Java runtime environment includes the JDK (Java Development Kit).
[0151] Data source configuration:
[0152] The data source is a crucial component, used for storing and managing data. A suitable data source must be configured to support the implementation of the methods provided in this application. In the Spring framework, connection information for the data source, including the database driver class, database URL, username, and password, can be specified through configuration files. In the MyBatis-Plus framework, connection information for the data source can be specified through configuration files, and mapping files can be configured to separate the data access layer from the business logic layer.
[0153] MyBatis-Plus Development Guidelines:
[0154] The MyBatis-Plus framework provides a convenient way to implement the data access layer. To ensure development efficiency and code quality, the following development guidelines must be followed:
[0155] 1. Database Table Mapping Standards: When using the MyBatis-Plus framework for data access, corresponding entity classes and mapping files must be created to define the mapping relationship between database tables and Java objects. Entity classes and mapping files should maintain consistent naming conventions and good coding style.
[0156] 2. SQL Statement Writing Guidelines: MyBatis-Plus supports data manipulation using native SQL statements. However, to improve code readability and maintainability, it is recommended to use the dynamic SQL statement generation feature provided by MyBatis-Plus. When using dynamic SQL statements, the corresponding writing guidelines should be followed to ensure the correctness and security of the SQL statements.
[0157] 3. Parameter Passing Guidelines: When accessing data using MyBatis-Plus, parameters are required to specify query conditions, update content, etc. To ensure the security and correctness of parameter passing, it is recommended to use the parameter passing methods provided by MyBatis-Plus and avoid directly concatenating SQL statements.
[0158] 4. Exception Handling Guidelines: When using MyBatis-Plus for data access, various exceptions may occur, such as database connection failures and SQL statement errors. To ensure timely and correct exception handling, appropriate exception handling logic must be added to the code, and exceptions must be logged and flagged in detail.
[0159] By adhering to the above development guidelines, the maintainability and scalability of the primary key management method provided in the embodiments of this application can be improved, while ensuring the stability and reliability of the application.
[0160] Figure 7 A schematic diagram of the primary key management device provided in this application is shown below. Figure 7 As shown, the primary key management device 700 specifically includes:
[0161] The determination module 701 is used to determine the target primary key strategy based on the business type when the business type of the received business request is detected to be data addition. The primary key strategy table includes at least one mapping relationship between business type and corresponding primary key strategy.
[0162] The generation module 702 is used to generate the target primary key value corresponding to the business request based on the target primary key strategy.
[0163] The insertion module 703 is used to populate the target primary key value into the business data corresponding to the requested business, and then insert the populated business data into the database.
[0164] Optionally, if each primary key strategy includes the corresponding business type, strategy name, current value, and step size, then module 702 is specifically used for:
[0165] Once the primary key policy is detected as enabled, the target primary key policy is locked.
[0166] The target primary key value is calculated based on the current value and step size corresponding to the target primary key strategy.
[0167] Optionally, the primary key management device 700 also includes:
[0168] The update module 704 is used to update the current value of the target primary key policy in the primary key policy table based on the target primary key value.
[0169] Optionally, the primary key management device 700 also includes:
[0170] The receiving module 705 is used to receive service requests sent by users.
[0171] Connection module 706 is used to establish a connection with the database based on business requests.
[0172] Module 707 is used to create the primary key strategy table after a successful connection to the database.
[0173] Optionally, module 707 is specifically used for:
[0174] When the application starts, it scans the business entity objects in the application to obtain the primary key strategy corresponding to each business entity object.
[0175] A primary key strategy table is created based on the primary key strategy corresponding to each business entity object.
[0176] Optionally, module 707 can also be specifically used for:
[0177] When the application starts, it scans the business entity objects in the application to obtain the primary key strategy corresponding to each business entity object.
[0178] Based on the primary key strategy corresponding to each business entity object, the pre-obtained initial primary key strategy table is updated to obtain the primary key strategy table.
[0179] Optionally, the primary key management device 700 also includes:
[0180] The rollback module 708 is used to roll back business data and target primary key values when a business anomaly is detected.
[0181] The primary key management device provided in the foregoing embodiments can execute the primary key management method provided in the above method embodiments. Its implementation principle and technical effect are similar, and will not be described in detail here.
[0182] Figure 8 A schematic diagram of the electronic structure provided in this application. (See attached diagram.) Figure 8As shown, the electronic device 800 provided in this embodiment includes at least one processor 801 and a memory 802. Optionally, the device 800 further includes a communication component 803. The processor 801, memory 802, and communication component 803 are connected via a bus 804.
[0183] In a specific implementation, at least one processor 801 executes computer execution instructions stored in memory 802, causing at least one processor 801 to perform the above-described method.
[0184] The specific implementation process of processor 801 can be found in the above method embodiments, and its implementation principle and technical effect are similar. It will not be repeated here.
[0185] In the above embodiments, it should be understood that the processor can be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), etc. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the method disclosed in this invention can be directly implemented by a hardware processor, or implemented by a combination of hardware and software modules within the processor.
[0186] The memory may include random access memory (RAM) and may also include non-volatile memory (NVM), such as at least one disk storage device.
[0187] The bus can be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus, etc. Buses can be categorized as address buses, data buses, control buses, etc. For ease of illustration, the buses shown in the accompanying drawings are not limited to a single bus or a single type of bus.
[0188] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the above-described method.
[0189] This application also provides a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, implement the above-described method.
[0190] The aforementioned readable storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk. The readable storage medium can be any available medium accessible to a general-purpose or special-purpose computer.
[0191] An exemplary readable storage medium is coupled to a processor, enabling the processor to read information from and write information to the readable storage medium. Of course, the readable storage medium can also be a component of the processor. The processor and the readable storage medium can reside in an Application Specific Integrated Circuit (ASIC). Alternatively, the processor and the readable storage medium can exist as discrete components in the device.
[0192] The division of units is merely a logical functional division; in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be indirect coupling or communication connection through some interfaces, devices, or units, and may be electrical, mechanical, or other forms.
[0193] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0194] In addition, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.
[0195] If a function is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this invention, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of the various embodiments of this invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0196] Those skilled in the art will understand that all or part of the steps of the above-described method embodiments can be implemented by hardware related to program instructions. The aforementioned program can be stored in a computer-readable storage medium. When executed, the program performs the steps of the above-described method embodiments; and the aforementioned storage medium includes various media capable of storing program code, such as ROM, RAM, magnetic disks, or optical disks.
[0197] Finally, it should be noted that other embodiments of the invention will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This invention is intended to cover any variations, uses, or adaptations of the invention that follow the general principles of the invention and include common knowledge or customary techniques in the art not disclosed herein, and is not limited to the precise structures described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of the invention is limited only by the appended claims.
Claims
1. A primary key management method, characterized in that, include: When the business type of the received business request is detected to be data addition, a target primary key strategy is determined in a pre-defined primary key strategy table based on the business type. The primary key strategy table includes at least one mapping relationship between business types and corresponding primary key strategies. Based on the target primary key strategy, generate the target primary key value corresponding to the business request; The target primary key value is filled into the business data corresponding to the requested business, and the filled business data is inserted into the database.
2. The method according to claim 1, characterized in that, Each primary key strategy includes the corresponding business type, strategy name, current value, and step size. The step of generating the target primary key value corresponding to the requested business based on the target primary key strategy includes: After detecting that the primary key policy is enabled, the target primary key policy is locked. The target primary key value is calculated based on the current value and step size corresponding to the target primary key strategy.
3. The method according to claim 1, characterized in that, The method further includes: Based on the target primary key value, update the current value of the target primary key policy in the primary key policy table.
4. The method according to claim 1, characterized in that, Before determining the target primary key strategy in a pre-defined primary key strategy table based on the business type, the method further includes: Receive the service request sent by the user; Establish a connection with the database based on the business request; After successfully establishing a connection with the database, the primary key strategy table is created.
5. The method according to claim 4, characterized in that, The establishment of the primary key strategy table includes: When the application starts, the business entity objects in the application are scanned to obtain the primary key strategy corresponding to each business entity object; The primary key strategy table is established based on the primary key strategy corresponding to each business entity object.
6. The method according to claim 4, characterized in that, The establishment of the primary key strategy table includes: When the application starts, the business entity objects in the application are scanned to obtain the primary key strategy corresponding to each business entity object; Based on the primary key strategy corresponding to each business entity object, the pre-obtained initial primary key strategy table is updated to obtain the primary key strategy table.
7. The method according to any one of claims 1 to 6, characterized in that, The method further includes: When a business anomaly is detected, the business data and the target primary key value are rolled back.
8. A primary key management device, characterized in that, include: The determination module is used to determine the target primary key strategy in a pre-set primary key strategy table based on the business type when the business type of the received business request is detected to be data addition. The primary key strategy table includes at least one mapping relationship between business type and corresponding primary key strategy. The generation module is used to generate the target primary key value corresponding to the business request based on the target primary key strategy; The insertion module is used to fill the target primary key value into the business data corresponding to the requested business, and then insert the filled business data into the database.
9. An electronic device, characterized in that, include: A processor, and a memory communicatively connected to the processor; The memory stores computer-executed instructions; The processor executes computer execution instructions stored in the memory, causing the processor to perform the method as described in any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, which, when executed by a processor, are used to implement the primary key management method as described in any one of claims 1 to 7.