Detailed explanation of generator.xml configuration of Mybatis

 

<?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">
<!-- Configuration Generator -->
<generatorConfiguration>
<!-- Can be used to load configuration items or configuration files, and you can use ${propertyKey} to reference configuration items in the entire configuration file
    resource: configure the resource load address, use resource, MBG starts to find from the classpath, such as com/myproject/generatorConfig.properties       
    url: Configure resource loading geology, using URL method, such as file:///C:/myfolder/generatorConfig.properties.
    Note that only one of the two properties can be located;
 
    In addition, if mybatis-generator-maven-plugin is used, the properties defined in pom.xml can be used directly in generatorConfig.xml
<properties resource="" url="" />
-->
 
<!-- When MBG is working, additional dependencies need to be loaded
     The location attribute specifies the full path to load the jar/zip package
<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />
  -->
 
<!--
    context: the environment in which a set of objects is generated
    id: required, context id, used to prompt when an error is generated
    defaultModelType: specifies the style of the generated object
        1, conditional: similar to hierarchical;
        2, flat: all content (primary key, blob), etc. are all generated in one object;
        3. Hierarchical: The primary key generates an XXKey object (key class), Blob, etc. generate a separate object, and other simple attributes are in one object (record class)
    targetRuntime:
        1. MyBatis3: The default value, which generates content based on versions above MyBatis3.x, including XXXBySample;
        2, MyBatis3Simple: similar to MyBatis3, but does not generate XXXBySample;
    introspectedColumnImpl: class fully qualified name, used to extend MBG
