A configurable data interaction method based on JAVA reflection mechanism
Patent Information
- Application Number
- CN202410768311.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-14
- Publication Date
- 2026-11-03
- Estimated Expiration
- 2044-06-14
AI Technical Summary
[0002]保险公司开展业务时,会进行大量数据交互,例如客户信息、收付费信息、保单信息等数据,按照常规方式获取这些数据、完成数据处理时,会产生大量的重复代码开发工作,重复开发相似功能代码会消耗大量的时间和人力资源,增加开发成本并延长项目周期;且该部分重复代码会随着请求参数增多而增加,严重影响开发效率;因此,一种新型的可配置化数据交互方法是非常有必要的
本发明提出的一种基于JAVA反射机制的可配置化数据交互方法
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a configurable data interaction method based on the JAVA reflection mechanism. Background Technology
[0002] When insurance companies conduct business, they engage in a large amount of data exchange, such as customer information, payment information, and policy information. Acquiring and processing this data in the conventional way generates a lot of repetitive code development work. Developing similar functional code repeatedly consumes a lot of time and human resources, increases development costs, and extends project cycles. Moreover, this repetitive code increases with the number of request parameters, seriously affecting development efficiency. Therefore, a new configurable data exchange method is essential. Summary of the Invention
[0003] The purpose of this invention is to address the shortcomings of existing technologies by proposing a configurable data interaction method based on the JAVA reflection mechanism.
[0004] To achieve the above objectives, the present invention adopts the following technical solution: A configurable data interaction method based on Java reflection mechanism includes the following steps: S1: The caller packages the request information and sends it to the service provider; The caller is the party that initiates the request, including applications, clients, other systems or services, etc. The service provider is the party that receives and processes the request, including web servers, API services, database servers, etc. The caller packages the request information in JSON format, which includes the parameter name, parameter value, and parameter type corresponding to the parameter. The parameters include data table field information parameters, database type parameters for the operation, database table name parameters, database operation type parameters, and primary key name parameters. In the field information parameters of the data table, the parameter name is the name of the data field to be entered, modified or deleted, the parameter value is the parameter value corresponding to the data field name, and the parameter type is set according to the caller's requirements. The database type parameter of the operation is used to specify the actual database type of the operation. The database type includes MySQL, Oracle, SQL Server, etc. The parameter name is the name of the actual database type of the operation, the parameter value is the specific parameter value corresponding to the database type name, and the parameter type is a string. The database table name parameter is used to specify the name of the database table to be operated on. The parameter name is the actual name of the database table to be operated on, the parameter value is the specific parameter value corresponding to the database table name, and the parameter type is string. The database operation type parameter is used to specify the type of operation to be performed, including input, update, deletion, etc.; wherein, the parameter name is the actual operation type to be performed, the parameter value is the parameter value of the specific operation type, and the parameter type is string; The primary key name parameter is used to specify the name of the primary key field in the database table; wherein, the parameter name is the name of the primary key field, the parameter value is the specific parameter value corresponding to the name of the primary key field, and the operation type is string. After the request information is packaged, the caller sends the request information to the service provider.
[0005] S2: The service provider receives the request information from the caller and processes the parameters in the request information; Includes the following sub-steps: S21: Receive the request information and obtain the parameter information in the request information; The service provider receives the request information from the caller, parses the request information using Java built-in class libraries, including using Java Servlet API or relevant modules in Spring Framework to parse the request information and obtain the parameter names and values in the request information; Specifically, a server-side Servlet is created using the Java Servlet API to handle request information. The server-side Servlet inherits from the HttpServlet class and retrieves parameter names and values from the request information using methods such as doGet and doPost in the inherited HttpServlet class. The Java Servlet API is a collection of Java classes and interfaces, and the HttpServlet class is an abstract class in the Java Servlet API. The parameter names and values in the request information can be obtained through relevant modules in the Spring Framework, such as the @RequestParam annotation in Spring MVC. S22: Create a Java entity class and inject the obtained parameter values into the corresponding parameter names of the Java entity class; Create a Java entity class, which contains fields and corresponding access methods for those fields; The field names corresponding to the fields are one-to-one with the parameter names in the request information; The access methods include getter functions and setter functions; The Java entity class obtains all field information through Java reflection. Iterates through the parameter names and values obtained in step S21, and sequentially finds the field names that match the parameter names in the Java entity class through reflection. The parameter values corresponding to the parameter names are then injected into the fields corresponding to the field names in the Java entity class, which are the field values corresponding to the fields.
[0006] S3: Based on the parameters in the request information, construct the SQL statement, complete the corresponding operation, and return the processing result to the caller; The service provider creates the corresponding SQL statement header based on the database type parameter and database operation type parameter in the obtained request information; Specifically, SQL statement headers are created using string concatenation or the String.format function; The database table name is concatenated with the SQL statement header. Based on the Java entity class that injects the parameter values, the field names and corresponding field values are concatenated into the created SQL statement through Java reflection in a traversal manner. Use the database connection object to execute the concatenated SQL statements to complete database operations such as data entry, update, and deletion; The database operation results are returned to the caller; the operation results include a success or failure indicator, the number of rows affected, and exception information.
[0007] Compared with the prior art, the beneficial effects of the present invention are as follows: This invention proposes a configurable data interaction method based on Java reflection mechanism. Data processing is accomplished using Java reflection. By configuring parameters in the request information, the runtime can dynamically determine the database operations to be performed and generate corresponding SQL statements according to different needs. This enables data interaction between different databases, allowing for flexible configuration, reducing code volume, and avoiding the problem of repeatedly developing similar functionalities that consumes a lot of time and human resources, increases development costs, and extends project cycles. This improves development efficiency and reduces maintenance costs. Detailed Implementation
[0008] To provide a further understanding of the purpose, structure, features, and functions of the present invention, detailed descriptions are provided below with reference to specific embodiments.
[0009] A configurable data interaction method based on Java reflection mechanism includes the following steps: S1: The caller packages the request information and sends it to the service provider; The caller is the party that initiates the request, including applications, clients, other systems or services, etc. The service provider is the party that receives and processes the request, including web servers, API services, database servers, etc. The caller packages the request information in JSON format, which includes the parameter name, parameter value, and parameter type corresponding to the parameter. The parameters include data table field information parameters, database type parameters, database table name parameters, database operation type parameters, and primary key name parameters. In the field information parameters of the data table, the parameter name is the name of the data field to be entered, modified, or deleted, the parameter value is the parameter value corresponding to the data field name, and the parameter type is set according to the caller's requirements; for example, if the data field name to be entered is "name", the corresponding parameter value is "cody", and the parameter type is string; The database type parameter of the operation is used to specify the actual database type of the operation. The database type includes MySQL, Oracle, SQL Server, etc. The parameter name is the name of the actual database type of the operation, and the parameter value is the specific parameter value corresponding to the database type name. The parameter type is a string. For example, if the actual database type name of the operation is named "sql_type", the corresponding parameter value is the actual database type name "oracle", and the parameter type is a string. The database table name parameter is used to specify the name of the database table to be operated on. The parameter name is the actual name of the database table to be operated on, the parameter value is the specific parameter value corresponding to the database table name, and the parameter type is string. For example, the actual name of the database table to be operated on is denoted as "table_name", and the corresponding parameter value is the actual database table name "f_cntr", and the parameter type is string. The database operation type parameter is used to specify the type of operation to be performed, including input, update, deletion, etc.; wherein, the parameter name is the actual operation type to be performed, the parameter value is the parameter value of the specific operation type, and the parameter type is string; for example, the actual operation type to be performed is denoted as "operate_type", the specific parameter value of the operation type is "insert", and the parameter type is string; The primary key name parameter is used to specify the name of the primary key field in the database table; where the parameter name is the name of the primary key field, the parameter value is the specific parameter value corresponding to the primary key field name, and the operation type is string; for example, the primary key field name is denoted as "key_name", the parameter value corresponding to the primary key field name is "id", and the parameter type is string; Furthermore, when the specified operation type is "insert", the primary key field name parameter value can be empty; when the specified operation type is "delete" or "update", the primary key field name parameter value cannot be empty. After the request information is packaged, the caller sends the request information to the service provider.
[0010] By requiring primary key field name parameters to be nullable or non-nullable under different operation types, the security, reliability, and consistency of the system can be improved, errors and risks can be reduced, code logic can be simplified, and thus the overall quality and stability of the system can be enhanced.
[0011] S2: The service provider receives the request information from the caller and processes the parameters in the request information; Includes the following sub-steps: S21: Receive the request information and obtain the parameter information in the request information; The service provider receives the request information from the caller, parses the request information using Java built-in class libraries, including using Java Servlet API or relevant modules in Spring Framework to parse the request information and obtain the parameter names and values in the request information; Specifically, a server-side Servlet is created using the Java Servlet API to handle request information. The server-side Servlet inherits from the HttpServlet class and retrieves parameter names and values from the request information using methods such as doGet and doPost in the inherited HttpServlet class. The Java Servlet API is a collection of Java classes and interfaces, and the HttpServlet class is an abstract class in the Java Servlet API. The parameter names and values in the request information can be obtained through relevant modules in the Spring Framework, such as the @RequestParam annotation in Spring MVC. S22: Create a Java entity class and inject the obtained parameter values into the corresponding parameter names of the Java entity class; Create a Java entity class, which contains fields and corresponding access methods for those fields; The field names corresponding to the fields are one-to-one with the parameter names in the request information; The access methods include getter functions and setter functions; The Java entity class obtains all field information through Java reflection. Iterates through the parameter names and values obtained in step S21, and sequentially finds the field names that match the parameter names in the Java entity class through reflection. The parameter values corresponding to the parameter names are then injected into the fields corresponding to the field names in the Java entity class, which are the field values corresponding to the fields.
[0012] By receiving and processing parameters in the request information, subsequent business logic can more easily manipulate these parameter values; at the same time, by using technologies such as the Java Servlet API or Spring Framework, greater flexibility and maintainability can be achieved when processing request information.
[0013] S3: Based on the parameters in the request information, construct the SQL statement, complete the corresponding operation, and return the processing result to the caller; The service provider creates the corresponding SQL statement header based on the database type parameter and database operation type parameter in the obtained request information; Specifically, SQL statement headers are created using string concatenation or the String.format function; The database table name is concatenated with the SQL statement header. Based on the Java entity class that injects the parameter values, the field names and corresponding field values are concatenated into the created SQL statement through Java reflection in a traversal manner. By configuring parameters in the request information, the runtime can dynamically determine the database operation to be performed and generate corresponding SQL statements according to different needs. Furthermore, if the database operation type is "insert", the field name and value of the corresponding field will be appended to the insert statement; if the database operation type is "delete", "update", etc., the field name and value of the corresponding field will be appended to the where condition. Use the database connection object to execute the concatenated SQL statements to complete database operations such as data entry, update, and deletion; The database connection object refers to the object used in Java to establish a connection with a database, which is usually instantiated from a class in a JDBC driver. The database operation results are returned to the caller; the operation results include a success or failure indicator, the number of rows affected, and exception information. If the operation result is a success or failure indicator, the indicator includes "true" and "false"; "true" indicates that the operation was successful and "false" indicates that the operation failed. By returning a success or failure indicator, the caller can intuitively understand the result of the database operation without needing to provide additional explanation or parsing of the returned result.
[0014] If the operation result is the number of rows affected, that is, the number of rows affected after the insertion, deletion, or modification operation, the caller determines the result of the operation based on the number of rows affected. Returning the number of rows affected helps the caller accurately determine the scope of the database operation's impact, thus enabling them to take appropriate follow-up actions. If an exception occurs during a database operation, the exception information will be returned to the caller, who will then handle the exception accordingly. If an exception occurs during a database operation, returning exception information can help the caller quickly identify the problem and perform appropriate exception handling, such as logging, notifying the administrator, or retrying. Returning database operation results to the caller, along with success or failure indicators, the number of rows affected, and exception information, can effectively improve system reliability, user experience, and development efficiency.
[0015] The present invention has been described in the above-described embodiments; however, these embodiments are merely examples for implementing the present invention. It must be noted that the disclosed embodiments do not limit the scope of the present invention. Conversely, any modifications and refinements made without departing from the spirit and scope of the present invention are within the scope of patent protection of the present invention.
Claims
1. A configurable data interaction method based on Java reflection mechanism, characterized in that: Includes the following steps: S1: The caller packages the request information and sends it to the service provider; The caller is the party that initiates the request; The service provider is the party that receives and processes the request; The caller packages the request information in JSON format, which includes the parameter name, parameter value, and parameter type corresponding to the parameter. The parameters include data table field information parameters, database type parameters, database table name parameters, database operation type parameters, and primary key name parameters. After the request information is packaged, the caller sends the request information to the service provider; S2: The service provider receives the request information from the caller and processes the parameters in the request information; Includes the following steps: S21: Receive the request information and obtain the parameter information in the request information; The service provider receives the request information from the caller, parses the request information using Java built-in class libraries, including using Java Servlet API or relevant modules in Spring Framework to parse the request information and obtain the parameter names and values in the request information; A server-side Servlet is created using the Java Servlet API to handle request information. The server-side Servlet inherits from the HttpServlet class and retrieves parameter names and values from the request information using the doGet and doPost methods in the inherited HttpServlet class. The Java Servlet API is a collection of Java classes and interfaces, and the HttpServlet class is an abstract class in the Java Servlet API. The parameter names and values in the request information can be obtained through the @RequestParam annotation in the relevant module of Spring MVC in the Spring Framework. S22: Create a Java entity class and inject the obtained parameter values into the corresponding parameter names of the Java entity class; Create a Java entity class, which contains fields and corresponding access methods for those fields; The field names corresponding to the fields are one-to-one with the parameter names in the request information; The access methods include a getter function and a setter function. All field information of this Java entity class is obtained through Java reflection mechanism. The parameter names and parameter values obtained in step S21 are traversed. Field names that match the parameter names are found in the Java entity class in turn through reflection. The parameter values corresponding to the parameter names are injected into the fields corresponding to the field names in the Java entity class, which are the field values corresponding to the fields. S3: Based on the parameters in the request information, construct an SQL statement to complete the corresponding operation. And return the processing result to the caller; The service provider creates the corresponding SQL statement header based on the database type parameter and database operation type parameter in the obtained request information; Create SQL statement headers using string concatenation or the String.format function; The database table name is concatenated with the SQL statement header. Based on the Java entity class that injects the parameter values, the field names and corresponding field values are concatenated into the created SQL statement through Java reflection in a traversal manner. Use the database connection object to execute the concatenated SQL statement to complete the database operation; then return the database operation result to the caller.
2. The configurable data interaction method based on JAVA reflection mechanism as described in claim 1, characterized in that: In step S1, the parameter name in the field information parameter of the data table is the name of the data field to be entered, modified or deleted, the parameter value is the parameter value corresponding to the data field name, and the parameter type is set according to the caller's requirements. The database type parameter of the operation is used to specify the actual database type of the operation, wherein the parameter name is the name of the actual database type of the operation, the parameter value is the specific parameter value corresponding to the database type name, and the parameter type is a string; The database table name parameter is used to specify the name of the database table to be operated on. The parameter name is the actual name of the database table to be operated on, the parameter value is the specific parameter value corresponding to the database table name, and the parameter type is string. The database operation type parameter is used to specify the type of operation to be performed, wherein the parameter name is the actual operation type to be performed, the parameter value is the parameter value of the specific operation type, and the parameter type is a string; The primary key name parameter is used to specify the name of the primary key field in the database table; wherein, the parameter name is the name of the primary key field, the parameter value is the specific parameter value corresponding to the name of the primary key field, and the operation type is string.
3. The configurable data interaction method based on JAVA reflection mechanism as described in claim 1, characterized in that: In step S3, the operation result includes a success or failure indicator, the number of rows affected, and exception information; if the operation result is a success or failure indicator, the indicator includes "true" and "false"; "true" indicates that the operation was successful and "false" indicates that the operation failed. If the operation result is the number of rows affected, that is, the number of rows affected after the insertion, deletion, or modification operation, the caller determines the result of the operation based on the number of rows affected. If an exception occurs during database operation, the exception information will be returned to the caller, who will then handle the exception accordingly.
Citation Information
Patent Citations
Long lasting implementing method for data
CN101339559A
A data integrating method and system based on a Java reflection technology
CN109725953A
