Method and system for reducing MybaatisPlus template codes and storage medium
By marking database entity mapping classes with annotations and using code generation classes to automatically generate boilerplate code, the problem of boilerplate code redundancy in MyBatisPlus is solved, achieving efficient code generation and good maintainability.
Patent Information
- Application Number
- CN202410669762.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-28
- Publication Date
- 2024-10-11
AI Technical Summary
When using MyBatisPlus, a large amount of repetitive boilerplate code needs to be written, resulting in high code storage costs and poor maintainability.
Mark database entity mapping classes by defining annotations, and use code generation classes to identify these annotations, automatically generate template code classes, including BaseMapper and ServiceImpl classes, and dynamically generate code in a non-invasive way.
It reduces the need to manually write boilerplate code and improves development efficiency and code maintainability.
Smart Images

Figure CN118760433A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a method, system and storage medium for reducing MybatisPlus boilerplate code. Background Art
[0002] MyBatisPlus is a tool enhanced on the basis of MyBatis, which simplifies the developer's operation on the database. MyBatisPlus allows developers to operate the database in an object-oriented way, without having to write complicated SQL statements.
[0003] However, in the process of using MybatisPlus, for database persistence classes, in addition to writing database entity mapping classes, you also need to write implementation classes of the BaseMapper class based on the database entity mapping class, as well as implementation classes that use the BaseMapper and inherit BaseServiceImp, in order to use the full functionality of MybaitsPlus. This results in a large amount of repeated boilerplate code, high code storage costs, and poor maintainability.
[0004] Based on this, a solution needs to be proposed to solve at least one of the above problems. Summary of the invention
[0005] In order to overcome the above-mentioned defects of the prior art, the present invention provides a method, system and storage medium for reducing MybatisPlus boilerplate code.
[0006] The specific technical solutions are as follows:
[0007] A first aspect of the present invention provides a method for reducing MybatisPlus boilerplate code, the method comprising:
[0008] Build an application development environment based on MyBatisPlus;
[0009] Define annotations, and use the annotations to mark database entity mapping classes;
[0010] Define a code generation class, identify the marked database entity mapping class through the code generation class and process the database entity mapping class to generate a template code class under the directory where the database entity mapping class is located;
[0011] The database entity mapping class and the boilerplate code class are compiled to obtain an executable file.
[0012] In a specific embodiment, the “defining code generation class” includes:
[0013] The code generation class is made to inherit the AbstractProcessor interface and implement the abstract methods in the AbstractProcessor interface.
[0014] In a specific embodiment, the code generation class identifies the marked database entity mapping class, including:
[0015] The code generation class scans the source code of all class files, and if the source code of the class file contains the annotation, it is determined that the class file is a marked database entity mapping class.
[0016] In a specific embodiment, the code generation class processes the database entity mapping class, including:
[0017] The loading information of the database entity mapping class is obtained, and the directory where the database entity mapping class is located is determined according to the loading information, and the name of the template code class to be generated is determined.
[0018] In a specific embodiment, the loading information at least includes the class name of the database entity mapping class or the package name of the package where the class is located.
[0019] In a specific embodiment, the template code class includes a first template code class, which inherits from the BaseMapper interface in MyBatisPlus and has a first generic parameter, and the name of the first generic parameter is the same as the name of the database entity mapping class.
[0020] In a specific embodiment, the boilerplate code class includes a second boilerplate code class, which inherits from the Servicelmpl class in MyBatisPlus and has a second generic parameter and a third generic parameter, the name of the second generic parameter is the same as the name of the database entity mapping class, and the name of the third generic parameter is the same as the name of the first boilerplate code class.
[0021] In a specific embodiment, the “building an application development environment based on MyBatisPlus” includes:
[0022] Introduce MybatisPlus dependency in the Maven project under the SpringBoot framework.
[0023] A second aspect of the present invention provides a computer system, the system comprising:
[0024] Development environment construction module, used to build an application development environment based on MyBatisPlus;
[0025] An annotation definition module is used to define annotations and mark database entity mapping classes with the annotations;
[0026] A code generation class definition module, used to define a code generation class, and identify the marked database entity mapping class through the code generation class and process the database entity mapping class to generate a template code class under the directory where the database entity mapping class is located;
[0027] A compiling module is used to compile the database entity mapping class and the template code class to obtain an executable file.
[0028] A third aspect of the present invention provides a computer storage medium storing computer executable instructions for executing the method for reducing MybatisPlus boilerplate code described in the first aspect of the present invention.
[0029] The present invention has at least the following beneficial effects:
[0030] The present invention provides a method, system and storage medium for reducing MybatisPlus boilerplate code, wherein the method is to mark a database entity mapping class by annotation so that a code generation class can identify the database entity mapping class by annotation, thereby automatically generating a boilerplate code class under the directory where the database entity mapping class is located, dynamically generating code in a non-intrusive manner, having high maintainability of the code, and eliminating the need to manually write related boilerplate code, thereby improving development efficiency and having good practical effects. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0032] Figure 1 A schematic flow chart of the method is provided for Example 1;
[0033] Figure 2 This is a schematic diagram of the code generation class in Example 1 processing the database entity mapping class;
[0034] Figure 3 Schematic diagram of the overall structure of the computer system in Example 2.
[0035] Reference numerals:
[0036] 1- Development environment construction module; 2- Annotation definition module; 3- Code generation class definition module; 4- Compilation module; 5- Database entity mapping class; 6- Boilerplate code class. DETAILED DESCRIPTION
[0037] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0038] Example 1
[0039] Please refer to Figure 1 , this embodiment provides a method for reducing MybatisPlus boilerplate code, the method comprising:
[0040] S1: Build an application development environment based on MyBatisPlus. MyBatisPlus is a tool that is further enhanced on the basis of MyBatis. It expands the functions of MyBatis, simplifies development, and provides many practical functions, making interaction with the database more convenient and efficient.
[0041] S2: Define annotations, and use the annotations to mark the database entity mapping class. Annotations are a form of metadata, which can be used on various program elements such as classes, methods, variables, parameters, etc. to provide additional information. By providing additional descriptive information, it can instruct compilers, development tools and other frameworks to perform specific operations in the program. The database entity mapping class is a Java class used to map the database table structure. It should be noted that there is no necessary order between step S1 and step S2, and their order can be swapped or performed simultaneously.
[0042] S3: define a code generation class, through which the marked database entity mapping class is identified and the database entity mapping class is processed to generate a template code class under the directory where the database entity mapping class is located. Specifically, the code generation class can be understood as an annotation processor that can identify annotations, thereby performing a preset operation of generating template code for the database entity mapping class marked by the annotation.
[0043] S4: Compile the database entity mapping class and the boilerplate code class to obtain an executable file.
[0044] It can be understood that this method marks the database entity mapping class through annotations, so that the code generation class can select the database entity mapping class by identifying the annotations, thereby automatically generating a boilerplate code class in the directory where the database entity mapping class is located. It adopts a non-intrusive code generation method, which does not require users to manually write related boilerplate codes, thereby improving development efficiency and code maintainability.
[0045] In a specific embodiment, the "building an application development environment based on MyBatisPlus" described in step S1 may include: introducing MybatisPlus dependencies in a Maven project under the SpringBoot framework.
[0046] Among them, SpringBoot is an open source framework for quickly building applications based on the Spring framework. It simplifies the initial configuration and deployment process of Spring applications, allowing developers to focus more on the implementation of business logic without paying too much attention to tedious configuration. SpringBoot provides a fast and convenient way to build independent, production-level Spring applications.
[0047] The Maven project is a tool for project management and build, which provides a standardized way to build, deploy and manage Java projects.
[0048] Specifically, the MybatisPlus dependency is introduced in the Maven project under the SpringBoot framework, mainly by adding the MybatisPlus dependency in the pom.xml file of the Maven project, generally including mybatis-plus-boot-starter, the core dependency, so that the MybatisPlus jar package can be indirectly imported into the Maven project, thereby realizing the construction of an application development environment based on MyBatisPlus.
[0049] In a specific embodiment, step S2 "defining annotations and marking database entity mapping classes with the annotations" can specifically be: manually defining and writing annotations, writing the defined and written annotations into the source code of the database entity mapping class, thereby marking the database entity mapping class.
[0050] Furthermore, step S3 "defining a code generation class" may include: making the code generation class inherit the AbstractProcessor interface and implementing the abstract methods in the AbstractProcessor interface. The AbstractProcessor interface is an annotation processor interface of Java, which is one of the most critical interfaces in the annotation compile-time processing flow and is located in the javax.annotation.processing package. The logic for processing annotations at compile time needs to implement this interface.
[0051] Specifically, the code generation class can implement the AbstractProcessor interface through the JSR 269 standard, which is a proposal (Java Specification Request) of the Java community, which defines the specifications and standards for annotation processing in the Java platform. The code generation class can be understood as a custom annotation processor at this time. The code generation class inherits the AbstractProcessor interface and implements the abstract method therein. Through the implementation method, the code generation class can identify the database entity mapping class marked by the annotation by identifying the annotation, so as to process the database entity mapping class by executing the code for generating the code, so as to automatically generate the boilerplate code class. Exemplarily, the code generation class can use the JavaPoet library to generate Java source code (boilerplate code class) and write it to the directory where the database entity mapping class is located.
[0052] For further information, please refer to Figure 2 , the code generation class processes the database entity mapping class, which may include:
[0053] Obtain the loading information of the database entity mapping class, determine the directory where the database entity mapping class is located based on the loading information, and determine the name of the template code class to be generated. Exemplarily, the code generation class can use the API in the javax.lang.model package to analyze the structure and properties of the database entity mapping class, thereby obtaining its loading information, the loading information including but not limited to the class name of the database entity mapping class or the package name of the package where the database entity mapping class is located. Since the package name is the same as the name of the directory where it is located, based on the obtained package name, the directory where the database entity mapping class is located can be determined; and based on the obtained class name, the name of the template code class to be generated can be determined. For example, if the class name of the database entity mapping class is "Xxxx", the name of the template code class can be "XxxxABC".
[0054] In a specific embodiment, the automatically generated template code class includes a first template code class, which inherits from the BaseMapper interface in MyBatisPlus and has a first generic parameter, and the name of the first generic parameter is the same as the name of the database entity mapping class. The BaseMapper interface is used in MyBatisPlus to define methods for interacting with database tables.
[0055] Exemplarily, the class name of the database entity mapping class is "Xxxx", and the first template code class inherits com.baomidou.mybatisplus.core.mapper.BaseMapper, and uses the first generic parameter "Xxxx" to indicate that it serves the database entity mapping class Xxxx. The name of the first template code class determined according to the class name of the database entity mapping class is "XxxxMpMapper.java", and the generated XxxxMpMapper.java contains predefined or customized CRUD methods, namely, Create, Read, Update, Delete, etc. These methods can directly operate the table corresponding to the Xxxx database entity mapping class on the database.
[0056] Furthermore, the template code class also includes a second template code class, which inherits from the Servicelmpl class in MyBatisPlus and has a second generic parameter and a third generic parameter, wherein the name of the second generic parameter is the same as the name of the database entity mapping class, and the name of the third generic parameter is the same as the name of the first template code class.
[0057] Exemplarily, the second template code inherits com.baomidou.mybatisplus.extension.service.impl.ServiceImpl, and has a second generic parameter "Xxxx" and a third generic parameter "XxxxMpMapper", and the name of the second template code class determined according to the class name of the database entity mapping class is "XxxxMpServiceImpl.java", and the generated XxxxMpServiceImpl.java is injected with an instance of XxxMpMapper and defines some business methods that operate the database by calling methods in XxxxMpMapper.
[0058] Example 2
[0059] See also Figure 3 , this embodiment provides a computer system, the system comprising:
[0060] Development environment construction module 1, used to build an application development environment based on MyBatisPlus;
[0061] An annotation definition module 2, used to define annotations and mark database entity mapping classes 5 through the annotations;
[0062] A code generation class definition module 3 is used to define a code generation class, and identify the marked database entity mapping class 5 through the code generation class and process the database entity mapping class 5 to generate a template code class 6 under the directory where the database entity mapping class 5 is located;
[0063] The compiling module 4 is used to compile the database entity mapping class 5 and the template code class 6 to obtain an executable file.
[0064] It can be understood that the modules of the present system can also realize non-intrusive dynamic code generation based on the method described in Example 1, without the need for manual boilerplate code compilation, with high development efficiency and good code maintainability.
[0065] Example 3
[0066] This embodiment provides a computer storage medium, in which computer executable instructions are stored. The computer executable instructions are used to execute the method for reducing MybatisPlus boilerplate code described in Example 1.
[0067] In summary, the present invention provides a method, system and storage medium for reducing MybatisPlus boilerplate code, wherein the method is to mark the database entity mapping class by annotation so that the code generation class can identify the database entity mapping class through the annotation, thereby automatically generating a boilerplate code class under the directory where the database entity mapping class is located, and dynamically generating code in a non-intrusive manner, the code is highly maintainable, and there is no need to manually write related boilerplate code, thereby improving development efficiency and having good practical effects.
[0068] It should be understood by those skilled in the art that the modules or steps of the present invention described above can be implemented by a general-purpose computing device, they can be concentrated on a single computing device, or distributed on a network composed of multiple computing devices, optionally, they can be implemented by a program code executable by a computer device, so that they can be stored in a storage device and executed by the computing device, or they can be made into individual integrated circuit modules, or multiple modules or steps therein can be made into a single integrated circuit module for implementation. Thus, the present invention is not limited to any specific combination of hardware and software.
[0069] Note that the above are only preferred embodiments of the present invention and the technical principles used. Those skilled in the art will understand that the present invention is not limited to the specific embodiments herein, and that various obvious changes, readjustments and substitutions can be made by those skilled in the art without departing from the scope of protection of the present invention. Therefore, although the present invention has been described in more detail through the above embodiments, the present invention is not limited to the above embodiments, and may include more other equivalent embodiments without departing from the concept of the present invention, and the scope of the present invention is determined by the scope of the appended claims.
[0070] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principle of the present invention should be included in the protection scope of the present invention.
Claims
1. A method to reduce MybatisPlus boilerplate code, It is characterized in that The method comprises: Build an application development environment based on MyBatisPlus; Define annotations, and use the annotations to mark database entity mapping classes; Define a code generation class, identify the marked database entity mapping class through the code generation class and process the database entity mapping class to generate a template code class under the directory where the database entity mapping class is located; The database entity mapping class and the boilerplate code class are compiled to obtain an executable file.
2. A method for reducing MybatisPlus boilerplate code according to claim 1, It is characterized in that The "definition code generation class" includes: The code generation class is made to inherit the AbstractProcessor interface and implement the abstract methods in the AbstractProcessor interface.
3. A method for reducing MybatisPlus boilerplate code according to claim 1, It is characterized in that The code generation class recognizes the marked database entity mapping class, including: The code generation class scans the source code of all class files, and if the source code of the class file contains the annotation, it is determined that the class file is a marked database entity mapping class.
4. A method for reducing MybatisPlus boilerplate code according to claim 1, It is characterized in that The code generation class processes the database entity mapping class, including: The loading information of the database entity mapping class is obtained, and the directory where the database entity mapping class is located is determined according to the loading information, and the name of the template code class to be generated is determined.
5. A method for reducing MybatisPlus boilerplate code according to claim 4, It is characterized in that The loading information at least includes the class name of the database entity mapping class or the package name of the package where it is located.
6. A method for reducing MybatisPlus boilerplate code according to claim 1 or 4, It is characterized in that The template code class includes a first template code class, which inherits from the BaseMapper interface in MyBatisPlus and has a first generic parameter, and the name of the first generic parameter is the same as the name of the database entity mapping class.
7. A method for reducing MybatisPlus boilerplate code according to claim 5, It is characterized in that The boilerplate code class includes a second boilerplate code class, which inherits from the Servicelmpl class in MyBatisPlus and has a second generic parameter and a third generic parameter, the name of the second generic parameter is the same as the name of the database entity mapping class, and the name of the third generic parameter is the same as the name of the first boilerplate code class.
8. A method for reducing MybatisPlus boilerplate code according to claim 1, It is characterized in that The "building an application development environment based on MyBatisPlus" includes: Introduce MybatisPlus dependency in the Maven project under the SpringBoot framework.
9. A computer system, It is characterized in that The system comprises: Development environment construction module, used to build an application development environment based on MyBatisPlus; An annotation definition module is used to define annotations and mark database entity mapping classes with the annotations; A code generation class definition module, used to define a code generation class, and identify the marked database entity mapping class through the code generation class and process the database entity mapping class to generate a template code class under the directory where the database entity mapping class is located; A compiling module is used to compile the database entity mapping class and the template code class to obtain an executable file.
10. A computer storage medium, It is characterized in that The computer storage medium stores computer executable instructions, and the computer executable instructions are used to execute the method for reducing MybatisPlus boilerplate code as described in any one of claims 1 to 8.
