Method for automatically mapping database table fields to generate entity classes

The method addresses the challenge of mapping database table fields to entity classes by using the Spring framework's automatic bean injection to generate Java entity classes and Dao layer files, enhancing backend development efficiency.

CN119088357APending Publication Date: 2024-12-06SHANGHAI SAIKE MOBILITY TECH SERVICE CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202410978734.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-22
Publication Date
2024-12-06

AI Technical Summary

Technical Problem

Existing technologies lack an efficient method to automatically map database table fields to generate entity classes, particularly in the context of Spring framework bean injection.

Method used

A method utilizing the Spring framework's automatic bean injection principle to automatically generate Java entity classes by reading database table information and mapping it to corresponding Dao layer files, involving the creation of a TableInfo entity class and a DbMappingBean class that implements ApplicationContextAware to manage this process.

Benefits of technology

Automatically generates Java entity classes and corresponding Dao layer files based on database table information, simplifying backend development by leveraging Spring framework knowledge and database interaction.

✦ Generated by Eureka AI based on patent content.
Patent Text Reader

Abstract

The invention discloses a method for automatically mapping database table fields to generate entity classes, and relates to the technical field of back-end development. And back-end development work: automatically reading table information of the database and generating a Java entity class and a corresponding Dao layer file by utilizing the principle of automatic beam injection of the Spring framework and related knowledge of the database.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of backend development, and in particular to the principle of automatic bean injection of a Spring framework. Background Art

[0002] Spring's property injection is part of the spring bean life cycle. The bean life cycle first remembers two concepts: Spring bean: an object that ultimately exists in the spring container. Object: an instantiated object, but an object is not necessarily a spring bean. The principle of Spring framework's autowiring bean mainly involves the following key components and steps: Bean definition: define beans in a Spring configuration file or using a Java configuration class. Component scanning: Spring scans the specified package path, looks for classes with annotations (such as @Component, @Service, @Repository, @Controller), and registers them as beans. Dependency injection annotations: Use the @Autowired annotation on a class field or constructor to indicate the dependencies that need to be autowired. Container initialization: Initialize the Spring container and load all configured beans. Dependency injection: When creating a bean, Spring checks and injects the required dependencies, that is, if a bean requires another bean as a dependency, Spring will automatically inject it into the dependent bean. Summary of the invention

[0003] In order to solve one of the problems existing in the prior art, a method for automatically mapping database table fields to generate entity classes is disclosed, which uses the principle of automatic bean injection of the Spring framework to automatically generate JAVA entity classes corresponding to the database table.

[0004] To solve the above problems, the technical solutions adopted by the present invention are as follows:

[0005] A method for automatically mapping database table fields to generate entity classes is as follows:

[0006] Step 1: Create a TableInfo entity class, which corresponds to the specific field information model of the database table. This class has four member variables: columnName: field name, defaultValue: default value, dataType: data type, comment: field comment information;

[0007] Step 2: Create the DbMappingBean class, which is used to initialize database information and map related table fields to related bean objects. This class has a static member variable tableFullInfoMap of type Map <String,Map<String,TableInfo> >, this class implements the ApplicationContextAware interface and implements its method setApplicationContext(ApplicationContext applicationContext);

[0008] First, based on the fully qualified name of the interface class as a parameter, use the Class.forName(StringclassName) method to get the object of the current interface class. For example, enter the fully qualified class name of the current interface class: com.saicmobility.sr.srpassengerservice.api.SrPassengerService to get the SrPassengerService class, thus obtaining all the information of the current interface class.

[0009] Since we are doing interface testing, we only need to get all the interface methods of the current object. Because it is an interface class, there will be no private methods. For each interface call, the sub-URL is the current interface class name plus the method name. For example, if the current method is getPassengerInfo, then the sub-URL ends with / SrPassengerService / getPassengerInfo.

[0010] Through the interface class obtained above, use Class.getDeclaredMethods() to get a list of all methods in the current class, which is presented in array form (Method[]), recorded as methods;

[0011] Declare a Map<String,Object> Object, used to store the final generated assembly parameter information, recorded as paramMap;

[0012] Traverse the above interface method array methods to assemble the internal input parameters. This is relatively complicated and needs to be completed in many steps.

[0013] In the for loop of the traversed object array, first get all the input parameter types of the current method through method.getParameterTypes(), which is in the format of a class array generic object (Class<?>[]), recorded as parameterTypes, and declare Map at the same time<String,Object> The object stores the current traversal method parameter, recorded as rawMap;

[0014] Traverse the above input parameter type array parameterTypes, and in the for loop, use the Class.forName(String className) method again to obtain the input parameter object class. The className here is obtained through the getName() of the traversed input parameter type, and the obtained object class is recorded as parameterClass;

[0015] Call the getDeclaredFields() method on the parameter class parameterClass obtained above to obtain an array of all declared member variables (Field[]) in the current parameter class, recorded as fields. This is the deep-level parameter field we need to obtain.

[0016] Traverse the member variable array fields above. In the for loop, the member variable Field needs to be classified and judged. If the object is of the following types: int.class, Integer.class, Object.class, List.class, long.class, Long.class, double.class, Double.class, LazyStringList.

[0017] Compared with the prior art, the implementation effects of the present invention are as follows:

[0018] This solution involves the field of back-end development technology. Back-end development work: It is necessary to use the principle of automatic bean injection of the Spring framework and relevant knowledge of the database to automatically read the table information of the database and generate Java entity classes and corresponding Dao layer files. DETAILED DESCRIPTION

[0019] The content of the present invention will be described below with reference to specific embodiments.

