Database field coding translation method based on section
By adopting an aspect-based database field encoding and translation method, and utilizing AOP aspects and annotation mechanisms to achieve automated encoding and translation, the problems of non-intrusiveness, reusability, and consistency in database field translation are solved, thereby improving the development efficiency and maintainability of the system.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- CHENGDU FANGLIAN CLOUD CODE TECH CO LTD
- Filing Date
- 2026-01-06
- Publication Date
- 2026-04-17
AI Technical Summary
Existing technologies lack non-intrusive, reusable, and loosely coupled database field encoding and translation mechanisms, resulting in low development efficiency, high maintenance costs, and data inconsistency, especially in microservice architectures where the same fields frequently require automated translation.
An aspect-based database field encoding translation method is adopted. AOP aspects are used to automatically trigger translation before data is returned. The fields that need to be translated are identified by method-level and field-level annotations, and the field values are obtained and updated using reflection. The translation service is called to achieve automated encoding translation.
It achieves centralized management of encoding and translation, reduces code redundancy, improves system maintainability and data consistency, supports multiple translation types, reduces inconsistencies between front-end and back-end logic, and improves development efficiency and system scalability.
Smart Images

Figure CN121880443A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of database access and data presentation technology, and in particular to an aspect-based database field encoding translation method. Background Technology
[0002] In modern information system development, database fields often use encoding (such as numbers and abbreviations) to store data. For example, "0" represents "disabled" and "1" represents "enabled"; "M" represents "male" and "F" represents "female". This encoding method is beneficial for data storage compactness and query efficiency.
[0003] However, when displaying information on the front end or processing business logic, users typically need to see readable text information (such as "enabled" or "disabled"). Therefore, it's necessary to translate the encoded fields in the database into corresponding Chinese or English descriptions. Currently, the common implementation methods are mainly as follows: (1) Using CASE WHEN or JOIN dictionary tables in SQL queries: This involves writing complex SQL statements to convert the code to text at the database level. The disadvantages of this approach are: the SQL statements are verbose and have poor maintainability, and the business semantics are coupled into the SQL, which is not conducive to code reuse and change management. (2) Manual conversion at the business logic layer: In the backend service, the query results are translated field by field using if-else, switch-case, or Map mapping. Although this method is flexible, it involves a lot of duplicate code, violates the principle of "write once, use everywhere", and requires modification of multiple business code sections when the coding rules change, resulting in high maintenance costs. (3) Translation on the front end: The encoding is passed to the front end and translated by the front-end JavaScript or template engine. This method increases the burden on the front end and may lead to inconsistencies in encoding rules between the front end and the back end, posing security risks (such as exposing sensitive encoding logic).
[0004] In summary, existing technologies lack a non-intrusive, reusable, and loosely coupled database field encoding translation mechanism. Especially in microservice architectures or large systems, the same fields (such as status codes) frequently appear in multiple services, requiring a technical solution that can automatically and transparently perform encoding translation to improve development efficiency, reduce maintenance costs, and ensure data consistency. Therefore, it is necessary to propose a new technical solution to address these issues. Summary of the Invention
[0005] To address the aforementioned technical problems, this invention provides a section-based database field encoding and translation method to solve the frequent and repetitive encoding and translation tasks in the system.
[0006] This invention is achieved using the following technical solution: An aspect-based database field encoding translation method includes the following steps: Step S1: Configure AOP aspects according to the front-end request. When any method marked by a method-level annotation completes execution and returns normally, the configured aspect will be automatically triggered. Step S2: Obtain the returned data information through the aspect, and use the reflection loop mechanism to traverse the returned value to obtain all fields in a single data item; Step S3: Determine if a field-level annotation exists. If it exists, it is identified as the source encoded field that needs to be translated, the field-level annotation attribute information is obtained, and the corresponding translation service is called. If it does not exist, return to step S2. Step S4: Again, use reflection to write the translated readable text result into the target field specified in the returned object; Step S5: Repeat steps S1 to S4 until all fields marked with field-level annotations in the returned object have been processed. The automated translation process is complete, and a complete data object with all specified encoded fields translated is obtained and can be directly returned to the client.
[0007] Specifically, in step S1, method-level annotations are used to mark service methods that need to perform field encoding translation, serving as triggering conditions for AOP aspects. This limits the aspects to only apply to methods annotated with method-level annotations, avoiding indiscriminate interception of all methods.
[0008] Specifically, in step S2, the returned data information is the return value of the interception method obtained through the JoinPoint object, which is the core data object to be processed.
[0009] Specifically, in step S3, field-level annotations are used to mark specific fields of DTO data transfer objects or entity classes, identifying the field as the target field that needs to be encoded and translated. The attributes of field-level annotations that guide the translation process include: targetCode(): Specifies the encoding type, including dictionary type name and enumeration class name; targetField(): Specifies the target field to which the translation result will be written; targetType(): Specifies a custom translation type, including dictionaries and enumerations.
[0010] Specifically, step S3 further includes performing the following operations for each identified field with a field-level annotation: By using reflection, the current value of the source encoded field can be obtained, i.e., the encoded value. Read the attribute information configured in the field-level annotation on the field to obtain the specified translation type and the name of the specified target field, so as to determine the type of translation service to be called and the target field to which the translation result is assigned.
[0011] Specifically, the readable text result in step S4 includes: passing the encoded value obtained in step S3 and the necessary context information as parameters to the translation service to obtain the corresponding readable text.
[0012] The beneficial effects of this invention are as follows: it achieves centralized management and automated execution of encoding and translation functions, eliminating the need to repeatedly write conversion logic in various business methods, effectively reducing code redundancy, improving system scalability and data presentation consistency, and has the following technical advantages: Non-intrusive design: Translation is automatically completed during the data return process through AOP aspects, without the need to write conversion code in business logic, without intruding on the original system, and with strong compatibility; High maintainability: The encoding and translation logic is centrally managed, and the field translation rules are configured through annotations. Modifying the translation rules only requires adjusting the annotation parameters, without having to change multiple parts of the business code; Low coupling and high reusability: The translation function is decoupled from the business logic, and the same translation mechanism can be reused in multiple interfaces for the same fields (such as status codes and type codes), improving development efficiency; Fine-grained control: The scope of method-level application is controlled by @NeedTransMethod, and field-level precise annotation is achieved by @TransField. It can be enabled on demand to avoid invalid processing. Highly scalable: Supports multiple translation types such as dictionary, enumeration, and region, and can be extended to support new data sources or translation strategies through custom translators; Improve data consistency: A unified translation entry point avoids inconsistencies in translation logic between the front-end, back-end, or multiple services, ensuring the accuracy and standardization of data display. Attached Figure Description
[0013] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on the structures shown in these drawings without creative effort.
[0014] Figure 1 This is a schematic diagram of the aspect-based database field encoding and translation method in an embodiment of the present invention. Detailed Implementation
[0015] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. The components of the embodiments of the present invention described and shown in the accompanying drawings can generally be arranged and designed in various different configurations.
[0016] It should be noted that similar labels and letters in the following figures indicate similar items. Therefore, once an item is defined in one figure, it does not need to be further defined and explained in subsequent figures.
[0017] The following is in conjunction with the appendix Figure 1 The following describes some embodiments of the present invention in detail. Unless otherwise specified, the following embodiments and features can be combined with each other.
[0018] This invention proposes a section-based database field encoding translation method, which, in a preferred embodiment, includes the following steps: Step S1: Configure AOP aspects according to the front-end request. When any method marked by a method-level annotation completes execution and returns normally, the configured aspect will be automatically triggered. Step S2: Obtain the returned data information through the aspect, and use the reflection loop mechanism to traverse the returned value to obtain all fields in a single data item; Step S3: Determine if a field-level annotation exists. If it exists, it is identified as the source encoded field that needs to be translated, the field-level annotation attribute information is obtained, and the corresponding translation service is called. If it does not exist, return to step S2. Step S4: Again, use reflection to write the translated readable text result into the target field specified in the returned object; Step S5: Repeat steps S1 to S4 until all fields marked with field-level annotations in the returned object have been processed. The automated translation process is complete, and a complete data object with all specified encoded fields translated is obtained and can be directly returned to the client.
[0019] In this embodiment, data encoding translation is widely used in data presentation scenarios such as list queries and detail displays. Common translation types include "dictionary translation" (based on a data dictionary), "enumeration translation" (based on program enumeration), and "regional translation" (such as country / region code translation). To decouple the encoding translation function from business logic, this invention employs Aspect-Oriented Programming (AOP) technology to separate common translation processing logic from core business code, achieving non-intrusive functional enhancement. By automatically completing field translation processing at a unified interception point before data return, the maintainability and reusability of the code are improved.
[0020] This invention achieves precise identification and processing of data requiring encoding and translation, as well as its specific fields, by defining and combining two custom annotations. This ensures the flexibility and controllability of the translation logic. The annotation methods include: Method-level annotation @NeedTransMethod: This annotation is used to mark service methods that require field encoding translation as a trigger condition for AOP aspects. By introducing this annotation, the aspect is limited to methods annotated with @NeedTransMethod, avoiding indiscriminate interception of all methods. For example, write operation methods such as add, edit, and delete that do not involve data display do not require field translation, so this annotation is not added, thus effectively controlling the scope of the aspect and improving system performance and logical clarity.
[0021] Field-level annotation @TransField: This annotation is used to mark specific fields in a Data Transfer Object (DTO) or entity class, identifying those fields as target fields requiring encoding translation. The annotation contains several attributes to guide the translation process, primarily including: targetCode(): Specifies the encoding type, such as dictionary type name, enumeration class name, etc.; targetField(): Specifies the target field (such as statusName) to which the translation result is written. targetType(): Used to specify a custom translation type, such as dictionary, enumeration, etc.
[0022] In one specific embodiment, the aspect-based database field encoding translation method is as follows: Figure 1 This includes the following steps: (1) Configure an AOP aspect and use the `@AfterReturning` advice. Its pointcut expression is defined as: `@annotation(NeedTransMethod)`; The aspect will be automatically triggered when any method annotated with `@NeedTransMethod` completes execution and returns normally.
[0023] (2) In the aspect method, the return value of the intercepted method is obtained through the `JoinPoint` object. This return value is the core data object to be processed.
[0024] (3) Using Java reflection, iterate through all fields of the returned object and check each field one by one whether it is marked with the `@TransField` annotation. All fields with this annotation are identified as source encoded fields that need to be translated.
[0025] (4) For each identified field annotated with `@TransField`, perform the following operations: By using reflection, the current value (i.e., the encoded value, such as status code `1`) of the source encoded field can be obtained.
[0026] Read the attribute information configured in the `@TransField` annotation on this field. This information includes at least the following: - `targetType()`: Used to specify the translation type (such as "DICTIONARY", "ENUM", etc.) to determine which translation service should be invoked; - `targetField()`: Specifies the name of the target field, to which the translation result will be assigned.
[0027] (5) Based on the `targetType()` information extracted from the `@TransField` annotation, route and call the corresponding translation service (e.g., dictionary lookup service, enumeration mapping component or other custom translator). Pass the "encoded value" obtained in step (4) and the necessary context information as parameters to the translation service to obtain the corresponding "readable text" (e.g., translating status code `1` into "activated").
[0028] (6) Again, through reflection, the “readable text” obtained in step (5) is written into the “target field” specified by the `targetField` property in the returned object.
[0029] (7) Repeat steps (4) to (6) until all fields marked with `@TransField` in the returned object have been processed. At this point, the entire automated translation process is complete, and a complete data object that has been translated and can be directly returned to the client is obtained.
[0030] Through the above-mentioned annotation collaboration mechanism, this invention realizes an encoding translation scheme that enables on-demand and controls fine-grained field-level parameters, which not only ensures the flexibility of functions but also avoids intrusion into business code, thereby improving the maintainability and scalability of the system.
[0031] For the foregoing embodiments, in order to simplify the description, they are all described as a series of actions. However, those skilled in the art should understand that this application is not limited to the described order of actions, because according to this application, some steps can be performed in other orders or simultaneously. Furthermore, those skilled in the art should also understand that the embodiments described in the specification are preferred embodiments, and the actions involved are not necessarily essential to this application.
[0032] The above embodiments describe the basic principles, main features, and advantages of the present invention. Those skilled in the art should understand that the present invention is not limited to the above embodiments. The embodiments and descriptions in the specification are merely illustrative of the principles of the invention. Modifications and variations made by those skilled in the art without departing from the spirit and scope of the invention should be within the protection scope of the appended claims.
Claims
1. A method for translating database field encoding based on aspects, characterized in that, Includes the following steps: Step S1: Configure AOP aspects according to the front-end request. When any method marked by a method-level annotation completes execution and returns normally, the configured aspect will be automatically triggered. Step S2: Obtain the returned data information through the aspect, and use the reflection loop mechanism to traverse the returned value to obtain all fields in a single data item; Step S3: Determine if a field-level annotation exists. If it exists, it is identified as the source encoded field that needs to be translated, the field-level annotation attribute information is obtained, and the corresponding translation service is called. If it does not exist, return to step S2. Step S4: Again, use reflection to write the translated readable text result into the target field specified in the returned object; Step S5: Repeat steps S1 to S4 until all fields marked with field-level annotations in the returned object have been processed. The automated translation process is complete, and a complete data object with all specified encoded fields translated is obtained and can be directly returned to the client.
2. The aspect-based database field encoding translation method as described in claim 1, characterized in that, In step S1, method-level annotations are used to mark service methods that need to perform field encoding translation, serving as triggering conditions for AOP aspects. This limits the aspects to only those marked with method-level annotations, avoiding indiscriminate interception of all methods.
3. The aspect-based database field encoding translation method as described in claim 1, characterized in that, In step S2, the returned data information is the return value of the interception method obtained through the JoinPoint object, which is the core data object to be processed.
4. The aspect-based database field encoding translation method as described in claim 1, characterized in that, In step S3, field-level annotations are used to mark specific fields of DTO data transfer objects or entity classes, identifying these fields as target fields requiring encoding translation. The attributes of field-level annotations that guide the translation process include: targetCode(): Specifies the encoding type, including dictionary type name and enumeration class name; targetField(): Specifies the target field to which the translation result will be written; targetType(): Specifies a custom translation type, including dictionaries and enumerations.
5. The aspect-based database field encoding translation method as described in claim 4, characterized in that, Step S3 further includes performing the following operations for each identified field with a field-level annotation: By using reflection, the current value of the source encoded field can be obtained, i.e., the encoded value. Read the attribute information configured in the field-level annotation on the field to obtain the specified translation type and the name of the specified target field, so as to determine the type of translation service to be called and the target field to which the translation result is assigned.
6. The aspect-based database field encoding translation method as described in claim 5, characterized in that, The readable text result in step S4 specifically includes: passing the encoded value obtained in step S3 and the necessary context information as parameters to the translation service to obtain the corresponding readable text.