Generator SqlMapCustom (mybatis reverse engineering)

Keywords: Java log4j Mybatis xml

Generator SqlMapCustom can be used as a tool to automatically load POJO, mapper interface and corresponding xml. First, paste the source link.

Click here: GitHub Source Link

Look at the source code annotations for usage. The contents of the project are as follows:

GeneratorSqlmap.java


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 GeneratorSqlmap {

    public void generator() throws Exception {

    	List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        //Specify reverse engineering configuration files
        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();
        }

    }
}

log4j.properties

log4j.rootLogger=DEBUG, Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

generatorConfig.xml

<?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">
        <commentGenerator>
            <!-- Whether to remove automatically generated comments true: Yes, false:no -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        
        <!--Information about database connection: driver class, connection address, user name, password -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/bookstore"
                        userId="root"
                        password="763081703">
        </jdbcConnection>
       
        <!-- default false,hold JDBC DECIMAL and NUMERIC Type resolution is Integer,by true Shi Ba JDBC DECIMAL and
            NUMERIC Type resolution is java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- targetProject:generate PO Class location -->
        <javaModelGenerator targetPackage="cn.dave.bookstore.pojo"
                            targetProject=".\src">
            <!-- enableSubPackages:Whether to let schema Suffixes for packages -->
            <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 mapping file generation -->
        <sqlMapGenerator targetPackage="cn.dave.bookstore.mapper"
                         targetProject=".\resource">
            <!-- enableSubPackages:Whether to let schema Suffixes for packages -->
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>
        <!-- targetPackage: mapper Location of interface generation -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="cn.dave.bookstore.mapper"
                             targetProject=".\src">
            <!-- enableSubPackages:Whether to let schema Suffixes for packages -->
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>
        <!-- Specify database tables -->
        <table tableName="admin"></table>
        <table tableName="book"></table>
        <table tableName="category"></table>
        <table tableName="orderitem"></table>
        <table tableName="orders"></table>
        <table tableName="tb_user"></table>
        <!-- <table schema="" tableName="sys_user"></table>
        <table schema="" tableName="sys_role"></table>
        <table schema="" tableName="sys_permission"></table>
        <table schema="" tableName="sys_user_role"></table>
        <table schema="" tableName="sys_role_permission"></table> -->

        <!-- Some table fields need to be specified java type
         <table schema="" tableName="">
            <columnOverride column="" javaType="" />
        </table> -->
    </context>
</generatorConfiguration>

Two possible mistakes:

1. Exception in thread "main" java.lang.Unsupported Class Version Error: com/devin/app/Generator: Unsupported major.minor version 52.0 This is because the compiler environment used for this project is jdk1.7, and you may be jdk1.8. Solution: Right-click on the project --> Properties > java compiler --> Enproject settings --> compiler compliance level will be compiler compliance level.

2.No embedded stylesheet instruction for file

The current open document is xml file when you click on it to run. Solution: Close the xml file and run it again

Posted by Kondie on Sat, 05 Oct 2019 07:55:21 -0700