[0020] A method for automatically mapping database table fields to generate entity classes is as follows:

[0021] Step 1: Create a TableInfo entity class, which corresponds to the specific field information model of the database table. This class has four member variables: columnName: field name, defaultValue: default value, dataType: data type, comment: field comment information;

[0022] Step 2: Create the DbMappingBean class, which is used to initialize database information and map related table fields to related bean objects. This class has a static member variable tableFullInfoMap of type Map <String,Map<String,TableInfo> >, this class implements the ApplicationContextAware interface and implements its method setApplicationContext(ApplicationContext applicationContext);

[0023] First, based on the fully qualified name of the interface class as a parameter, use the Class.forName(StringclassName) method to get the object of the current interface class. For example, enter the fully qualified class name of the current interface class: com.saicmobility.sr.srpassengerservice.api.SrPassengerService to get the SrPassengerService class, thus obtaining all the information of the current interface class.

[0024] Since we are doing interface testing, we only need to get all the interface methods of the current object. Because it is an interface class, there will be no private methods. For each interface call, the sub-URL is the current interface class name plus the method name. For example, if the current method is getPassengerInfo, then the sub-URL ends with / SrPassengerService / getPassengerInfo.

[0025] Through the interface class obtained above, use Class.getDeclaredMethods() to get a list of all methods in the current class, which is presented in array form (Method[]), recorded as methods;

[0026] Declare a Map<String,Object> Object, used to store the final generated assembly parameter information, recorded as paramMap;

[0027] Traverse the above interface method array methods to assemble the internal input parameters. This is relatively complicated and needs to be completed in many steps.

[0028] In the for loop of the traversed object array, first get all the input parameter types of the current method through method.getParameterTypes(), which is in the format of a class array generic object (Class<?>[]), recorded as parameterTypes, and declare Map at the same time<String,Object> The object stores the current traversal method parameter, recorded as rawMap;

[0029] Traverse the above input parameter type array parameterTypes, and in the for loop, use the Class.forName(String className) method again to obtain the input parameter object class. The className here is obtained through the getName() of the traversed input parameter type, and the obtained object class is recorded as parameterClass;

[0030] Call the getDeclaredFields() method on the parameter class parameterClass obtained above to obtain an array of all declared member variables (Field[]) in the current parameter class, recorded as fields. This is the deep-level parameter field we need to obtain.

[0031] Traverse the member variable array fields above. In the for loop, the member variable Field needs to be classified and judged. If the object is of the following types: int.class, Integer.class, Object.class, List.class, long.class, Long.class, double.class, Double.class, LazyStringList.

[0032] This solution involves the field of back-end development technology. Back-end development work: It is necessary to use the principle of automatic bean injection of the Spring framework and relevant knowledge of the database to automatically read the table information of the database and generate Java entity classes and corresponding Dao layer files.

[0033] The above content is a detailed description of the present invention in combination with specific embodiments, and it cannot be determined that the specific implementation of the present invention is limited to these descriptions. For those skilled in the art of the present invention, without departing from the concept of the present invention, several simple deductions or substitutions can be made, which should be regarded as falling within the scope of protection of the present invention.

Claims

1. A method for automatically mapping database table fields to generate entity classes, characterized by: Step 1: Create a TableInfo entity class, which corresponds to the specific field information model of the database table. This class has four member variables: columnName: field name, defaultValue: default value, dataType: data type, comment: field comment information; Step 2: Create the DbMappingBean class, which is used to initialize database information and map related table fields to related bean objects. This class has a static member variable tableFullInfoMap of type Map <String,Map<String,TableInfo> >, this class implements the ApplicationContextAware interface and implements its method setApplicationContext(ApplicationContext applicationContext); First, based on the fully qualified name of the interface class as a parameter, use the Class.forName(String className) method to get the object of the current interface class. For example, enter the fully qualified class name of the current interface class: com.saicmobility.sr.srpassengerservice.api.SrPassengerService to get the SrPassengerService class, thus obtaining all the information of the current interface class. Since we are doing interface testing, we only need to get all the interface methods of the current object. Because it is an interface class, there will be no private methods. For each interface call, the sub-URL is the current interface class name plus the method name. For example, if the current method is getPassengerInfo, then the sub-URL ends with / SrPassengerService / getPassengerInfo. Through the interface class obtained above, use Class.getDeclaredMethods() to get a list of all methods in the current class, which is presented in array form (Method[]), recorded as methods; Declare a Map<String,Object> Object, used to store the final generated assembly parameter information, recorded as paramMap; Traverse the above interface method array methods to assemble the internal input parameters. This is relatively complicated and needs to be completed in many steps. In the for loop of the traversed object array, first get all the input parameter types of the current method through method.getParameterTypes(), which is in the format of a class array generic object (Class<?>[]), recorded as parameterTypes, and declare Map at the same time<String,Object> The object stores the current traversal method parameter, recorded as rawMap; Traverse the above input parameter type array parameterTypes, and in the for loop, use the Class.forName(String className) method again to obtain the input parameter object class. The className here is obtained through the getName() of the traversed input parameter type, and the obtained object class is recorded as parameterClass; Call the getDeclaredFields() method on the parameter class parameterClass obtained above to obtain an array of all declared member variables (Field[]) in the current parameter class, recorded as fields. This is the deep-level parameter field we need to obtain. Traverse the member variable array fields above. In the for loop, the member variable Field needs to be classified and judged. If the object is of the following types: int.class, Integer.class, Object.class, List.class, long.class, Long.class, double.class, Double.class, LazyStringList.

Citation Information

Cited By

  • Database entity object-based ORM DAO code automatic generation method

    CN121029144A