Database Interaction Method, Device, and Storage Medium
By creating weak entity classes and entity base classes, binding database fields and custom entity class objects, the problem that traditional ORM framework cannot recognize the updated object-field relationship is solved, simplifying the project development process and improving efficiency.
Patent Information
- Application Number
- CN202210011911.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-01-06
- Publication Date
- 2025-06-27
- Estimated Expiration
- 2042-01-06
AI Technical Summary
The traditional ORM framework cannot recognize the correspondence between the updated objects and fields, resulting in the inability to interact with the fields in the database based on the updated objects, increasing the complexity and inefficiency of project development.
By creating weak entity classes and entity base classes, obtain the data table fields in the database and the annotation information of the custom entity class, bind the target fields and custom entity class objects, and make the custom entity class inherit the entity base class to call the calling methods of the weak entity class for database interaction.
It solves the problem that the traditional ORM framework cannot recognize the updated object-field relationship, simplifies the project development process, improves development efficiency, and ensures the stability of custom entity classes.
Smart Images

Figure CN114328467B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of software development, and in particular to a database interaction method, apparatus, device, and storage medium.
Background Art
[0002] Currently, for performing create, update, delete, and query operations in database interaction, an Object Relational Mapping (ORM) framework is usually used.
[0003] The traditional way of using ORM for database interaction includes: generating corresponding objects in an entity class according to the fields in a data table in the database; and interacting with the database according to the corresponding relationship between the fields and the objects.
[0004] However, when developing a large project, the objects in the entity class may be updated. At this time, the objects in the entity class and the fields in the data table are no longer in a one-to-one correspondence relationship. The traditional ORM framework cannot recognize the corresponding relationship between the updated objects and the fields, resulting in the inability to interact with the fields in the database according to the updated objects. Developers need to manually add the corresponding relationship between the updated objects and the fields in the database, which will lead to higher complexity and lower efficiency in project development.
Summary of the Invention
[0005] This application provides a database interaction method, apparatus, device, and storage medium, which can solve the problem that the traditional ORM framework cannot recognize the corresponding relationship between the updated objects and the fields, resulting in the inability to interact with the fields in the database according to the updated objects. Developers need to manually add the corresponding relationship between the updated objects and the fields in the database, which will lead to higher complexity and lower efficiency in project development. This application provides the following technical solutions:
[0006] In a first aspect, a database interaction method is provided. The method includes:
[0007] Create a weak entity class, which encapsulates: call methods corresponding to different operation types, and / or call methods corresponding to specified operation types of specified fields;
[0008] Create an entity base class for inheriting the weak entity class;
[0009] Obtain the data table fields in the database;
[0010] Obtain a custom entity class and the annotation information of the custom entity class;
[0011] Determine a target field that matches the annotation information from the data table fields, and bind the target field to an object in the custom entity class;
[0012] Enable the custom entity class to inherit the entity base class, so as to call the call method encapsulated in the weak entity class through the entity base class;
[0013] Use the custom entity class to interact with the database according to the call method.
[0014] Optionally, the using the custom entity class to interact with the database according to the call method includes:
[0015] Receive a database interaction operation, which is used to indicate an operation on a target object in the custom entity class;
[0016] Determine the field to be operated on corresponding to the target object based on the binding relationship between the target field and the object;
[0017] Operate on the field to be operated on based on the call method corresponding to the database interaction operation.
[0018] Optionally, the call method in the weak entity class is used for:
[0019] Delete columns with null fields in the data table, delete other columns in the data table except the reserved columns, set the column values in the data table, obtain the column values of sensitive fields in the data table, delete the attributes of the data table, and / or add the attributes of the data table.
[0020] Optionally, the call method in the weak entity class is further used for:
[0021] Perform insert, delete, update, and query operations on the data table.
[0022] Optionally, the data table fields include:
[0023] The table name of the data table and the primary key field of the data table.
[0024] In a second aspect, a database interaction device is provided, and the device includes:
[0025] A first creation module, configured to create the weak entity class, and the weak entity class encapsulates: call methods corresponding to different operation types, and / or call methods corresponding to specified operation types for specified fields;
[0026] A second creation module, configured to create an entity base class, and the entity base class is used to inherit the weak entity class;
[0027] The first acquisition module is used to acquire the data table fields in the database;
[0028] The second acquisition module is used to acquire the custom entity class and the annotation information of the custom entity class;
[0029] The field binding module is used to determine the target fields that match the annotation information from the data table fields, and bind the target fields to the objects in the custom entity class;
[0030] The method call module is used to make the custom entity class inherit the entity base class, so as to call the call method encapsulated in the weak entity class through the entity base class;
[0031] The database interaction module is used to interact with the database using the custom entity class according to the call method.
[0032] Optionally, the database interaction module is used to:
[0033] Receive a database interaction operation, where the database interaction operation is used to indicate an operation on a target object in the custom entity class;
[0034] Determine the fields to be operated on corresponding to the target object based on the binding relationship between the target fields and the object;
[0035] Operate on the fields to be operated on based on the call method corresponding to the database interaction operation.
[0036] Optionally, the call method in the weak entity class is used to:
[0037] Delete the columns in the data table where the fields are null, delete the other columns in the data table except the reserved columns, set the column values in the data table, obtain the column values of the sensitive fields in the data table, delete the attributes of the data table, and / or add the attributes of the data table.
[0038] In a third aspect, an electronic device is provided, and the device includes a processor and a memory; a program is stored in the memory, and the program is loaded and executed by the processor to implement the database interaction method described in the first aspect.
[0039] In a fourth aspect, a computer-readable storage medium is provided, and a program is stored in the storage medium, and when the program is executed by a processor, it is used to implement the database interaction method described in the first aspect.
[0040] The beneficial effects of the present application are: by creating a weak entity class, the weak entity class encapsulates: calling methods corresponding to different operation types, and / or calling methods corresponding to the specified operation type of the specified field; creating an entity base class, the entity base class is used to inherit the weak entity class; obtaining the data table field in the database; obtaining the annotation information of the custom entity class and the custom entity class; determining the target field that matches the annotation information from the data table field, and binding the target field to the object in the custom entity class; making the custom entity class inherit the entity base class to call the calling method encapsulated in the weak entity class through the entity base class; using the custom entity class to interact with the database according to the calling method. It can solve the problem that the traditional ORM framework cannot recognize the correspondence between the updated object and the field, resulting in the inability to interact with the field in the database according to the updated object, and the developer needs to manually add the correspondence between the updated object and the field in the database, which will lead to high complexity and low efficiency during project development. Custom entity classes can flexibly specify the mapping relationship between custom entity class objects and data table fields by configuring annotation information, eliminating the one-to-one correspondence with the data table. At the same time, they can match any data table by changing the annotation information, thus simplifying the complexity of project development and improving the efficiency of project development.
[0041] In addition, since the custom entity class inherits the entity base class, even if the fields of the data table are changed, the custom entity can remain unchanged and will not affect the interaction of the custom entity class with the database, ensuring the stability of the custom entity class.
[0042] In addition, when interacting with the database, even if the fields of the data table are modified, the corresponding fields of the data table and the corresponding contents of the fields can also be mapped to the custom entity class with a certain relationship.
[0043] In addition, since this database interaction method is based on the ORM concept, it facilitates the interaction with the database, avoids the occurrence of a large number of SQL statements, and improves the convenience of project development.
[0044] In addition, due to the ORM mapping concept, the readability of the business logic code is enhanced, and the structure of the data table can be understood by viewing the corresponding custom entity class.
[0045] In addition, due to the calling method encapsulated by the weak entity class, the column value of the sensitive field can be obtained and the column value of any field can be set during database interaction.
Brief Description of the Drawings
[0046] Figure 1 This is a flowchart of a database interaction method provided by an embodiment of the present application;
[0047] Figure 2 is a block diagram of a database interaction device provided by an embodiment of the present application;
[0048] Figure 3 is a structural diagram of an electronic device for a database interaction method provided by an embodiment of the present application.
Specific Embodiments
[0049] The technical solutions of the present application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are some, but not all, of the embodiments of the present application. The present application will be described in detail below with reference to the drawings and in conjunction with the embodiments. It should be noted that, without conflict, the embodiments in the present application and the features in the embodiments may be combined with each other.
[0050] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and do not necessarily need to be used to describe a specific order or sequence.
[0051] In the present application, unless otherwise stated, the orientation terms such as "upper", "lower", "top", "bottom" are usually in the direction shown in the drawings, or in the vertical, perpendicular or gravitational direction of the component itself; similarly, for the convenience of understanding and description, "inner" and "outer" refer to the inner and outer of the contour of each component itself, but the above orientation terms do not limit the present application.
[0052] The database interaction method provided by the present application will be introduced in detail below.
[0053] This embodiment provides a database interaction method, as Figure 1 shown, the method at least includes the following steps:
[0054] Step 101, create a weak entity class, which encapsulates: call methods corresponding to different operation types, and / or call methods corresponding to specified operation types of specified fields;
[0055] Among them, the call methods corresponding to different operation types are used to perform operations on the data table including addition, deletion, modification, and query.
[0056] Specifically, the addition, deletion, modification, and query operations include operating on a specified data table to add field content, operating on a specified data table to delete field content, operating on a specified data table to modify field content, and operating on a specified data table to query field content.
[0057] Among them, the calling methods corresponding to the specified operation types of the specified fields include deleting columns with null values in the data table, deleting other columns in the data table except the reserved columns, setting column values in the data table, obtaining column values of sensitive fields in the data table, deleting attributes of the data table, and / or adding attributes of the data table.
[0058] During actual operation, methods can be encapsulated in the weak entity class according to specific requirements.
[0059] Step 102, create an entity base class, which is used to inherit the weak entity class.
[0060] Step 103, obtain the data table fields in the database.
[0061] Specifically, the data table fields in the database include the table name of the data table and the primary key field of the data table.
[0062] Step 104, obtain the custom entity class and the annotation information of the custom entity class.
[0063] Specifically, the annotation information is configured with the table name and primary key field of the data table.
[0064] Step 105, determine the target fields that match the annotation information from the data table fields, and bind the target fields to the objects in the custom entity class.
[0065] In this embodiment, binding to any data table can be achieved by changing the annotation information.
[0066] Step 106, make the custom entity class inherit the entity base class, so as to call the calling methods encapsulated in the weak entity class through the entity base class.
[0067] Among them, since the entity base class inherits the weak entity class, the entity base class contains the calling methods in the weak entity class.
[0068] Specifically, the calling methods include the calling methods corresponding to different operation types, and / or the calling methods corresponding to the specified operation types of the specified fields.
[0069] Step 107, use the custom entity class to interact with the database according to the calling methods.
[0070] Specifically, using the custom entity class to interact with the database according to the calling methods includes the following steps:
[0071] Step 1, receive a database interaction operation, which is used to indicate an operation on the target object in the custom entity class;
[0072] Step 2, determine the fields to be operated on corresponding to the target object based on the binding relationship between the target fields and the object;
[0073] Step 3: Operate the fields to be operated based on the calling method corresponding to the database interaction operation.
[0074] Optionally, the operation on the field to be operated includes: deleting columns in the data table whose fields are null, deleting other columns in the data table except reserved columns, setting column values in the data table, obtaining column values of sensitive fields in the data table, deleting attributes of the data table, and / or adding attributes of the data table.
[0075] Optionally, operating the field to be operated also includes: adding field content operation to the specified data table, deleting field content operation to the specified data table, modifying field content operation to the specified data table, and querying field content operation to the specified data table.
[0076] In summary, the database interaction method provided in this embodiment creates a weak entity class, which encapsulates: calling methods corresponding to different operation types and / or calling methods corresponding to specified operation types of specified fields; creates an entity base class, which is used to inherit the weak entity class; obtains data table fields in the database; obtains custom entity classes and annotation information of custom entity classes; determines the target field that matches the annotation information from the data table fields, and binds the target field to the object in the custom entity class; enables the custom entity class to inherit the entity base class, so as to call the calling method encapsulated in the weak entity class through the entity base class; and uses the custom entity class to interact with the database according to the calling method. This can solve the problem that the traditional ORM framework cannot recognize the correspondence between the updated object and the field, resulting in the inability to interact with the fields in the database according to the updated object, and the developer needs to manually add the correspondence between the updated object and the field in the database, which will lead to high complexity and low efficiency during project development. Custom entity classes can flexibly specify the mapping relationship between custom entity class objects and data table fields by configuring annotation information, eliminating the one-to-one correspondence with the data table. At the same time, they can match any data table by changing the annotation information, thus simplifying the complexity of project development and improving the efficiency of project development.
[0077] In addition, since the custom entity class inherits the entity base class, even if the fields of the data table are changed, the custom entity can remain unchanged and will not affect the interaction of the custom entity class with the database, ensuring the stability of the custom entity class.
[0078] In addition, when interacting with the database, even if the fields of the data table are modified, the corresponding fields of the data table and the corresponding contents of the fields can also be mapped to the custom entity class with a certain relationship.
[0079] In addition, since this database interaction method is based on the ORM concept, it simplifies the interaction with the database, avoids the appearance of a large number of SQL statements, and improves the convenience during project development.
[0080] In addition, due to the mapping concept of ORM, the readability of the business logic code is enhanced, and the structure of the data table can be understood by viewing the corresponding custom entity class.
[0081] In addition, due to the calling methods encapsulated in the weak entity class, the column values of sensitive fields can be obtained during database interaction, and the column values of any field can be set.
[0082] Figure 2 It is a block diagram of a database interaction device provided by an embodiment of the present application. The device at least includes the following modules: a first creation module 210, a second creation module 220, a first acquisition module 230, a second acquisition module 240, a field binding module 250, a method call module 260, and a database interaction module 270.
[0083] The first creation module 210 is used to create a weak entity class, and the weak entity class encapsulates: calling methods corresponding to different operation types, and / or calling methods corresponding to specified operation types of specified fields;
[0084] The second creation module 220 is used to create an entity base class, and the entity base class is used to inherit the weak entity class;
[0085] The first acquisition module 230 is used to acquire the data table fields in the database;
[0086] The second acquisition module 240 is used to acquire the custom entity class and the annotation information of the custom entity class;
[0087] The field binding module 250 is used to determine the target fields that match the annotation information from the data table fields, and bind the target fields to the objects in the custom entity class;
[0088] The method call module 260 is used to make the custom entity class inherit the entity base class, so as to call the calling methods encapsulated in the weak entity class through the entity base class;
[0089] The database interaction module 270 is used to interact with the database using the custom entity class according to the calling method.
[0090] Specifically, the database interaction module receives a database interaction operation, and the database interaction operation is used to indicate an operation on a target object in the custom entity class;
[0091] Based on the binding relationship between the target field and the object, determine the field to be operated corresponding to the target object;
[0092] Operate the field to be operated based on the call method corresponding to the database interaction operation.
[0093] This embodiment provides an electronic device, such as Figure 3 As shown, the electronic device at least includes a processor 301 and a memory 302.
[0094] The processor 301 may include one or more processing cores, such as: a 4-core processor, an 8-core processor, etc. The processor 301 may be implemented in at least one of the following hardware forms: DSP (Digital Signal Processing), FPGA (Field-Programmable Gate Array), and PLA (Programmable Logic Array). The processor 301 may also include a main processor and a coprocessor. The main processor is a processor for processing data in the wake state, also known as the CPU (Central Processing Unit); the coprocessor is a low-power processor for processing data in the standby state. In some embodiments, the processor 301 may be integrated with a GPU (Graphics Processing Unit), and the GPU is responsible for rendering and drawing the content to be displayed on the display screen. In some embodiments, the processor 301 may further include an AI (Artificial Intelligence) processor, and the AI processor is used to process computational operations related to machine learning.
[0095] The memory 302 may include one or more computer-readable storage media, and the computer-readable storage media may be non-transitory. The memory 302 may also include high-speed random access memory and non-volatile memory, such as one or more disk storage devices and flash storage devices. In some embodiments, the non-transitory computer-readable storage medium in the memory 302 is used to store at least one instruction, and the at least one instruction is used to be executed by the processor 301 to implement the database interaction method provided in the method embodiment of the present application.
[0096] In some embodiments, the electronic device may further optionally include: a peripheral device interface and at least one peripheral device. The processor 301, the memory 302, and the peripheral device interface may be connected through a bus or signal lines. Each peripheral device may be connected to the peripheral device interface through a bus, signal lines, or a circuit board. Schematically, the peripheral devices include but are not limited to: a radio frequency circuit, a touch display screen, an audio circuit, and a power supply, etc.
[0097] Of course, the electronic device may also include fewer or more components, which is not limited in this embodiment.
[0098] Optionally, the present application also provides a computer-readable storage medium, in which a program is stored, and the program is loaded and executed by a processor to implement the database interaction method in the above method embodiment.
[0099] The technical features of the above-described embodiments can be combined arbitrarily. For the sake of brevity of description, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, it should be considered as the scope described in this specification.
[0100] The above-described embodiments only represent several implementation manners of the present application. The description is relatively specific and detailed, but it should not be construed as a limitation on the scope of the invention patent. It should be noted that for those of ordinary skill in the art, without departing from the concept of the present application, several modifications and improvements can be made, and these all belong to the protection scope of the present application. Therefore, the protection scope of the patent of the present application shall be subject to the appended claims.
Claims
1. A database interaction method, characterized in that, The method includes: Creating a weak entity class, which encapsulates: call methods corresponding to different operation types, and / or call methods corresponding to specified operation types for specified fields; Creating an entity base class, which is used to inherit the weak entity class; Obtaining the data table fields in the database; Obtaining a custom entity class and the annotation information of the custom entity class; Determining target fields that match the annotation information from the data table fields, and binding the target fields to the objects in the custom entity class; Making the custom entity class inherit the entity base class, so as to call the call methods encapsulated in the weak entity class through the entity base class; Using the custom entity class to interact with the database according to the call methods, including: receiving a database interaction operation, which is used to indicate an operation on a target object in the custom entity class; determining the fields to be operated corresponding to the target object based on the binding relationship between the target fields and the objects; operating on the fields to be operated based on the call methods corresponding to the database interaction operation.
2. The method according to claim 1, wherein The call methods in the weak entity class are used for: Deleting columns with null values in the data table, deleting other columns in the data table except the reserved columns, setting the column values in the data table, obtaining the column values of sensitive fields in the data table, deleting the attributes of the data table, and / or adding the attributes of the data table.
3. The method according to claim 1, characterized in that, The call methods in the weak entity class are also used for: Performing insert, delete, update, and query operations on the data table.
4. The method according to claim 1, wherein The data table fields include: The table name of the data table and the primary key field of the data table.
5. A database interaction device, characterized in that, The device includes: A first creation module, which is used to create a weak entity class, which encapsulates: call methods corresponding to different operation types, and / or call methods corresponding to specified operation types for specified fields; A second creation module, which is used to create an entity base class, which is used to inherit the weak entity class; A first acquisition module, which is used to obtain the data table fields in the database; A second acquisition module, which is used to obtain a custom entity class and the annotation information of the custom entity class; A field binding module, which is used to determine target fields that match the annotation information from the data table fields, and bind the target fields to the objects in the custom entity class; A method call module, which is used to make the custom entity class inherit the entity base class, so as to call the call methods encapsulated in the weak entity class through the entity base class; A database interaction module, which is used to use the custom entity class to interact with the database according to the call methods, including: receiving a database interaction operation, which is used to indicate an operation on a target object in the custom entity class; determining the fields to be operated corresponding to the target object based on the binding relationship between the target fields and the objects; operating on the fields to be operated based on the call methods corresponding to the database interaction operation.
6. The device according to claim 5, characterized in that, The call methods in the weak entity class are used for: Delete columns in the data table where the fields are null, delete other columns in the data table except the reserved columns, set the column values in the data table, obtain the column values of sensitive fields in the data table, delete the attributes of the data table, and / or add attributes to the data table.
7. An electronic device, characterized in that, The device includes a processor and a memory; a program is stored in the memory, and the program is loaded and executed by the processor to implement the database interaction method according to any one of claims 1 to 4.
8. A computer-readable storage medium, characterized in that, A program is stored in the storage medium, and when the program is executed by a processor, it is used to implement the database interaction method according to any one of claims 1 to 4.