Mybatis generatorconfig auto generate

Document preparation

Using Mybatis Generator requires

  1. Two jar packages -- mybatis-generator-core-1.3.2.jar and database connection jar package
  2. A configuration file GeneratorConfig.xml (encoded in UTF-8)
  3. Execution statement

File download link: https://download.csdn.net/download/evlisric/10559545

There are two ways of paths in the configuration file. Here I use relative paths.

Note that using the relative path requires entering the folder where the configuration file and jar package are located before executing the instruction; you can also use the absolute path.

Core: GeneratorConfig.xml configuration file

Go straight to the code:

<?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>  
<!-- Database driven-->  
    <classPathEntry  location="mysql-connector-java-5.1.25-bin.jar"/>  
    <context id="DB2Tables"  targetRuntime="MyBatis3">  
        <commentGenerator>  
            <property name="suppressDate" value="true"/>  
            <!-- Remove automatically generated comments or not true: Yes, false:no -->  
            <property name="suppressAllComments" value="true"/>  
        </commentGenerator>  

        <!--Database links URL,User name, password -->  
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/schema name "userid =" root "password =" * * * * ">  
        </jdbcConnection>  


         <!-- false: JDBC DECIMAL,NUMERIC Type resolves to Integer,Default mode -->
         <!-- true:  JDBC DECIMAL,NUMERIC Type resolves to java.math.BigDecimal -->
        <javaTypeResolver>  
            <property name="forceBigDecimals" value="false"/>  
        </javaTypeResolver>  
        

        <!-- Package name and location of the build model-->  
        <javaModelGenerator targetPackage="com.zhj.po" targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
            <property name="trimStrings" value="true"/>  
        </javaModelGenerator>  
        

        <!-- Package name and location of the build mapping file-->  
        <sqlMapGenerator targetPackage="com.zhj.mapper" targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
        </sqlMapGenerator>  
        

        <!-- generate DAO Package name and location for-->  
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.zhj.dao" targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
        </javaClientGenerator>  
        

        <!-- Table to generate tableName Is the table or view name in the database domainObjectName Is the entity class name-->  
        <table tableName="blog" domainObjectName="Blog" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>  
</generatorConfiguration>  

Related label analysis


1. If enableCountByExample is set to true, an Example class corresponding to domainObjectName will be generated. If false is not generated, the default policy is true.
Similar properties are enableUpdateByExample, enableDeleteByExample, enableSelectByExample, and selectByExampleQueryId.

2. To operate on some database fields, you can add the following tags to the table tag
(1) Ignore a field
<ignoreColumn column="name" />
(2) Regardless of the type of database field, the generated class property is varchar
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />

generate

Open the cmd window of dos to enter the folder where the file is located (SRC folder needs to be created), enter

Instruction: java -jar mybatis-generator-core-1.3.2.jar -configfile GeneratorConfig.xml -overwrite

If absolute path is used, instruction: java -jar F:/xxx/mybatis-generator-core-1.3.2.jar -configfile F:/xxx/GeneratorConfig.xml -overwrite

Tags: Mybatis Database xml Java

Posted on Fri, 31 Jan 2020 05:03:18 -0800 by gljaber