Reverse engineering of mybatis under IDEA

One: What is retrograde engineering.

    MyBatis is an excellent persistence layer framework that supports custom SQL, stored procedures and advanced mapping. MyBatis avoids almost all JDBC code and manually setting parameters and getting result sets. MyBatis can use simple XML or annotations to configure and map native information, and map interfaces and Java POJOs (Plain Old Java Objects, ordinary Java objects) to records in the database.

    When there are many database tables, the mapper that repeatedly creates pojo objects and simple database table (CRUD) operations is inefficient. Officially, the use of mybatis Generator is given to reversely generate pojo and mapper files based on database tables. convenient development.

    Let's see how to reverse-generate files using Generator.

Two: how to do it.

    1. Create a maven project on IDEA.

    2. Add dependencies in maven.

        They are mybatis-generator-maven-plugin plugin, log4j, mybatis, mysql, mybatis-generator dependencies.

<build>
    <plugins>
      <!-- mybatis-generator automatically generates code plugin -->
      <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.6</version>
      </plugin>
    </plugins>
  </build>
  <dependencies>
      <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
      </dependency>
      <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.2.3</version>
      </dependency>
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>6.0.6</version>
      </dependency>
    <dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.3.6</version>
    </dependency>
  </dependencies>

    3: Modify the generationConfig.xml file.

<?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="MyBatis">
            <commentGenerator>
                <!-- Whether to remove automatically generated comments true: yes: false: no -->
                <property name="suppressAllComments" value="true" />
            </commentGenerator>
            <!--Database connection information: driver class, connection address, user name, password-->
            <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                            connectionURL="jdbc:mysql://localhost:3306/mybatisGenerator?
                                serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true" userId="root"
                            password="12345">
            </jdbcConnection>

            <!-- Default false, resolve JDBC DECIMAL and NUMERIC types to Integer, when true, JDBC DECIMAL and NUMERIC
                NUMERIC type resolves to java.math.BigDecimal -->
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>

            <!-- targetProject: The location where the PO class is generated -->
            <javaModelGenerator targetPackage="pojo"
                                targetProject=".\src">
                <!-- enableSubPackages: Whether to use schema as the suffix of the package -->
                <property name="enableSubPackages" value="false" />
                <!-- Whitespace before and after the value returned from the database is sanitized -->
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
            <!-- targetProject: where the mapper mapping file is generated -->
            <sqlMapGenerator targetPackage="mapper"
                             targetProject=".\src">
                <!-- enableSubPackages: Whether to use schema as the suffix of the package -->
                <property name="enableSubPackages" value="false" />
            </sqlMapGenerator>
            <!-- targetPackage: The location where the mapper interface is generated -->
            <javaClientGenerator type="XMLMAPPER"
                                 targetPackage="mapper"
                                 targetProject=".\src">
                <!-- enableSubPackages: Whether to use schema as the suffix of the package -->
                <property name="enableSubPackages" value="false" />
            </javaClientGenerator>
            <!-- Specify database table -->
            <table tableName="items"></table>
            <table tableName="orders"></table>
            <table tableName="orderdetail"></table>
            <table tableName="user"></table>
          
            <!-- Some table fields need to specify java type
             <table schema="" tableName="">
                <columnOverride column="" javaType="" />
            </table> -->
        </context>
    </generatorConfiguration>

    4: Officially provided java classes.

public class Generator {
    public void generator() throws Exception{
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
       /**Point to the reverse engineered configuration file*/
        File configFile = new File("src/generatorConfig.xml");
        ConfigurationParser parser = new ConfigurationParser(warnings);
        Configuration config = parser.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 {
            Generator generatorSqlmap = new Generator();
            generatorSqlmap.generator();
        } catch (Exception e) {
            e.printStackTrace ();
        }
    }
}

    5: Run the main method to generate pojo and mapperr objects.


Three: Summarize the problems that arise.

    1、

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. 

    MySQL 6.0.6 is used here, which stipulates that

driverClass="com.mysql.cj.jdbc.Driver"
    2、
java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one
time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property)
to use a more specifc time zone value if you want to utilize time zone support.
   

    Time zone time difference problem, serverTimezone=UTC", otherwise, an error will be reported due to time zone problems. Here, the CTT China Taiwan time zone is used to avoid the 8-hour time difference.

connectionURL="jdbc:mysql://localhost:3306/mybatisGenerator?
                            serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true"

mybatis generator official website http://www.mybatis.org/generator/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325716580&siteId=291194637