MyBatis reverse engineering -- automatic generation of Java code

Keywords: Java Mybatis xml Database

As for automatic code generation, there are many tools and different methods on the Internet. MyBatis is the first automatic code generator I have come into contact with. It's relatively simple, with less code, and it's easy to understand. After that, I won't talk about much nonsense. Let's talk about the implementation method and code explanation:

Let's create a common Java project. The tool I use here is idea, which is a very powerful code writing tool.

The first is to realize the jar package needed for automatic code generation

Second, write a Java class

import java.io.File;
import java.io.IOException;
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.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

public class GeneratorSqlmap {

public void generator() throws Exception{
    List<String> warnings = new ArrayList<String>();
    boolean overwrite = true;
    //Specify reverse engineering profile
    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 {
    try {
        GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
        generatorSqlmap.generator();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

There is also a profile

<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

    <!-- targetProject:generate POJO Class location -->
    <javaModelGenerator targetPackage="cn.lht.pojo"
                        targetProject=".\src">
        <!-- enableSubPackages:Whether to let schema As suffix of package -->
        <property name="enableSubPackages" value="false" />
        <!-- Spaces before and after values returned from the database are cleaned up -->
        <property name="trimStrings" value="true" />
    </javaModelGenerator>
    <!-- targetProject:mapper Location of map file generation -->
    <sqlMapGenerator targetPackage="cn.lht.mapper"
                     targetProject=".\src">
        <!-- enableSubPackages:Whether to let schema As suffix of package -->
        <property name="enableSubPackages" value="false" />
    </sqlMapGenerator>
    <!-- targetPackage: mapper Location of interface generation -->
    <javaClientGenerator type="XMLMAPPER"
                         targetPackage="cn.lht.mapper"
                         targetProject=".\src">
        <!-- enableSubPackages:Whether to let schema As suffix of package -->
        <property name="enableSubPackages" value="false" />
    </javaClientGenerator>

    <!-- Specify database tables -->
    <table schema="" tableName="tb_content"></table>
    <table schema="" tableName="tb_content_category"></table>
    <table schema="" tableName="tb_item"></table>
    <table schema="" tableName="tb_item_cat"></table>
    <table schema="" tableName="tb_item_desc"></table>
    <table schema="" tableName="tb_item_param"></table>
    <table schema="" tableName="tb_item_param_item"></table>
    <table schema="" tableName="tb_order"></table>
    <table schema="" tableName="tb_order_item"></table>
    <table schema="" tableName="tb_order_shipping"></table>
    <table schema="" tableName="tb_user"></table>

</context>

Note that this configuration file is the same as the src directory

I explained the specific code comments in the code

Then run it

Note: if you have run it once, but the generated code needs to be changed and needs to be generated again, you must delete all the generated code and package structure that have been run before, otherwise an error will occur.

If you have comments or a better way to generate code automatically, please comment.

Posted by phychion on Thu, 05 Dec 2019 16:45:50 -0800