Configurable data interaction method based on JAVA reflection mechanism
Through the configurable data interaction method based on JAVA reflection mechanism, the problem of repeated code development in insurance company data interaction is solved, flexible data interaction and efficient development are realized, costs are reduced and efficiency is improved.
Patent Information
- Application Number
- CN202410768311.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-14
- Publication Date
- 2024-09-10
- Estimated Expiration
- 2044-06-14
AI Technical Summary
Insurance companies generate a large amount of repetitive code development during the data interaction process, resulting in high development costs and long development cycles. As request parameters increase, development efficiency is affected.
Adopt a configurable data interaction method based on the JAVA reflection mechanism, package the request information through JSON, and use the Java reflection mechanism to dynamically determine database operations and generate SQL statements, reducing code duplication and improving development efficiency.
It achieves flexible data interaction, reduces repeated development, reduces development costs and maintenance costs, and improves development efficiency and system reliability.
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a configurable data interaction method based on a JAVA reflection mechanism. Background Art
[0002] When insurance companies conduct business, they interact with a large amount of data, such as customer information, payment information, policy information, etc. When obtaining this data and completing data processing in the conventional way, a large amount of duplicate code development work will be generated. Repeated development of similar functional codes will consume a lot of time and human resources, increase development costs and extend project cycles; and this part of duplicate code will increase with the increase of request parameters, seriously affecting development efficiency; therefore, a new configurable data interaction method is very necessary. Summary of the invention
[0003] The purpose of the present invention is to solve the shortcomings of the prior art and to propose a configurable data interaction method based on the JAVA reflection mechanism.
[0004] To achieve the above object, the present invention adopts the following technical solutions: A configurable data interaction method based on JAVA reflection mechanism, comprising the following steps: S1: The caller packages the request information and sends it to the server; The caller is the party that initiates the request, including an application, a client, 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, where the request information includes the parameter name, parameter value, and parameter type corresponding to the parameter; The parameters include field information parameters of the data table, database type parameters of the operation, database table name parameters, database operation type parameters, and primary key name parameters; The parameter name in the field information parameter of the data table is the name of the data field that needs 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 database type of the actual operation, and the database types include mysql, oracle, sqlserver, etc., wherein the parameter name is the name of the database type of the actual 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 on which the operation is to be performed, wherein the parameter name is the name of the database table actually to be operated, the parameter value is the specific parameter value corresponding to the database table name, and the parameter type is a string; The database operation type parameter is used to specify the type of operation to be performed, including entry, update, deletion, etc.; wherein the parameter name is the type of operation actually 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 primary key field name of the database table; wherein the parameter name is the primary key field name, the parameter value is the specific parameter value corresponding to the primary key field name, and the operation type is a string; After the request information is packaged, the caller sends the request information to the server.
[0005] S2: The service provider receives the request information from the caller and processes the parameters in the request information; The following sub-steps are included: S21: receiving the request information and obtaining parameter information in the request information; The service provider receives the request information from the caller and parses the request information through the Java built-in class library, including using the Java Servlet API or related modules in the Spring Framework to parse the request information and obtain the parameter names and parameter values in the request information; Specifically, a server program Servlet is created through the Java Servlet API to process request information. The server program Servlet inherits the HttpServlet class and obtains parameter names and parameter values from the request information according to 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; Get the parameter name and value in the request information through the relevant modules in Spring Framework, such as @RequestParam annotations in Spring MVC; S22: Create a Java entity class and inject the obtained parameter value into the parameter name corresponding to the Java entity class; Create a Java entity class, which includes fields, access methods corresponding to the fields, etc.; The field names corresponding to the fields correspond one-to-one to the parameter names in the request information; The access method includes obtaining function getter, setting function setter, etc.; All field information of this Java entity class is obtained through the Java reflection mechanism, the parameter name and parameter value obtained in step S21 are traversed, and the field names consistent with the parameter names are searched in turn from the Java entity class through reflection, and the parameter value corresponding to the parameter name is injected into the field corresponding to the field name in the Java entity class, which is the field value corresponding to the field.
[0006] S3: Based on the parameters in the request information, SQL statements are spliced to complete the corresponding operations and the processing results are returned to the caller; The service provider creates a corresponding SQL statement header according to the database type parameter and database operation type parameter of the operation in the obtained request information; Specifically, create the SQL statement header through string concatenation or String.format function; The database table name is concatenated with the SQL statement header. Based on the Java entity class of the injected parameter value, the field name and the corresponding field value are concatenated into the created SQL statement in a traversal manner through the Java reflection mechanism. Use the database connection object to execute the spliced SQL statements to complete database entry, update, deletion and other operations; The result of the database operation is returned to the caller; the operation result includes a success or failure indicator, the number of rows affected, exception information, etc.
[0007] Compared with the prior art, the present invention has the following beneficial effects: A configurable data interaction method based on JAVA reflection mechanism proposed by the present invention The Java reflection mechanism is used to complete data processing. By configuring the parameters in the request information, the runtime can dynamically determine the database operations to be performed, and generate corresponding SQL statements according to different requirements to achieve data interaction operations between different databases. It can be flexibly configured to reduce the amount of code, avoid the problem of repeated development of similar functional codes that consumes a lot of time and human resources, increases development costs and prolongs the project cycle, improves development efficiency and reduces maintenance costs. DETAILED DESCRIPTION
[0008] In order to provide a further understanding of the purpose, structure, features, and functions of the present invention, the following detailed description is given in conjunction with the embodiments.
[0009] A configurable data interaction method based on JAVA reflection mechanism, comprising the following steps: S1: The caller packages the request information and sends it to the server; The caller is the party that initiates the request, including an application, a client, 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, where the request information includes the parameter name, parameter value, and parameter type corresponding to the parameter; The parameters include field information parameters of the data table, database type parameters of the operation, database table name parameters, database operation type parameters, and primary key name parameters; 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; for example, if the name of the data field to be entered is "name", the corresponding parameter value is "cody", and the parameter type is a string; The database type parameter of the operation is used to specify the database type of the actual operation, and the database types include mysql, oracle, sqlserver, etc., wherein the parameter name is the name of the database type of the actual operation, the parameter value is the specific parameter value corresponding to the database type name, and the parameter type is a string; for example, the name of the database type of the actual operation is named "sql_type", and the corresponding parameter value, that is, the actual database type name is "oracle", and the parameter type is a string; The database table name parameter is used to specify the name of the database table on which the operation is performed, wherein the parameter name is the name of the database table actually to be operated, the parameter value is the specific parameter value corresponding to the database table name, and the parameter type is a string; for example, the name of the database table actually to be operated is recorded as "table_name", and the corresponding parameter value, that is, the actual database table name is "f_cntr", and the parameter type is a string; The database operation type parameter is used to specify the type of operation to be performed, including entry, update, deletion, etc.; wherein the parameter name is the type of operation actually performed, the parameter value is the parameter value of the specific operation type, and the parameter type is a string; for example, the type of operation actually performed is recorded as "operate_type", the specific parameter value of the operation type is "insert", and the parameter type is a string; The primary key name parameter is used to specify the name of the primary key field of the database table; 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 a string; for example, the primary key field name is recorded as "key_name", the parameter value corresponding to the primary key field name is "id", and the parameter type is a 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 server.
[0010] By requiring null and non-null values for the primary key field name parameter value under different operation types, the security, reliability and consistency of the system can be improved, errors and risks can be reduced, and code logic can be simplified, thereby improving the overall quality and stability of the system.
[0011] S2: The service provider receives the request information from the caller and processes the parameters in the request information; The following sub-steps are included: S21: receiving the request information and obtaining parameter information in the request information; The service provider receives the request information from the caller and parses the request information through the Java built-in class library, including using the Java Servlet API or related modules in the Spring Framework to parse the request information and obtain the parameter names and parameter values in the request information; Specifically, a server program Servlet is created through the Java Servlet API to process request information. The server program Servlet inherits the HttpServlet class and obtains parameter names and parameter values from the request information according to 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; Get the parameter name and value in the request information through the relevant modules in Spring Framework, such as @RequestParam annotations in Spring MVC; S22: Create a Java entity class and inject the obtained parameter value into the parameter name corresponding to the Java entity class; Create a Java entity class, which includes fields, access methods corresponding to the fields, etc.; The field names corresponding to the fields correspond one-to-one to the parameter names in the request information; The access method includes obtaining function getter, setting function setter, etc.; All field information of this Java entity class is obtained through the Java reflection mechanism, the parameter name and parameter value obtained in step S21 are traversed, and the field names consistent with the parameter names are searched in turn from the Java entity class through reflection, and the parameter value corresponding to the parameter name is injected into the field corresponding to the field name in the Java entity class, which is the field value corresponding to the field.
[0012] By receiving and processing the parameters in the request information, the subsequent business logic can more easily operate these parameter values; at the same time, by using technologies such as Java Servlet API or Spring Framework, higher flexibility and maintainability can be achieved when processing request information.
[0013] S3: Based on the parameters in the request information, SQL statements are spliced to complete the corresponding operations and the processing results are returned to the caller; The service provider creates a corresponding SQL statement header according to the database type parameter and database operation type parameter of the operation in the obtained request information; Specifically, create the SQL statement header through string concatenation or String.format function; The database table name is concatenated with the SQL statement header. Based on the Java entity class of the injected parameter value, the field name and the corresponding field value are concatenated into the created SQL statement in a traversal manner through the Java reflection mechanism. By configuring the parameters in the request information, the runtime can dynamically determine the database operations to be performed and generate corresponding SQL statements according to different requirements; Furthermore, if the database operation type is "insert", the field name and field value of the corresponding field are spliced into the insert statement; if the database operation type is "delete", "update", etc., the field name and field value corresponding to the field are spliced into the where condition; Use the database connection object to execute the spliced SQL statements to complete database entry, update, deletion and other operations; The database connection object refers to an object used to establish a connection with a database in Java, which is usually instantiated from a class in a JDBC driver; Return the result of the database operation to the caller; the operation result includes the success or failure mark, the number of rows affected, exception information, etc.; If the operation result is an indicator of success or failure, the indicator includes "true" and "false"; "true" indicates that the operation is successful, and "false" indicates that the operation fails; By returning a success or failure indicator, the caller can intuitively understand the result of the database operation without the need for 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 insert, delete, or modify operation, the caller determines the result of the operation based on the number of rows affected; Returning the number of affected rows can help the caller accurately determine the impact scope of the database operation and take appropriate follow-up measures; If an exception occurs in a database operation, the exception information is returned to the caller, and the caller performs exception processing based on the exception information; If an exception occurs in a database operation, returning the exception information can help the caller quickly identify the problem and perform corresponding exception processing, such as logging, notifying the administrator, or retrying. Returning the results of database operations to the caller and providing a success or failure indicator, 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 by the above-mentioned relevant embodiments, however, the above-mentioned embodiments are only examples for implementing the present invention. It must be pointed out that the disclosed embodiments do not limit the scope of the present invention. On the contrary, changes and modifications made without departing from the spirit and scope of the present invention are all 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: The following steps are involved: S1: The caller packages the request information and sends it to the server; The caller is the party that initiates the request; The service party is the party that receives and processes the request; The caller packages the request information in JSON format, where the request information includes the parameter name, parameter value, and parameter type corresponding to the parameter; The parameters include field information parameters of the data table, database type parameters of the operation, 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; The following steps are involved: S21: receiving the request information and obtaining parameter information in the request information; S22: Create a Java entity class and inject the obtained parameter value into the parameter name corresponding to the Java entity class; S3: Based on the parameters in the request information, SQL statements are spliced to complete the corresponding operations. And return the processing result to the caller; The service provider creates a corresponding SQL statement header according to the database type parameter and database operation type parameter of the operation in the obtained request information; Create the SQL statement header through string concatenation or String.format function; The database table name is concatenated with the SQL statement header. Based on the Java entity class of the injected parameter value, the field name and the corresponding field value are concatenated into the created SQL statement in a traversal manner through the Java reflection mechanism. Use the database connection object to execute the spliced SQL statement to complete the database operation; return the database operation result to the caller.
2. The configurable data interaction method based on JAVA reflection mechanism as claimed 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 database type of the actual operation, wherein the parameter name is the name of the database type of the actual 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 on which the operation is to be performed, wherein the parameter name is the name of the database table actually to be operated, the parameter value is the specific parameter value corresponding to the database table name, and the parameter type is a string; The database operation type parameter is used to specify the type of operation to be performed, wherein the parameter name is the type of operation actually 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 primary key field name of the database table; wherein the parameter name is the primary key field name, the parameter value is the specific parameter value corresponding to the primary key field name, and the operation type is a string.
3. The configurable data interaction method based on JAVA reflection mechanism as claimed in claim 1, characterized in that: The specific contents of step S2 are as follows: S21: receiving the request information and obtaining parameter information in the request information; The service provider receives the request information from the caller and parses the request information through the Java built-in class library, including using the Java Servlet API or related modules in the Spring Framework to parse the request information and obtain the parameter names and parameter values in the request information; S22: Create a Java entity class and inject the obtained parameter value into the parameter name corresponding to the Java entity class; Create a Java entity class, wherein the Java entity class includes fields and access methods corresponding to the fields; The field names corresponding to the fields correspond one-to-one to the parameter names in the request information; The access method includes obtaining function getter and setting function setter; All field information of this Java entity class is obtained through the Java reflection mechanism, the parameter name and parameter value obtained in step S21 are traversed, and the field names consistent with the parameter names are searched in turn from the Java entity class through reflection, and the parameter value corresponding to the parameter name is injected into the field corresponding to the field name in the Java entity class, which is the field value corresponding to the field.
4. The configurable data interaction method based on JAVA reflection mechanism as claimed in claim 3, characterized in that: Specifically, a server program Servlet is created through the Java Servlet API to process request information. The server program Servlet inherits the HttpServlet class and obtains parameter names and parameter values from the request information according to 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; Get the parameter name and parameter value in the request information through the @RequestParam annotation in the relevant module Spring MVC in the Spring Framework.
5. The configurable data interaction method based on JAVA reflection mechanism as claimed in claim 1, characterized in that: In step S3, the operation result includes a success or failure indicator, the number of rows affected, and abnormal information; If the operation result is an indicator of success or failure, the indicator includes "true" and "false"; "true" indicates that the operation is successful, and "false" indicates that the operation fails; If the operation result is the number of rows affected, that is, the number of rows affected after the insert, delete, or modify operation, the caller determines the result of the operation based on the number of rows affected; If an exception occurs in a database operation, the exception information is returned to the caller, and the caller performs exception handling based on the exception information.
Citation Information
Patent Citations
Long lasting implementing method for data
CN101339559A
A data integrating method and system based on a Java reflection technology
CN109725953A
Method and system for realizing object relation mapping framework based on JSON (JavaScript Object Notation)
CN116881269A
Automatic water draining and disinfection device for dental handpiece
KR102931713B1
Cited By
Data docking system based on API
CN119830325A
