MyBatis: Reverse Engineering

catalog

1, What is reverse engineering?

Automatically generate the code required for MyBatis execution for the data table, including: Mapper.java (***Repository), Mapper.xml , entity class.

Two. Characteristics of reverse engineering of MyBatis

1. Advantages of MyBatis reverse engineering

Use MyBatis reverse engineering to automatically generate required resources, reduce the workload of developers, so that developers can focus on business logic, rather than creating entity classes, Repository interfaces and other work.

2. Disadvantages of MyBatis reverse engineering

Reverse engineering has its limitations. The method can only be executed once. If it is executed again, resources such as repository interface and entity class will be generated repeatedly. If the table structure needs to be modified, all the generated resources must be deleted and regenerated.

3, MyBatis reverse engineering construction

MBG is a code generator specially designed for the MyBatis framework, which can generate corresponding data quickly according to the data table structure Mapper.xml Mapper interface and entity class support basic CRUD. Complex SQL statements need to be written manually by developers.

1. Add dependency pom.xml

<dependencies>
    <!-- MySQL drive -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.48</version>
    </dependency>

    <!-- MyBatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.0</version>
    </dependency>

    <!-- MBG -->
    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.3.2</version>
    </dependency>
</dependencies>

2. Create MBG profile generatorConfig.xml

  • JDBC connection: configure database connection information.
  • Javamodel generator: configure the generation policy of JavaBean s.
  • sqlMapGenerator: configure the generation policy of SQL map files.
  • javaClientGenerator: configure the generation policy of Mapper interface.
  • Table: configure the data table that needs reverse parsing (tableName: data table name, domainObjectName: class name corresponding to JavaBean)
<?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="testTables" targetRuntime="MyBatis3">
        <!-- Configure database connection information -->
        <jdbcConnection
                driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/mydb?useUnicode=true&amp;characterEncoding=UTF-8"
                userId="root"
                password="root"
        ></jdbcConnection>

        <!-- Entity class -->
        <javaModelGenerator targetPackage="com.spring.entity" targetProject="./src/main/java"></javaModelGenerator>

        <!-- Mapper.xml -->
        <sqlMapGenerator targetPackage="com.spring.repository" targetProject="./src/main/java"></sqlMapGenerator>

        <!-- Mapper.java -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.spring.repository" targetProject="./src/main/java"></javaClientGenerator>

        <!-- data sheet -->
        <table tableName="t_user" domainObjectName="User"></table>
    </context>
</generatorConfiguration>

3. Generate related resources

package com.spring;

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

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GeneratorMain {
    public static void main(String[] args) {
        List<String> warings = new ArrayList<String>();
        boolean overwrite = true;
        String genCig = "/generatorConfig.xml";
        File configFile = new File(GeneratorMain.class.getResource(genCig).getFile());
        ConfigurationParser cp = new ConfigurationParser(warings);
        Configuration config = null;
        try {
            config = cp.parseConfiguration(configFile);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMLParserException e) {
            e.printStackTrace();
        }

        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = null;
        try {
            myBatisGenerator = new MyBatisGenerator(config,callback,warings);
        } catch (InvalidConfigurationException e) {
            e.printStackTrace();
        }

        try {
            myBatisGenerator.generate(null);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Tags: Mybatis Java xml MySQL

Posted on Thu, 04 Jun 2020 06:45:24 -0700 by millsy007