MyBatis Plus-6-Code Generator

Code generator

1.1 Code Generator

MP provides a large number of custom settings, and the generated code can fully meet all kinds of requirements.

MP code generator can be generated: entity class (can choose whether to support AR), Mapper interface, Mapper mapping file, Service layer, Controller layer.

2.1 Primary Key Policy Selection

MP supports the following four primary key policies

value describe
IdType.AUTO Self-increasing database ID
IdType.INPUT User input ID
IdType.ID_WORKER Global unique ID, empty automatic filling (default configuration)
IdType.UUID Global unique ID with empty content filled automatically

3.1 Naming Policy Selection for Tables and Fields

In MP, we recommend hump naming for database table name and table field name, global underscore switch for underscore naming, annotation specifying for inconsistent table name segment name, and we recommend consistency.

The reason for doing so is to avoid performance degradation when corresponding to entity classes, so that fields can directly correspond to entity classes without mapping. Of course, if you don't need to consider this performance loss in your project, then it's okay to use the downline, just configure the dbColumnUnderline attribute when generating code.

4.1 Code Generator Dependency

<!-- template engine -->
    <version>Latest version</version>

<!-- Template engine, need to specify mpg.setTemplateEngine(new FreemarkerTemplateEngine()); -->
    <version>Latest version</version>

<!-- MP Core Library -->
    <version>Latest version</version>

5.1 Sample Code

public class TestMPGenerator01 {

     * Code Generation Sample Code
    public void testGenerator() {
        //1. Global configuration
        GlobalConfig config = new GlobalConfig();
        config.setActiveRecord(true) // Whether AR mode is supported or not
                .setAuthor("don") // author
                .setOutputDir("D:\\mybatis-plus\\mp03\\src\\main\\java") // Generation Path
                .setFileOverride(true)  // File coverage
                .setIdType(IdType.AUTO) // Primary key strategy
                .setServiceName("%sService")  // Set whether the initials of the name of the generated service interface are I//IEmployeeService

        //2. Data Source Configuration
        DataSourceConfig dsConfig = new DataSourceConfig();
        dsConfig.setDbType(DbType.MYSQL)  // Setting up the database type

        //3. Policy configuration
        StrategyConfig stConfig = new StrategyConfig();
        stConfig.setCapitalMode(true) //Global capitalization naming
                .setDbColumnUnderline(true)  // Specifies whether the table name field name is underlined
                .setNaming(NamingStrategy.underline_to_camel) // Naming strategy for mapping database tables to entities
                .setInclude("tbl_employee");  // Generated tables

        //4. Package Name Policy Configuration
        PackageConfig pkConfig = new PackageConfig();

        //5. Configuration
        AutoGenerator ag = new AutoGenerator();


        //6. Implementation


Or Reference Official Website


EmployeeService Impl inherits the ServiceImpl class, and mybatis-plus injects EmployeeMapper into us in this way.
In this way, we can use many methods provided by default in the service layer, and we can also call the methods we write in the dao layer to operate the database.

Tags: Programming Database Mybatis MySQL FreeMarker

Posted on Wed, 04 Sep 2019 20:24:02 -0700 by pcjackson06