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 -->
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-engine-core</artifactId>
    <version>Latest version</version>
</dependency>

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

<!-- MP Core Library -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>Latest version</version>
</dependency>

5.1 Sample Code

public class TestMPGenerator01 {

    /**
     * Code Generation Sample Code
     */
    @Test
    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
                .setBaseResultMap(true)
                .setBaseColumnList(true);

        //2. Data Source Configuration
        DataSourceConfig dsConfig = new DataSourceConfig();
        dsConfig.setDbType(DbType.MYSQL)  // Setting up the database type
                .setDriverName("com.mysql.jdbc.Driver")
                .setUrl("jdbc:mysql://localhost:3306/mp")
                .setUsername("root")
                .setPassword("123456");

        //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
                .setTablePrefix("tbl_")
                .setInclude("tbl_employee");  // Generated tables

        //4. Package Name Policy Configuration
        PackageConfig pkConfig = new PackageConfig();
        pkConfig.setParent("com.don.mp")
                .setMapper("mapper")
                .setService("service")
                .setController("controller")
                .setEntity("beans")
                .setXml("mapper");

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

        ag.setGlobalConfig(config)
                .setDataSource(dsConfig)
                .setStrategy(stConfig)
                .setPackageInfo(pkConfig);

        //6. Implementation
        ag.execute();
    }

}

Or Reference Official Website

Note:

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