MyBatis -- generator reverse engineering

Keywords: Java Mybatis xml Database

I. Introduction

Official website: http://www.mybatis.org/generator/index.html

By using the official mapper auto generation tool, mybatis-generator-core-1.3.2, po classes and mapper mapping files are automatically generated.

Function: mybatis officially provides reverse engineering, which can be used to automatically generate Mapper interface, mapping files (single table addition, deletion, modification and query) and Po classes through tables in the database

1. jar package to be imported

  

2.mapper generates configuration file

The details of mapper generation need to be configured in generatorConfig.xml. The main points need to be modified are as follows:

1) add the database table to be generated

2) the path of the package where the po file is located

3) path of the package where the mapper file is located

generatorConfig.xml is configured as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <context id="MybatisGenerator" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- Remove automatically generated comments or not -->
            <property name="supressAllComments" value="true" />
        </commentGenerator>

        <!-- Database connection information: driver class, connection address, user name, password -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/springmvc?serverTimezone=UTC"
            userId="root" password="950806">
        </jdbcConnection>

        <!-- Default value -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- Generated po Storage location of entity class -->
        <javaModelGenerator targetPackage="po"
            targetProject=".\src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- Generated mapper Mapping files( XML Storage location of documents) -->
        <sqlMapGenerator targetPackage="mapper"
            targetProject=".\src">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <!-- Generated mapper Interface ( JAVA Storage location of documents) -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="mapper" targetProject=".\src">
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!-- Specify database tables -->
        <table tableName="items" />
        <table tableName="users" />

    </context>
</generatorConfiguration>

3. After configuration, write java class to generate mapper file

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

public class MyGenerator {
    public void generate() throws Exception {

        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("GeneratorConfig.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);

    }

    public static void main(String[] args) throws Exception {
        MyGenerator myGenerator = new MyGenerator();
        myGenerator.generate();
    }

}

4. Execute java program to automatically generate corresponding mapper mapping file and po file for table data in database

Refresh package structure: po file and mapper mapping file are generated

Posted by sumathi on Wed, 04 Dec 2019 21:56:30 -0800