This paper introduces the reverse engineering of MyBatis, introduces two startup modes and provides an example of MBG configuration file. Reverse engineering automatically creates three components through the database to reduce development workload and improve development efficiency.

Introduction to the

Official document: mybatis.org/generator/

Official code: github.com/mybatis/gen…

MyBatis framework needs: entity class, custom Mapper interface, mapper.xml

In traditional development, developers need to create three components manually. Reverse engineering can help developers to automatically create three components, reducing development workload and improving development efficiency

Maven plugin startup mode (no startup class required)

The generator plugin

 <properties>
     <mysql.connect.version>8.0.25</mysql.connect.version>
     <mybatis.generator.plugin.version>1.4.0</mybatis-plus.version>
     <mybatis.generator.vision>1.4.0</mybatis-plus.generator.vision>
 </properties>
 ​
 <build>
     <plugins>
         <plugin>
             <groupId>org.mybatis.generator</groupId>
             <artifactId>mybatis-generator-maven-plugin</artifactId>
             <version>${mybatis.generator.plugin.version}</version>
             <configuration>
                 <! -- Specify the location of MBG configuration file Generatorconfig.xml (need to modify)-->
                 <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                 <! Print build procedure to console -->
                 <verbose>true</verbose>
                 <! *.java.1, *.java.2, *.java.2
                 <overwrite>true</overwrite>
             </configuration>
             <executions>
                 <execution>
                     <id>Generate MyBatis Artifacts</id>
                     <goals>
                         <goal>generate</goal>
                     </goals>
                 </execution>
             </executions>
             <dependencies>
                 <! - mysql driver - >
                 <dependency>
                     <groupId>mysql</groupId>
                     <artifactId>mysql-connector-java</artifactId>
                     <version>${mysql.connect.version}</version>
                 </dependency>
                 <! -- Mybatis code generator -->
                 <dependency>
                     <groupId>org.mybatis.generator</groupId>
                     <artifactId>mybatis-generator-core</artifactId>
                     <version>${mybatis.generator.version}/<version>
                 </dependency>
             </dependencies>
         </plugin>
     </plugins>
 </build>
Copy the code

Main Boot mode (boot class required)

Import dependence

 <dependencies>
     <! - mysql driver - >
     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>8.0.11</version>
     </dependency>
     <! --mybatis-->
     <dependency>
         <groupId>org.mybatis</groupId>
         <artifactId>mybatis</artifactId>
         <version>3.4.5</version>
     </dependency>
     <! -- Mybatis code generator -->
     <dependency>
         <groupId>org.mybatis.generator</groupId>
         <artifactId>mybatis-generator-core</artifactId>
         <version>1.3.2</version>
     </dependency>
 </dependencies>
Copy the code

Start the class

Generator.java

 public class Generator {
     public static void main(String[] args) throws InterruptedException, SQLException, IOException, XMLParserException, InvalidConfigurationException {
         List<String> warnings = new ArrayList<>();  // Warning information during execution
         boolean overwrite = true;   // If the generated code is duplicated, override the original code
         // Specify the location of the configuration file
         String projectPath = System.getProperty("user.dir"); // Get the current project path
         String configFilePath = projectPath + "/src/main/resources/generatorConfig.xml"; / / need to be modified
         File configFile = new File(configFilePath);
         ConfigurationParser cp = new ConfigurationParser(warnings);
         Configuration config = cp.parseConfiguration(configFile);
         DefaultShellCallback callback = new DefaultShellCallback(overwrite);
         // Mybatis generator-core relies on 1.3.6 for MyBatisGenerator, which requires JRE1.8 to run
         MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
         myBatisGenerator.generate(null);        // Execute the generated code
         warnings.forEach(System.out::println);  // Displays a warning message}}Copy the code

Create an MBG configuration file

Core Configuration Description

  1. JdbcConnection: Configures database connection information

  2. JavaModelGenerator: Configures the generation policy for Javabeans

  3. SqlMapGenerator: Configures the SQL mapping file generation policy

  4. JavaClientGenerator: Configures a Mapper interface generation policy

  5. Table: configures the target database

    • tableName: the name of the table
    • domainObjectName: a JavaBean class name

generatorConfig.xml

 
      
 <! DOCTYPEgeneratorConfiguration
   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
 ​
 <generatorConfiguration>
     <! -- Specify the location of the JDBC driver (not required with Maven dependencies)-->
     <! --<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />-->
 ​
     <context id="mysqlTables" targetRuntime="MyBatis3">
         <! --jdbcConnection: specify the target database to connect to -->
         <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                         connectionURL="JDBC: mysql: / / localhost: 3306 / database name? serverTimezone=UTC&useUnicode=true&useSSL=false&characterEncoding=utf8"
                         userId="root"
                         password="Password">
         </jdbcConnection>
         
         <! -- Resolve JDBC DECIMAL and NUMERIC types to Integer when the default is false; Parse JDBC DECIMAL and NUMERIC types to java.math.bigDecimal when true -->
         <javaTypeResolver>
             <property name="forceBigDecimals" value="false" />
         </javaTypeResolver>
 ​
         <! --javaModelGenerator: Specifies the generation strategy for Javabeans (entity classes); TargetPackage: targetPackage name; TargetProject -->
         <javaModelGenerator targetPackage="com.mybatis.pojo" targetProject="./src/main/java">
             <property name="enableSubPackages" value="true"/>
             <! -- whitespace before and after the value returned from the database is cleaned -->
             <property name="trimStrings" value="true" />
         </javaModelGenerator>
 ​
         <! SQL MapGenerator -->
         <sqlMapGenerator targetPackage="com.mybatis.mapper"  targetProject="./src/main/resources">
             <property name="enableSubPackages" value="true"/>
         </sqlMapGenerator>
 ​
         <! JavaClientGenerator: Specify mapper interface location -->
         <javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatis.mapper"  targetProject="./src/main/java">
             <property name="enableSubPackages" value="true"/>
         </javaClientGenerator>
 ​
         <! Create javaBean from table -->
         <table tableName="m_user" domainObjectName="User"></table>
 ​
     </context>
 </generatorConfiguration>
Copy the code

Reference Sources:

Extremely brief introduction 】 【 4 hours to MyBatis | Nantucket sound elder brother teach you to learn Java