MyBatis: Reverse Engineering


1, What is reverse engineering?

Automatically generate the code required for MyBatis execution for the data table, including: (***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

    <!-- MySQL drive -->

    <!-- MyBatis -->

    <!-- MBG -->

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 Generator Configuration 1.0//EN" "">

    <context id="testTables" targetRuntime="MyBatis3">
        <!-- Configure database connection information -->

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

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

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

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

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.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) {
        } catch (XMLParserException e) {

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

        try {
        } catch (SQLException e) {
        } catch (IOException e) {
        } catch (InterruptedException e) {

Tags: Mybatis Java xml MySQL

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