Query construction method, system and device based on entity state awareness and medium
By using an entity state awareness mechanism and Java reflection to create default instances, the code redundancy and error-proneness issues in the construction of dynamic query conditions in existing technologies are resolved. This achieves automated null value detection and type-safe query construction, improving development efficiency and code maintainability.
Patent Information
- Application Number
- CN202511358103.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-23
- Publication Date
- 2026-02-03
- Estimated Expiration
- 2045-09-23
AI Technical Summary
Existing technologies suffer from code redundancy, error susceptibility, lack of overall entity object state awareness, and a unified null value handling strategy in the construction of dynamic query conditions, resulting in low development efficiency and poor code maintainability.
Through an entity state awareness mechanism, a default instance is created using Java reflection, and it is determined whether the entity object is blank. Its fields are traversed and database query conditions are constructed based on a predefined null value judgment strategy. SFunction is used to implement compile-time type safety and chained call interface.
Automated null value detection reduces code volume, eliminates spelling errors, improves development efficiency and code readability, supports flexible query schemes, and enhances practicality in complex business scenarios.
Smart Images

Figure CN120849677B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of data processing technology, specifically relating to a query construction method, system, device, and medium based on entity state awareness. Background Technology
[0002] In current enterprise application development, using ORM frameworks such as MyBatis-Plus for data persistence has become the industry standard. However, these frameworks have significant limitations in constructing dynamic query conditions. Developers need to manually write a large amount of redundant code to check for null values: for each query field, an explicit if statement must be used to check if its value is not null before the condition constructor method can be called, resulting in bloated and error-prone code. Furthermore, field names are passed as strings, lacking compile-time type safety guarantees, making them prone to runtime exceptions due to spelling errors. Simultaneously, existing technologies lack the ability to perceive the overall state of entity objects, cannot intelligently skip entities with completely empty fields, and do not provide a unified null value handling strategy or chained call interface. These problems severely restrict development efficiency and code maintainability in complex query scenarios, urgently requiring a new solution to overcome them. Summary of the Invention
[0003] In view of the above-mentioned shortcomings of the prior art, the present invention provides a query construction method, system, device and medium based on entity state awareness to solve the above-mentioned technical problems.
[0004] In a first aspect, the present invention provides a query construction method based on entity state awareness, comprising:
[0005] It receives an entity object as the source of query conditions.
[0006] A default instance of the entity object is created using reflection.
[0007] The entity object is compared with the default instance to determine whether the entity object is a blank entity, and the corresponding status flag is set.
[0008] If the entity object is not a blank entity, then its fields are traversed;
[0009] Based on the current value of the field and the predefined null value judgment strategy, construct the corresponding database query conditions.
[0010] In an optional implementation, the entity object is a parameter object that is independent of the persistent entity domain model and is dedicated to transmitting query conditions.
[0011] Each member variable in this parameter object corresponds to an optional query condition;
[0012] The value state of the member variables is used to determine whether and how the final query condition clause is generated.
[0013] In an optional implementation, a default instance of the entity object is created via reflection, including:
[0014] Obtain the parameterless constructor of the entity object class using the Java reflection API;
[0015] Calling the setAccessible(true) method ensures that the parameterless constructor is accessible;
[0016] Calling the newInstance() method of the parameterless constructor instantiates a new object. All field values of this new object are in the initial state of their type, which is the default instance.
[0017] In an optional implementation, the entity object is compared with the default instance to determine whether the entity object is a blank entity, and a corresponding status flag is set, including:
[0018] Take the entity object and the default instance as input;
[0019] The equals() method of the entity object is called to perform an overall equivalence comparison between the entity object and the default instance;
[0020] If the equals() method returns true, the entity object is determined to be a blank entity, and the status flag blankEntity is set to true;
[0021] If the equals() method returns false, or an exception occurs during the comparison process, the entity object is determined to be a non-blank entity, and the status flag blankEntity is set to false.
[0022] The status flag blankEntity is used to control the execution logic of all subsequent condition construction methods; when it is true, all condition construction steps are skipped.
[0023] In an optional implementation, the equals() method of the entity object is called to perform an overall equivalence comparison between the entity object and the default instance, including:
[0024] The class to which the entity object belongs overrides the equals(Object obj) method inherited from the Object class;
[0025] The internal logic of the overridden equals() method is as follows: iterate through all fields in the class and recursively or at the field level compare whether the values of corresponding fields of two objects are equal;
[0026] If all field values are equal, return true; otherwise, return false.
[0027] The overall equivalence comparison is a semantic equivalence judgment performed on the entity object and the default instance based on all field values.
[0028] In an optional implementation, based on the current value of the field and a predefined null value judgment strategy, corresponding database query conditions are constructed, including:
[0029] Based on the data type of the field, select the corresponding null value judgment strategy from the predefined strategy set;
[0030] Execute the selected strategy and determine whether the current value of the field meets the requirements for constructing the query conditions;
[0031] If the condition is met, the corresponding condition building operation is triggered, adding a condition clause to the query condition builder;
[0032] The predefined null value detection strategy includes at least the following:
[0033] For string type fields, the strategy is to check whether the value is a non-empty string, that is, to use the StringUtils.isNotBlank(value) method to check;
[0034] For collection type fields, the strategy is to check whether the value is a non-empty collection, that is, to use the CollectionUtils.isNotEmpty(value) method to check;
[0035] For other object type fields, the strategy is to check if their value is not null.
[0036] In an optional implementation, the corresponding condition building operation is triggered to add a condition clause to the query condition builder, including:
[0037] Based on the SQL semantics implied by the method name of the currently executed condition builder, the corresponding method of the underlying query condition builder (such as MyBatis-Plus's QueryWrapper) is called.
[0038] The Lambda expression reference (SFunction) of the field and the valid value after null check are passed as parameters to the underlying method;
[0039] The correspondence between the method name of the condition construction method and the SQL semantics includes, but is not limited to:
[0040] The `eqIfPresent` or `equalIfPresent` method triggers the constructor to generate an equal-value query condition clause.
[0041] The likeIfPresent method triggers the constructor to generate a LIKE fuzzy query condition clause.
[0042] The inIfNotEmpty method triggers the constructor to generate an IN range query condition clause;
[0043] The `betweenIfPresent` method triggers the constructor to generate a range query condition clause.
[0044] Secondly, the present invention provides a query construction system based on entity state awareness, comprising:
[0045] The object receiving module is used to receive an entity object as a source of query conditions;
[0046] The reflection creation module is used to create a default instance of the entity object through reflection.
[0047] The blank judgment module is used to compare the entity object with the default instance to determine whether the entity object is a blank entity and set the corresponding status flag.
[0048] The field traversal module is used to traverse the fields of the entity object if the entity object is not a blank entity;
[0049] The query construction module is used to construct corresponding database query conditions based on the current value of the field and a predefined null value judgment strategy.
[0050] Thirdly, a device is provided, comprising:
[0051] Memory for storing entity state-aware query building programs;
[0052] A processor, used to implement the steps of the entity-state-aware query construction method as provided in the first aspect when executing the entity-state-aware query construction program.
[0053] Fourthly, a computer-readable medium is provided, on which an entity-state-aware query builder is stored, wherein when the entity-state-aware query builder is executed by a processor, it implements the steps of the entity-state-aware query builder method provided in the first aspect.
[0054] The beneficial effects of this invention are as follows: the entity state-aware query construction method, system, device, and medium provided by this invention significantly improve the intelligence level of dynamic query construction through an entity state-aware mechanism and a type-adaptive null value judgment strategy. Its beneficial effects are: First, automated null value detection and condition generation completely avoid manual null checks, reducing code volume and improving development efficiency; second, compile-time field validation through SFunction completely eliminates field spelling errors, reducing code error rate; furthermore, a unified processing strategy and chained call interface greatly improve code readability and maintainability, while supporting flexible query schemes such as default value rollback, demonstrating outstanding practical value in complex business scenarios. Attached Figure Description
[0055] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0056] Figure 1 This is a schematic flowchart of a method according to an embodiment of the present invention.
[0057] Figure 2 This is a system component architecture diagram of an embodiment of the present invention.
[0058] Figure 3 This is another illustrative flowchart of a method according to an embodiment of the present invention.
[0059] Figure 4 This is a class relationship UML diagram of an embodiment of the present invention.
[0060] Figure 5 This is a schematic block diagram of a system according to an embodiment of the present invention.
[0061] Figure 6 This is a schematic diagram of the structure of a device provided in an embodiment of the present invention. Detailed Implementation
[0062] To enable those skilled in the art to better understand the technical solutions of this invention, the technical solutions of the embodiments of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this invention, and not all embodiments. Based on the embodiments of this invention, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of this invention.
[0063] 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 invention pertains. The terminology used herein in the description of the invention is for the purpose of describing particular embodiments only and is not intended to be limiting of the invention.
[0064] The entity state-aware query construction method provided in this embodiment of the invention is executed by a computer device, and correspondingly, the entity state-aware query construction system runs on the computer device.
[0065] Figure 1 This is a schematic flowchart illustrating a method according to an embodiment of the present invention. Wherein, Figure 1 The executing entity can be a query building system based on entity state awareness. Depending on different needs, the order of the steps in this flowchart can be changed, and some can be omitted.
[0066] like Figure 1 As shown, the method includes:
[0067] S1. Receive an entity object as the source of query conditions;
[0068] S2. Create a default instance of the entity object using reflection;
[0069] S3. Compare the entity object with the default instance to determine whether the entity object is a blank entity and set the corresponding status flag;
[0070] S4. If the entity object is not a blank entity, then iterate through its fields;
[0071] S5. Based on the current value of the field and the predefined null value judgment strategy, construct the corresponding database query conditions.
[0072] In one embodiment of the present invention, based on step S1, the following will provide a possible embodiment and describe its specific implementation in a non-limiting manner.
[0073] 1. Define a dedicated query parameter object. First, you need to define a dedicated class that is independent of any persistent entity (such as User or Product). This class does not map to any database table; its sole purpose is to encapsulate all possible dynamic query conditions.
[0074] User information query parameter object, independent of the User entity, is dedicated to transmitting query conditions. `public class UserSearchParams {`
[0075] / / Each of the following member variables corresponds to an optional query condition.
[0076] private String userName; / / Condition 1: Username (fuzzy search)
[0077] private Integer minAge; / / Condition 2: Lower limit of age
[0078] private Integer maxAge; / / Condition 3: Maximum age
[0079] private Integer departmentId; / / Condition 4: Department ID
[0080] private List <integer>statusList; / / Condition 5: Status list (multi-value query)
[0081] / / Standard Getter and Setter methods
[0082] public String getUserName() { return userName;}
[0083] public void setUserName(String userName) { this.userName = userName;}
[0084] / / ... other Getter and Setter omitted}
[0085] 2. Receive objects and drive conditional construction
[0086] The instance of the above parameter object is passed as a data source to the core auxiliary class of this invention, thereby initiating the dynamic query construction process.
[0087] / / Receive query parameters (e.g., from frontend requests) at the service layer.
[0088] UserSearchParams params = new UserSearchParams();
[0089] params.setUserName("Zhang"); / / Sets the condition: username contains "Zhang"
[0090] params.setMinAge(20); / / Set the condition: age greater than or equal to 20
[0091] / / maxAge and departmentId are null, and statusList is empty, meaning it is not effective.
[0092] / / Call the method of this invention to receive the query parameter object as the source
[0093] LambdaQueryWrapper <user>wrapper = LambdaQueryWrapperHelper
[0094] .of(params) / / [Receives an entity object as the source of query conditions].
[0095] In one embodiment of the present invention, based on step S2, the following will provide a possible embodiment and describe its specific implementation in a non-limiting manner.
[0096] S201. Obtain the parameterless constructor of the entity object class through the Java reflection API.
[0097] At runtime, the class structure of the query parameter object is manipulated through Java's built-in Reflection API (Application Programming Interface). Specifically, the system locates and retrieves the parameter class's parameterless constructor by calling the `getDeclaredConstructor()` method of the `Class` object and passing in an empty parameter list. This process allows the system to dynamically access its constructor even when the specific class type is unknown.
[0098] S202. Call the setAccessible(true) method to ensure that the parameterless constructor is accessible.
[0099] Because the parameterless constructor may have private access permissions, the system needs to call the `setAccessible(true)` method. This operation aims to remove the constraints of Java's access control checks, ensuring that the constructor can be successfully called subsequently, thus guaranteeing the smooth execution of reflection operations. This is a crucial step in accessing non-public constructors.
[0100] S203. Call the newInstance() method of the parameterless constructor to instantiate a new object. All field values of the new object are in the initial state of their type, which is the default instance.
[0101] The `newInstance()` method, which has been made accessible with a no-argument constructor, is invoked. This call triggers the JVM (Java Virtual Machine) to perform the same instantiation process as when using the `new` keyword, creating a brand new instance of the class in the heap memory. All fields of this new instance are automatically initialized by the JVM to their corresponding type default values (e.g., null for reference types, 0 for integers, and false for booleans). This resulting clean instance is defined as the "default instance" and will serve as the baseline object for subsequent state comparisons with user-provided query parameter objects.
[0102] In one embodiment of the present invention, based on step S3, the following will provide a possible embodiment and describe its specific implementation in a non-limiting manner.
[0103] First, the user-provided query parameter object is compared with the default instance created in the aforementioned reflection step. The system relies on a pre-defined premise: the class to which the query parameter object belongs must have overridden the `equals()` method inherited from the `Object` base class. This overridden method is not a simple memory address comparison, but rather includes deep, field-content-based comparison logic. Internally, it iterates through all declared important fields in the class, recursively or directly comparing the values of each field in the two instances to ensure they are completely identical. If all field values are determined to be equal, the method returns `true`, indicating that the two objects are semantically equivalent; otherwise, it returns `false`.
[0104] The system then makes a decision based on the return value of the `equals()` method. If the return value is `true`, the system determines that the query parameter object passed by the user is a "blank entity," meaning that all its field values are in their initial state and do not contain any valid query conditions. In this case, the system sets an internal state flag `blankEntity` to `true`. Conversely, if the return value is `false`, or if an exception is unexpectedly thrown during the comparison, the system adopts a conservative strategy, setting the `blankEntity` flag to `false`, determining that the entity is a non-blank entity and may contain valid query conditions.
[0105] Ultimately, the blankEntity status flag will serve as the core control signal for the entire subsequent process. When it is true, the system will short-circuit and skip all subsequent condition building steps, directly returning an empty query builder without any conditions, thereby avoiding the generation of unnecessary full table queries and improving system performance and logical rationality.
[0106] In one embodiment of the present invention, based on step S5, a possible embodiment will be given below, and its specific implementation will be described in a non-limiting manner.
[0107] First, the system identifies the data type of the field to be processed. Based on this type, the system selects a matching null value judgment strategy from a predefined strategy set. This strategy set is a built-in, standardized processing rule library for different data types.
[0108] Once a strategy is selected, the system immediately executes it to determine the validity of the field's current value. For string fields, the strategy requires the value to be a valid string containing non-whitespace characters, typically checked by calling the `StringUtils.isNotBlank(value)` utility method. For collection or array fields, the strategy requires the collection to contain at least one element, typically checked by calling the `CollectionUtils.isNotEmpty(value)` utility method. For all other object types, the strategy is the most basic: it checks if the field's reference value is not null.
[0109] If the field value is determined to meet the strategy requirements, a condition construction operation is triggered. This operation translates the implicit SQL semantics of the current condition construction method into a specific call to the underlying query condition builder (such as MyBatis-Plus's QueryWrapper). The system calls the corresponding method of the builder based on the mapping relationship between the method name and the SQL semantics. For example, when a user calls the eqIfPresent method, the system will call the QueryWrapper.eq() method to generate an equality query (=) condition clause; calling the likeIfPresent method will trigger the generation of a fuzzy query (LIKE) condition clause; similarly, the inIfNotEmpty and betweenIfPresent methods generate range query (IN) and interval query (BETWEEN...AND...) condition clauses, respectively.
[0110] When calling the underlying method, the system passes two key parameters: first, the field reference obtained through the lambda expression (SFunction), ensuring compile-time type safety; and second, the valid value obtained after the aforementioned null value check. Finally, the underlying framework completes the concatenation of the SQL condition clauses, thereby achieving automated, type-safe, and intelligent conversion from object fields to query conditions.
[0111] In one embodiment of the present invention, the system architecture is as follows: Figure 2 As shown, the main core modules work together as follows:
[0112] 1. Entity State Awareness Module: This module serves as the system's entry point and scheduling center, responsible for receiving user-input query entity objects (such as UserQuery). Internally, it creates a default instance of the entity class using reflection tools such as ClassUtils.newInstance(), and performs deep field comparisons using the overridden equals() method to determine if the entity is in a blank entity state. This flag controls whether the entire query construction process continues.
[0113] 2. Dynamic Condition Processing Module: This module contains the core logic. If the entity is not blank, this module will take over the processing flow. It contains multiple type-adaptive condition processors (such as string processors, collection processors, and general object processors). The processors determine the validity of field values based on predefined null value strategies (such as using `isNotBlank` for strings). For valid values, the processor will generate corresponding condition fragments.
[0114] 3. Query Constructor Encapsulation Module: This module receives the fragments generated by the condition handler and converts them into concrete calls to the underlying MyBatis-PlusQueryWrapper (such as .eq(), .like(), .in()) through a chained call engine. This module provides chained method interfaces such as eqIfPresent() and inIfNotEmpty() for developers to call, and returns the final constructed QueryWrapper object.
[0115] Workflow: After a user calls `LambdaQueryWrapperHelper.of(queryEntity)`, the request first enters the entity state awareness module for state determination. If the entity is empty, an empty wrapper is returned directly; otherwise, it is handed over to the dynamic condition processing module. The processing module calls different condition handlers as needed. After the handler makes its decision, it sends the generated condition instructions to the query builder encapsulation module. This module ultimately interacts with the MyBatis-Plus native `QueryWrapper` to complete the construction of dynamic SQL conditions and return the results.
[0116] The specific implementation of this invention is achieved through a core helper class, LambdaQueryWrapperHelper. <t>The implementation is similar to LambdaQueryWrapper in the MyBatis-Plus framework. <t>Collaborative relationships such as Figure 4 As shown, the auxiliary class encapsulates the entity state perception and dynamic condition construction logic.
[0117] Combination Figure 3 The process and method described herein are implemented according to the following steps:
[0118] Step 1: Initialization and State Awareness. The user calls the static factory method `LambdaQueryWrapperHelper.of(entity)`, passing in the query entity (e.g., `UserQuery`). The system initializes a `LambdaQueryWrapperHelper` instance, which internally holds the native `LambdaQueryWrapper` instance from MyBatis-Plus. Immediately afterwards, the entity state awareness process begins: a default instance with all fields initialized is created using reflection (e.g., `ClassUtils.newInstance(entity.getClass())`), and the passed-in entity is compared with this default instance using the overridden `equals()` method of the entity class. If all field values are equal, the internal state flag `blankEntity` is set to `true`; otherwise, it is set to `false`.
[0119] Step Two: Decision-Making and Process Branching. For example... Figure 3 As shown in the decision node, the system checks the `blankEntity` flag. If it's true, the system determines that the current entity does not contain any valid query conditions, directly returns an empty `LambdaQueryWrapper` instance or one without any added conditions, and the process ends. If it's false, the system enters the dynamic condition construction loop.
[0120] Step 3: Dynamically constructing the loop. In this stage, the user declares their query intent through chained API calls (e.g., `.eqIfPresent(...).inIfNotEmpty(...)`). For each method call, the system executes the following sub-process (taking `eqIfPresent(User::getName)` as an example):
[0121] Field value extraction and judgment: The value of the corresponding field (name) in the entity is obtained through the SFunction functional interface.
[0122] Apply a null value strategy: Select a predefined strategy based on the field type for judgment. For a String type name field, use StringUtils.isNotBlank(value) to check; if it is not null, it is considered a valid value.
[0123] Condition construction and injection: If the value is valid, the system automatically calls the internally encapsulated LambdaQueryWrapper.eq(column, value) method to add the corresponding name = 'value' condition clause to the final SQL query conditions. This process is fully automated and requires no manual null checks by the user.
[0124] Step 4: Returning the result. After all chained calls are complete, the user calls the `build()` method, which returns the final, fully constructed LambdaQueryWrapper containing all dynamic conditions. <t>The object can be directly passed to the MyBatis-Plus Mapper to execute queries.
[0125] In some embodiments, the entity-state-aware query construction system may include multiple functional modules composed of computer program segments. The computer programs for each program segment in the entity-state-aware query construction system may be stored in the memory of a computer device and executed by at least one processor to perform (see details). Figure 1 (Description) Functionality for building queries based on entity state awareness.
[0126] In this embodiment, the entity state-aware query construction system can be divided into multiple functional modules based on its functions, such as... Figure 5 As shown. The module referred to in this invention is a series of computer program segments that can be executed by at least one processor and perform a fixed function, and is stored in memory. In this embodiment, the functions of each module will be described in detail in subsequent embodiments.
[0127] The object receiving module is used to receive an entity object as a source of query conditions;
[0128] The reflection creation module is used to create a default instance of the entity object through reflection.
[0129] The blank judgment module is used to compare the entity object with the default instance to determine whether the entity object is a blank entity and set the corresponding status flag.
[0130] The field traversal module is used to traverse the fields of the entity object if the entity object is not a blank entity;
[0131] The query construction module is used to construct corresponding database query conditions based on the current value of the field and a predefined null value judgment strategy.
[0132] Figure 6 The entity state-aware query construction method provided in the embodiments of this application can be applied to devices. Those skilled in the art will understand that the device structures involved in the embodiments of this invention do not constitute a limitation on the device. A device may include more or fewer components than illustrated, or combine certain components, or have different component arrangements. In the embodiments of this invention, the device includes, but is not limited to, laptop computers, desktop computers, workbenches, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The device may also represent various forms of mobile devices, such as personal digital processors, cellular phones, smartphones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the embodiments of this application described and / or claimed herein.
[0133] The device 600 may include a processor 610, a memory 620, and a communication unit 630. These components communicate via one or more buses. Those skilled in the art will understand that the server structure shown in the figures does not constitute a limitation of the present invention. It may be a bus topology or a star topology, and may include more or fewer components than shown, or combine certain components, or have different component arrangements.
[0134] The memory 620 can be used to store execution instructions of the processor 610. The memory 620 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. When the execution instructions in the memory 620 are executed by the processor 610, the device 600 is able to perform some or all of the steps in the above method embodiments.
[0135] The processor 610 serves as the control center of the storage device, connecting various parts of the electronic device via various interfaces and lines. It executes software programs and / or modules stored in the memory 620, and calls data stored in the memory to perform various functions of the electronic device and / or process data. The processor can be composed of integrated circuits (ICs), such as a single packaged IC or multiple packaged ICs with the same or different functions connected together. For example, the processor 610 may consist only of a central processing unit (CPU). In this embodiment of the invention, the CPU may have a single processing core or include multiple processing cores.
[0136] The communication unit 630 is used to establish a communication channel, enabling the storage device to communicate with other devices. It can receive user data sent by other devices or send user data to other devices.
[0137] The present invention also provides a computer medium, wherein the computer medium may store a program, which, when executed, may include some or all of the steps provided in the embodiments of the present invention. The medium may be a magnetic disk, an optical disk, read-only memory (ROM), or random access memory (RAM), etc.
[0138] Those skilled in the art will clearly understand that the techniques in the embodiments of the present invention can be implemented using software plus necessary general-purpose hardware platforms. Based on this understanding, the technical solutions in the embodiments of the present invention, or the parts that contribute to the prior art, can be embodied in the form of a software product. This computer software product is stored in a medium such as a USB flash drive, a portable hard drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk, or any other medium capable of storing program code. It includes several instructions to cause a computer device (which may be a personal computer, a server, or a second device, network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention.
[0139] The same or similar parts between the various embodiments in this specification can be referred to mutually. In particular, the device embodiments are basically similar to the method embodiments, so the description is relatively simple, and the relevant parts can be referred to the description in the method embodiments.
[0140] In the embodiments provided by this invention, it should be understood that the disclosed systems and methods can be implemented in other ways. For example, the system embodiments described above are merely illustrative. For instance, the division of modules is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple modules 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 through some interfaces; the indirect coupling or communication connection between systems or modules may be electrical, mechanical, or other forms.
[0141] The modules described as separate components may or may not be physically separate. The components shown as modules may or may not be physical modules; that is, they may be located in one place or distributed across multiple network modules. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs.
[0142] In addition, the functional modules in the various embodiments of the present invention can be integrated into one processing module, or each module can exist physically separately, or two or more modules can be integrated into one module.
[0143] Although the present invention has been described in detail with reference to the accompanying drawings and preferred embodiments, the present invention is not limited thereto. Various equivalent modifications or substitutions can be made to the embodiments of the present invention by those skilled in the art without departing from the spirit and essence of the invention, and such modifications or substitutions should all be within the scope of the present invention. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in the present invention should also be covered within the protection scope of the present invention.< / t> < / t> < / t> < / user> < / integer>
Claims
1. A query construction method based on entity state awareness, characterized in that, include: It receives an entity object as the source of query conditions. A default instance of the entity object is created using reflection. The entity object is compared with the default instance to determine whether the entity object is a blank entity, and the corresponding status flag is set. If the entity object is not a blank entity, then its fields are traversed; Based on the current value of the field and the predefined null value judgment strategy, construct the corresponding database query conditions; Creating a default instance of the entity object using reflection includes: Obtain the parameterless constructor of the entity object class using the Java reflection API; Calling the setAccessible method ensures that the parameterless constructor is accessible; Call the newInstance method of the parameterless constructor to instantiate a new object. All field values of the new object are in the initial state of their type, which is the default instance. The entity object is compared with the default instance to determine whether the entity object is a blank entity, and a corresponding status flag is set, including: Take the entity object and the default instance as input; The equals method of the entity object is called to perform an overall equivalence comparison between the entity object and the default instance; If the equals method returns true, the entity object is determined to be a blank entity, and the status flag blankEntity is set to true; If the equals method returns false, or an exception occurs during the comparison process, the entity object is determined to be a non-blank entity, and the status flag blankEntity is set to false. The status flag blankEntity is used to control the execution logic of all subsequent condition construction methods; when it is true, all condition construction steps are skipped.
2. The method according to claim 1, characterized in that, The entity object is a parameter object that is independent of the persistent entity domain model and is specifically used to transmit query conditions; Each member variable in this parameter object corresponds to an optional query condition; The value state of the member variables is used to determine whether and how the final query condition clause is generated.
3. The method according to claim 1, characterized in that, Call the `equals` method of the entity object to perform an overall equivalence comparison between the entity object and the default instance, including: The class to which the entity object belongs overrides the equals method inherited from the Object class; The internal logic of the overridden equals method is as follows: iterate through all fields in the class and recursively or at the field level compare whether the values of corresponding fields of two objects are equal; If all field values are equal, return true; otherwise, return false. The overall equivalence comparison is a semantic equivalence judgment performed on the entity object and the default instance based on all field values.
4. The method according to claim 1, characterized in that, Based on the current value of the field and the predefined null value judgment strategy, construct the corresponding database query conditions, including: Based on the data type of the field, select the corresponding null value judgment strategy from the predefined strategy set; Execute the selected strategy and determine whether the current value of the field meets the requirements for constructing the query conditions; If the condition is met, the corresponding condition building operation is triggered, adding a condition clause to the query condition builder; The predefined null value detection strategy includes at least the following: For string type fields, the strategy is to check whether the value is a non-empty string, i.e., to use the StringUtils.isNotBlank method to perform the check; For collection type fields, the strategy is to check whether its value is a non-empty collection, that is, to use the CollectionUtils.isNotEmpty method to check; For other object type fields, the strategy is to check if their value is not null.
5. The method according to claim 4, characterized in that, Trigger the corresponding condition building operation to add a condition clause to the query condition builder, including: Based on the SQL semantics implied by the method name of the currently executed condition builder, the corresponding method of the underlying query condition builder is called; The Lambda expression reference SFunction of the field and the valid value after null check are passed as parameters to the underlying method; The correspondence between the method name of the condition construction method and the SQL semantics includes, but is not limited to: The `eqIfPresent` or `equalIfPresent` method triggers the constructor to generate an equal-value query condition clause. The likeIfPresent method triggers the constructor to generate a LIKE fuzzy query condition clause. The inIfNotEmpty method triggers the constructor to generate an IN range query condition clause; The `betweenIfPresent` method triggers the constructor to generate a range query condition clause.
6. A query construction system based on entity state awareness, characterized in that, include: The object receiving module is used to receive an entity object as a source of query conditions; The reflection creation module is used to create a default instance of the entity object through reflection. The blank judgment module is used to compare the entity object with the default instance to determine whether the entity object is a blank entity and set the corresponding status flag. The field traversal module is used to traverse the fields of the entity object if the entity object is not a blank entity; The query construction module is used to construct corresponding database query conditions based on the current value of the field and a predefined null value judgment strategy. Creating a default instance of the entity object using reflection includes: Obtain the parameterless constructor of the entity object class using the Java reflection API; Calling the setAccessible method ensures that the parameterless constructor is accessible; Call the newInstance method of the parameterless constructor to instantiate a new object. All field values of the new object are in the initial state of their type, which is the default instance. The entity object is compared with the default instance to determine whether the entity object is a blank entity, and a corresponding status flag is set, including: Take the entity object and the default instance as input; The equals method of the entity object is called to perform an overall equivalence comparison between the entity object and the default instance; If the equals method returns true, the entity object is determined to be a blank entity, and the status flag blankEntity is set to true; If the equals method returns false, or an exception occurs during the comparison process, the entity object is determined to be a non-blank entity, and the status flag blankEntity is set to false. The status flag blankEntity is used to control the execution logic of all subsequent condition construction methods; when it is true, all condition construction steps are skipped.
7. A query construction device based on entity state awareness, characterized in that, include: Memory for storing entity state-aware query building programs; A processor, configured to implement the steps of the entity-state-aware query construction method as described in any one of claims 1-5 when executing the entity-state-aware query construction program.
8. A computer-readable medium storing a computer program, characterized in that, The readable medium stores an entity state-aware query building program, which, when executed by a processor, implements the steps of the entity state-aware query building method as described in any one of claims 1-5.
Citation Information
Patent Citations
Query object mapping method and device and product
CN118227626A