-->
<context id="mysql" defaultModelType="hierarchical" targetRuntime="MyBatis3Simple" >
	<commentGenerator>
		<!-- Whether to remove automatically generated comments true: yes: false: no -->
		<property name="suppressAllComments" value="true" />
	</commentGenerator>
    <!-- Automatically identify database keywords, the default is false, if set to true, according to the keyword list defined in SqlReservedWords;
        Generally keep the default value, when encountering database keywords (Java keywords), use columnOverride to override
     -->
    <property name="autoDelimitKeywords" value="false"/>
    <!-- The encoding of the generated Java file -->
    <property name="javaFileEncoding" value="UTF-8"/>
    <!-- Format java code-->
    <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
    <!-- Format XML code-->
    <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
 
    <!-- beginningDelimiter and endingDelimiter: indicate the symbols of the database used to mark database object names, such as ORACLE is double quotes, MYSQL defaults to `backticks; -->
    <property name="beginningDelimiter" value="`"/>
    <property name="endingDelimiter" value="`"/>
 
    <!-- Required, use this configuration to link the database
        @TODO: Is it possible to extend
     -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql:///pss" userId="root" password="admin">
        <!-- The property property can be set here, and each property property is set to the configured Driver -->
    </jdbcConnection>
 
    <!-- java type handler
        For processing types in DB to types in Java, JavaTypeResolverDefaultImpl is used by default;
        Note that by default, it will try to use Integer, Long, Short, etc. to correspond to the DECIMAL and NUMERIC data types;
    -->
    <javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl">
        <!--
            true: use BigDecimal for DECIMAL and NUMERIC data types
            false: default,
                scale>0;length>18:使用BigDecimal;
                scale=0;length[10,18]: use Long;
                scale=0;length[5,9]: use Integer;
                scale=0;length<5: use Short;
         -->
        <property name="forceBigDecimals" value="false"/>
    </javaTypeResolver>
 
 
    <!-- java model creator, is a required element
        Responsible for: 1, key class (see defaultModelType of context); 2, java class; 3, query class
        targetPackage: The package to be placed in the generated class, the real package is controlled by the enableSubPackages attribute;
        targetProject: target project, specify an existing directory, the generated content will be placed in the specified directory, if the directory does not exist, MBG will not automatically create a directory
     -->
    <javaModelGenerator targetPackage="com._520it.mybatis.domain" targetProject="src/main/java">
        <!--  for MyBatis3/MyBatis3Simple
            Automatically create a constructor for each generated class that includes all fields; instead of using setters;
         -->
        <property name="constructorBased" value="false"/>
 
        <!-- On the basis of targetPackage, another layer of package is generated according to the schema of the database, and the final generated class is placed under this package, the default is false -->
        <property name="enableSubPackages" value="true"/>
 
        <!-- for MyBatis3 / MyBatis3Simple
            whether to create an immutable class, if true,
            Then MBG will create a class without a setter method, instead a class like constructorBased
         -->
        <property name="immutable" value="false"/>
 
        <!-- set a root object,
            If this root object is set, the generated keyClass or recordClass will inherit this class; this option can be overridden in the rootClass property of Table
            Note: If there are the same attributes as the root class in the key class or record class, MBG will not regenerate these attributes, including:
                1. The property names are the same, the types are the same, and the getter/setter methods are the same;
         -->
        <property name="rootClass" value="com._520it.mybatis.domain.BaseDomain"/>
 
        <!-- Set whether to call the trim() method on the String type field in the getter method -->
        <property name="trimStrings" value="true"/>
    </javaModelGenerator>
 
 
    <!-- XML ​​file generator that generates SQL maps,
        Note that after Mybatis3, we can use mapper.xml file + Mapper interface (or without mapper interface),
            Or just use the Mapper interface + Annotation, so if the javaClientGenerator configuration is configured to generate XML, this element must be configured
        targetPackage/targetProject:同javaModelGenerator
     -->
    <sqlMapGenerator targetPackage="com._520it.mybatis.mapper" targetProject="src/main/resources">
        <!-- On the basis of targetPackage, another layer of package is generated according to the schema of the database, and the final generated class is placed under this package, the default is false -->
        <property name="enableSubPackages" value="true"/>
    </sqlMapGenerator>
 
 
    <!-- For mybatis, the Mapper interface is generated. Note that if this element is not configured, the Mapper interface will not be generated by default.
        targetPackage/targetProject:同javaModelGenerator
        type: choose how to generate the mapper interface (under MyBatis3/MyBatis3Simple):
            1. ANNOTATEDMAPPER: It will be created using the Mapper interface + Annotation (SQL is generated in the annotation), and the corresponding XML will not be generated;
            2. MIXEDMAPPER: Using the mixed configuration, the Mapper interface will be generated, and the appropriate Annotation will be added appropriately, but the XML will be generated in the XML;
            3. XMLMAPPER: The Mapper interface will be generated, and the interface is completely dependent on XML;
        Note that if the context is MyBatis3Simple: only ANNOTATEDMAPPER and XMLMAPPER are supported
    -->
    <javaClientGenerator targetPackage="com._520it.mybatis.mapper" type="ANNOTATEDMAPPER" targetProject="src/main/java">
        <!-- On the basis of targetPackage, another layer of package is generated according to the schema of the database, and the final generated class is placed under this package, the default is false -->
        <property name="enableSubPackages" value="true"/>
 
        <!-- You can add a parent interface to all generated interfaces, but MBG is only responsible for generating, not checking
        <property name="rootInterface" value=""/>
         -->
    </javaClientGenerator>
 
    <!-- Select a table to generate related files, there can be one or more tables, and there must be table elements
        The selected table will generate the following file:
        1. SQL map file
        2. Generate a primary key class;
        3. Classes of other fields except BLOB and primary key;
        4. Classes that contain BLOBs;
        5. A condition class for user-generated dynamic query (selectByExample, deleteByExample), optional;
        6, Mapper interface (optional)
 
        tableName (required): the name of the table to generate the object;
        Note: case sensitive issue. Under normal circumstances, MBG will automatically recognize the case sensitivity of database identifiers.
            To query the data table according to the set schema, catalog or tablename, follow the following process:
            1. If there are spaces in the schema, catalog or tablename, then what format is set, use the specified case format to query accurately;
            2. Otherwise, if the identifier of the database is in uppercase, then MBG will automatically change the table name to uppercase and then search;
            3. Otherwise, if the identifier of the database is in lowercase, then MBG will automatically change the table name to lowercase and then search;
            4. Otherwise, query using the specified case format;
        In addition, if the "" is used to specify the case of the database object when creating a table, even if the database identifier is in uppercase, in this case, the table name will be created using the given case;
        At this time, please set delimitIdentifiers="true" to preserve case format;
 
        Optional:
        1, schema: the schema of the database;
        2, catalog: the catalog of the database;
        3, alias: the alias set for the data table, if alias is set, then in all the generated SELECT SQL statements, the column name will become: alias_actualColumnName
        4. domainObjectName: The name of the generated domain class. If it is not set, the table name will be used directly as the name of the domain class; it can be set to somepck.domainName, then the domainName class will be automatically placed in the somepck package;
        5, enableInsert (default true): specify whether to generate an insert statement;
        6, enableSelectByPrimaryKey (default true): specify whether to generate a statement to query objects according to the primary key (that is, getById or get);
        7, enableSelectByExample (default true): MyBatis3Simple is false, specifying whether to generate dynamic query statements;
        8, enableUpdateByPrimaryKey (default true): specify whether to generate a statement that modifies the object according to the primary key (ie update);
        9, enableDeleteByPrimaryKey (default true): specify whether to generate a statement to delete objects according to the primary key (ie delete);
        10, enableDeleteByExample (default true): MyBatis3Simple is false, specifying whether to generate a dynamic delete statement;
        11, enableCountByExample (default true): MyBatis3Simple is false, specifying whether to generate a dynamic query total number statement (total number query for paging);
        12, enableUpdateByExample (default true): MyBatis3Simple is false, specifying whether to generate a dynamic modification statement (only modify the attributes that are not empty in the object);
        13, modelType: refer to the defaultModelType of the context element, which is equivalent to overwriting;
        14. delimitIdentifiers: Refer to the explanation of tableName. Note that the default delimitIdentifiers are double quotes. If a database like MYSQL uses ` (backticks, then you also need to set the beginningDelimiter and endingDelimiter properties of the context)
        15, delimitAllColumns: Set whether all the column names in the generated SQL are caused by identifiers. The default is false, delimitIdentifiers refer to the properties of the context
 
        Note that many parameters in the table are a copy of the default properties of elements such as javaModelGenerator, context, etc.;
     -->
    <table tableName="userinfo" >
 
        <!-- Refer to the constructorBased property of javaModelGenerator-->
        <property name="constructorBased" value="false"/>
 
        <!-- The default is false, if set to true, the table name will not add catalog or schema in the generated SQL; -->
        <property name="ignoreQualifiersAtRuntime" value="false"/>
 
        <!-- Refer to the immutable property of javaModelGenerator-->
        <property name="immutable" value="false"/>
 
        <!-- Specify whether to generate only the domain class. If set to true, only the domain class will be generated. If sqlMapGenerator is also configured, in the mapper XML file, only the resultMap element will be generated -->
        <property name="modelOnly" value="false"/>
 
        <!-- Refer to the rootClass property of javaModelGenerator
        <property name="rootClass" value=""/>
         -->
 
        <!-- Refer to the rootInterface property of javaClientGenerator
        <property name="rootInterface" value=""/>
        -->
 
        <!-- If runtimeCatalog is set, then in the generated SQL, use the specified catalog instead of the catalog on the table element
        <property name="runtimeCatalog" value=""/>
        -->
 
        <!-- If runtimeSchema is set, then in the generated SQL, use the specified schema instead of the schema on the table element
        <property name="runtimeSchema" value=""/>
        -->
 
        <!-- If runtimeTableName is set, then in the generated SQL, use the specified tablename instead of the tablename on the table element
        <property name="runtimeTableName" value=""/>
        -->
 
        <!-- Note that this property is only useful for MyBatis3Simple;
            If the selected runtime is MyBatis3Simple, a SelectAll method will be generated. If selectAllOrderByClause is specified, the specified order condition will be added to the SQL;
         -->
        <property name="selectAllOrderByClause" value="age desc,username asc"/>
 
        <!-- If set to true, the generated model class will directly use the name of the column itself, instead of using the camel case naming method, such as BORN_DATE, the generated attribute name is BORN_DATE, not bornDate -->
        <property name="useActualColumnNames" value="false"/>
 
 
        <!-- generatedKey is used to generate the method for generating the primary key,
            If this element is set, MBG will generate a correct <selectKey> element in the generated <insert> element, which is optional
            column: the column name of the primary key;
            sqlStatement: The selectKey statement to be generated, with the following options:
                Cloudscape: The SQL equivalent to selectKey is: VALUES IDENTITY_VAL_LOCAL()
                DB2: The SQL equivalent to selectKey is: VALUES IDENTITY_VAL_LOCAL()
                DB2_MF: The SQL equivalent to selectKey is: SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1
                Derby: The SQL equivalent to selectKey is: VALUES IDENTITY_VAL_LOCAL()
                HSQLDB: The SQL equivalent to selectKey is: CALL IDENTITY()
                Informix: The SQL equivalent to selectKey is: select dbinfo('sqlca.sqlerrd1') from systables where tabid=1
                MySql: The SQL equivalent to selectKey is: SELECT LAST_INSERT_ID()
                SqlServer: The SQL equivalent to selectKey is: SELECT SCOPE_IDENTITY()
                SYBASE: The SQL equivalent to selectKey is: SELECT @@IDENTITY
                JDBC: Equivalent to adding useGeneratedKeys="true" and keyProperty attributes on the generated insert element
        <generatedKey column="" sqlStatement=""/>
         -->
 
        <!--
            This element will rename the column name before calculating the object attribute name based on the column name in the table, which is very suitable for when the columns in the table have a common prefix string,
            For example, the column names are: CUST_ID, CUST_NAME, CUST_EMAIL, CUST_ADDRESS, etc.;
            Then you can set the searchString to "^CUST_" and replace it with blank, then the attribute name in the generated Customer object is not
            custId, custName, etc., are first replaced with ID, NAME, EMAIL, and then become attributes: id, name, email;
 
            Note that MBG uses java.util.regex.Matcher.replaceAll to replace searchString and replaceString,
            If the columnOverride element is used, this attribute has no effect;
 
        <columnRenamingRule searchString="" replaceString=""/>
         -->
 
 
         <!-- is used to modify the attribute of a column in the table, MBG will use the modified column to generate the attribute of domain;
             column: the column name to be reset;
             Note that there can be multiple columnOverride elements in a table element~
          -->
         <columnOverride column="username">
             <!-- Use the property attribute to specify the name of the property to be generated for the column -->
             <property name="property" value="userName"/>
 
             <!-- javaType is used to specify the property type of the generated domain, using the fully qualified name of the type
             <property name="javaType" value=""/>
              -->
 
             <!-- jdbcType is used to specify the JDBC type of the column
             <property name="jdbcType" value=""/>
              -->
 
             <!-- typeHandler is used to specify the TypeHandler used by this column. If you want to specify it, configure the fully qualified name of the type handler
                 Note that in mybatis, the typeHandler in mybatis-config.xml will not be generated
                 It will only generate a parameter description like: where id = #{id,jdbcType=BIGINT,typeHandler=com._520it.mybatis.MyTypeHandler}
             <property name="jdbcType" value=""/>
             -->
 
             <!-- Refer to the delimitAllColumns configuration of the table element, the default is false
             <property name="delimitedColumnName" value=""/>
              -->
         </columnOverride>
 
         <!-- ignoreColumn sets a column ignored by MGB. If the column is set to be changed, the column will not appear in the generated domain and generated SQL
             column: specifies the name of the column to be ignored;
             delimitedColumnName: Refer to the delimitAllColumns configuration of the table element, the default is false
 
             Note that there can be multiple ignoreColumn elements in a table element
         <ignoreColumn column="deptId" delimitedColumnName=""/>
         -->
    </table>
 
</context>
 
</generatorConfiguration>

 

 

Reprinted from: http://www.jianshu.com/p/e09d2370b796

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326668466&siteId=291194637