Mybatis Generator most perfect profile details of full version

  • 2020-05-12 02:44:47
  • OfStack

I haven't done any project recently, but I have reorganized the most complete configuration file of Mybatis Generator (MBG for short) with detailed explanation. I don't need to look at User Guide of EN anymore.


<?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>
<!--  It can be used to load configuration items or configuration files, which can be used throughout a configuration file ${propertyKey} Is used to reference configuration items 
resource : configure the resource loading address to use resource . MBG from classpath Start looking, for example com/myproject/generatorConfig.properties 
url : configure resources to load geology, use URL For example file:///C:/myfolder/generatorConfig.properties.
 Note that two properties can only be selected 1 a ;
 Also, if used mybatis-generator-maven-plugin And so on pom.xml Defined in the properties Both can be directly in generatorConfig.xml The use of 
<properties resource="" url="" />
-->
<!--  in MBG When working, you need to load additional dependency packages 
location Property to indicate loading jar/zip Full path of the package 
<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />
-->
<!-- 
context: generate 1 The environment of the group object  
id: Required, context id Is used to prompt when generating an error 
defaultModelType: Specifies the style of the generated object 
1 . conditional Similar to: hierarchical ; 
2 . flat : all content (primary key, blob ) and so on all generated in 1 In objects; 
3 . hierarchical : primary key generation 1 a XXKey object (key class) . Blob Isomeric generation 1 Object, other simple properties in 1 An object (record class)
targetRuntime:
1 . MyBatis3 : the default value based on MyBatis3.x The above version of the content includes XXXBySample ; 
2 . MyBatis3Simple Similar to: MyBatis3 , just not generated XXXBySample ; 
introspectedColumnImpl : the fully qualified name of the class for extension MBG
-->
<context id="mysql" defaultModelType="hierarchical" targetRuntime="MyBatis3Simple" >
<!--  Automatically identify database keywords by default false , if set to true , according to the SqlReservedWords A list of keywords defined in; 
1 Leave the default values as usual, and the database keyword ( Java Key words), use columnOverride cover 
-->
<property name="autoDelimitKeywords" value="false"/>
<!--  The generated Java File encoding  -->
<property name="javaFileEncoding" value="UTF-8"/>
<!--  formatting java code  -->
<property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
<!--  formatting XML code  -->
<property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
<!-- beginningDelimiter and endingDelimiter : indicates the symbol used to mark the database object name of the database, e.g ORACLE Double quotation marks, MYSQL The default is ` The quotation marks;  -->
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!--  Must have, use this configuration to link to the database 
@TODO: Whether it can be extended 
-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql:///pss" userId="root" password="admin">
<!--  You can set it up here property Each attribute, 1 a property Properties are set to the configured Driver on  -->
</jdbcConnection>
<!-- java Type processor  
 To deal with DB to Java Is used by default JavaTypeResolverDefaultImpl ; 
 Pay attention to 1 Click, and by default it will be tried first Integer . Long . Short Such as to correspond to DECIMAL and  NUMERIC Data type;  
-->
<javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl">
<!-- 
true Use: BigDecimal The corresponding DECIMAL and  NUMERIC The data type 
false : the default ,
scale>0;length>18 Use: 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 The model creator is a must-have element 
 Responsible for: 1 . key Class (see context the defaultModelType ); 2 . java Class; 3 , the query classes 
targetPackage : the package to be put by the generated class, the real package to be accepted enableSubPackages Attribute control; 
targetProject : target item, specify 1 The generated content will be placed in the specified directory. If the directory does not exist, MBG Does not automatically create directories 
-->
<javaModelGenerator targetPackage="com._520it.mybatis.domain" targetProject="src/main/java">
<!-- for MyBatis3/MyBatis3Simple
 Automatically for each 1 Create a generated class 1 The constructor contains all of them field ; Instead of using setter ; 
-->
<property name="constructorBased" value="false"/>
<!--  in targetPackage Based on the database schema generating 1 layer package The resulting generated class is placed here package The default is false -->
<property name="enableSubPackages" value="true"/>
<!-- for MyBatis3 / MyBatis3Simple
 Whether to create 1 If is true . 
 then MBG Will create 1 A no setter Method class, replaced by similar constructorBased The class of 
-->
<property name="immutable" value="false"/>
<!--  Set up the 1 Root objects, 
 If the root object is set, it is generated keyClass or recordClass It will inherit this class; in Table the rootClass This option can be overridden in the property 
 Note: if in key class or record class There are root class Same property, MBG These properties will not be regenerated, including: 
1 , the property name is the same, the type is the same, there is the same getter/setter Methods; 
-->
<property name="rootClass" value="com._520it.mybatis.domain.BaseDomain"/>
<!--  Set whether getter In the method, right String Type field call trim() methods  -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--  generate SQL map the XML File generator, 
 Note that in Mybatis3 After that, we can use it mapper.xml file +Mapper Interface (or not mapper Interface), 
 Or just use Mapper interface +Annotation So, if  javaClientGenerator The configuration is configured to be generated XML This element must be configured 
targetPackage/targetProject: with javaModelGenerator
-->
<sqlMapGenerator targetPackage="com._520it.mybatis.mapper" targetProject="src/main/resources">
<!--  in targetPackage Based on the database schema generating 1 layer package The resulting generated class is placed here package The default is false -->
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--  for mybatis In other words, generate Mapper Interface, note that if this element is not configured, it will not be generated by default Mapper interface  
targetPackage/targetProject: with javaModelGenerator
type : select how to generate mapper Interfaces (in the MyBatis3/MyBatis3Simple ) : 
1 . ANNOTATEDMAPPER : will be generated for use Mapper interface +Annotation To create ( SQL Generated in the annotation , no corresponding one will be generated XML ; 
2 . MIXEDMAPPER : with mixed configuration, it will be generated Mapper Interface and add the appropriate ones as appropriate Annotation , but XML Will be generated in the XML ; 
3 . XMLMAPPER : will be generated Mapper Interfaces, interfaces are completely dependent XML ; 
 Notice that if context is MyBatis3Simple : only support ANNOTATEDMAPPER and XMLMAPPER
-->
<javaClientGenerator targetPackage="com._520it.mybatis.mapper" type="ANNOTATEDMAPPER" targetProject="src/main/java">
<!--  in targetPackage Based on the database schema generating 1 layer package The resulting generated class is placed here package The default is false -->
<property name="enableSubPackages" value="true"/>
<!--  You can add for all the generated interfaces 1 A parent interface, however MBG Generate only, not check 
<property name="rootInterface" value=""/>
-->
</javaClientGenerator>
<!--  choose 1 a table To generate the relevant files, you can have 1 One or more table There has to be table The element 
 Select the table Will be generated 1 The file: 
1 . SQL map file 
2 To generate the 1 Primary key class; 
3 , in addition to BLOB Class and other fields of the primary key; 
4 , including BLOB In the class; 
5 . 1 Four user generated dynamic query condition classes ( selectByExample, deleteByExample ), optional; 
6 . Mapper Interface (optional) 
tableName (necessary) : the table name of the object to be generated; 
 Note: case sensitive issues. Normally, MBG Will automatically identify the case sensitivity of the database identifier 1 Normally, MBG will 
 According to Settings schema . catalog or tablename To query the data table, follow the following procedure: 
1 If the schema . catalog or tablename In the space, then what format is set, the exact use of the specified case format to query; 
2 , otherwise, if the identifier of the database is capitalized, then MBG Automatically change the table name to uppercase and look it up; 
3 Otherwise, if the identifier of the database is lowercase, then MBG Automatically change the table name to lowercase and look it up; 
4 , otherwise, use the specified case format; 
 Also, if you are creating a table, use "" Specify the case of the database object, even if the database identifier is capitalized, in which case the table name will be created using the given case. 
 At this time, please set delimitIdentifiers="true" Can preserve the case format; 
 Optional: 
1 . schema : database schema ; 
2 . catalog : database catalog ; 
3 . alias : the alias set for the data table, if set alias So all of the generated SELECT SQL In the statement, the column name becomes: alias_actualColumnName
4 . domainObjectName Generated: domain Class name, if not set, directly use the table name as domain The name of the class; You can set it to somepck.domainName , then it will take care of itself domainName Class put somepck Inside the package; 
5 . enableInsert (the default true ) : specifies whether to generate insert Statements; 
6 . enableSelectByPrimaryKey (the default true ) : specifies whether to generate statements that query the object by primary key (i.e getById or get ); 
7 . enableSelectByExample (the default true ) : MyBatis3Simple for false , specifying whether to generate dynamic query statements; 
8 . enableUpdateByPrimaryKey (the default true ) : specifies whether to generate statements that modify the object by primary key (i.e update) ; 
9 . enableDeleteByPrimaryKey (the default true ) : specifies whether to generate statements to delete objects by primary key (i.e delete ); 
10 . enableDeleteByExample (the default true ) : MyBatis3Simple for false , specifying whether to generate dynamic delete statements; 
11 . enableCountByExample (the default true ) : MyBatis3Simple for false , specifying whether to generate the total number of dynamic query statements (the total number of queries for paging); 
12 . enableUpdateByExample (the default true ) : MyBatis3Simple for false , specifying whether to generate dynamic modification statements (only properties that are not empty in the object are modified); 
13 . modelType Reference: context Elements of the defaultModelType , equivalent to coverage; 
14 . delimitIdentifiers Reference: tableName The explanation of, note, the default delimitIdentifiers It's double quotes, if it's similar MYSQL Such a database, use is ` (backquotes, then you need to set it context the beginningDelimiter and endingDelimiter Attributes) 
15 . delimitAllColumns : sets whether all are generated SQL The column names in the. The default is false . delimitIdentifiers reference context The properties of the 
 Pay attention to, table Many of the parameters are correct javaModelGenerator . context Is the default attribute of the element 1 A copy; 
-->
<table tableName="userinfo" >
<!--  reference  javaModelGenerator  the  constructorBased attribute -->
<property name="constructorBased" value="false"/>
<!--  The default is false , if set to true Is generated SQL , table You don't add the name catalog or schema ;  -->
<property name="ignoreQualifiersAtRuntime" value="false"/>
<!--  reference  javaModelGenerator  the  immutable  attribute  -->
<property name="immutable" value="false"/>
<!--  Specifies whether to generate only domain Class, if set to true And only generate domain Class, if configured sqlMapGenerator And so on mapper XML In the file, only generated resultMap The element  -->
<property name="modelOnly" value="false"/>
<!--  reference  javaModelGenerator  the  rootClass  attribute  
<property name="rootClass" value=""/>
-->
<!--  reference javaClientGenerator  the  rootInterface  attribute 
<property name="rootInterface" value=""/>
-->
<!--  If you set it runtimeCatalog , so it's generating SQL , using the specified catalog Rather than table On the element catalog 
<property name="runtimeCatalog" value=""/>
-->
<!--  If you set it runtimeSchema , so it's generating SQL , using the specified schema Rather than table On the element schema 
<property name="runtimeSchema" value=""/>
-->
<!--  If you set it runtimeTableName , so it's generating SQL , using the specified tablename Rather than table On the element tablename 
<property name="runtimeTableName" value=""/>
-->
<!--  Note that this property is only for MyBatis3Simple Useful; 
 If selected runtime is MyBatis3Simple , then it will be generated 1 a SelectAll Method, if specified selectAllOrderByClause So it's going to be in this SQL Adds the specified one in order Conditions; 
-->
<property name="selectAllOrderByClause" value="age desc,username asc"/>
<!--  If I set it to true The resulting model Class will be used directly column Instead of using the hump naming method, for example BORN_DATE , the generated property name is BORN_DATE, Rather than bornDate -->
<property name="useActualColumnNames" value="false"/>
<!-- generatedKey The method used to generate the primary key, 
 If this element is set, MBG It's going to be generated <insert> Generated in the element 1 The right <selectKey> Element, which is optional 
column: The column name of the primary key; 
sqlStatement : to be generated selectKey Statement, with the following options: 
Cloudscape: The equivalent of selectKey the SQL To:  VALUES IDENTITY_VAL_LOCAL()
DB2 : The equivalent of selectKey the SQL To:  VALUES IDENTITY_VAL_LOCAL()
DB2_MF : The equivalent of selectKey the SQL To: SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1
Derby : The equivalent of selectKey the SQL To: VALUES IDENTITY_VAL_LOCAL()
HSQLDB : The equivalent of selectKey the SQL To: CALL IDENTITY()
Informix : The equivalent of selectKey the SQL To: select dbinfo('sqlca.sqlerrd1') from systables where tabid=1
MySql : The equivalent of selectKey the SQL To: SELECT LAST_INSERT_ID()
SqlServer : The equivalent of selectKey the SQL To: SELECT SCOPE_IDENTITY()
SYBASE : The equivalent of selectKey the SQL To: SELECT @@IDENTITY
JDBC : It's the same thing as being generated insert Add on the element useGeneratedKeys="true" and keyProperty attribute 
<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 ideal for when all the columns in the table have a common prefix string. 
 For example, the column name is: CUST_ID,CUST_NAME,CUST_EMAIL,CUST_ADDRESS And so on; 
 So you can set it searchString for "^CUST_" , and use a blank replacement, then generate Customer Property names in an object are not 
custId,custName And so on, instead of being replaced by ID,NAME,EMAIL, Then it becomes a property: id . name . email ; 
 Pay attention to, MBG Is the use of java.util.regex.Matcher.replaceAll To replace searchString and replaceString The, 
 If you use columnOverride Element, the attribute is invalid; 
<columnRenamingRule searchString="" replaceString=""/>
-->
<!--  To modify the properties of a column in a table, MBG The modified column is used to generate domain The properties of the; 
column: The column name to reset; 
 Pay attention to, 1 a table You can have more than one element columnOverride Elements, ~
-->
<columnOverride column="username">
<!--  use property Property to specify the name of the property to be generated by the column  -->
<property name="property" value="userName"/>
<!-- javaType Used to specify the generated domain , using the fully qualified name of the type 
<property name="javaType" value=""/>
-->
<!-- jdbcType Used to specify the column JDBC type  
<property name="jdbcType" value=""/>
-->
<!-- typeHandler  Used to specify which is used by this column TypeHandler , if specified, configure the fully qualified name of the type handler 
 Pay attention to, mybatis , will not be generated to mybatis-config.xml In the typeHandler
 Will only generate something like: where id = #{id,jdbcType=BIGINT,typeHandler=com._520it.mybatis.MyTypeHandler} Parameter description of 
<property name="jdbcType" value=""/>
-->
<!--  reference table Elements of the delimitAllColumns Configuration, by default false
<property name="delimitedColumnName" value=""/>
-->
</columnOverride>
<!-- ignoreColumn Set up the 1 a MGB Ignore columns, if set to change columns, then in the generated domain , generated SQL None of the columns will appear  
column: Specifies the name of the column to be ignored; 
delimitedColumnName Reference: table Elements of the delimitAllColumns Configuration, by default false
 Pay attention to, 1 a table You can have more than one element ignoreColumn The element 
<ignoreColumn column="deptId" delimitedColumnName=""/>
-->
</table>
</context>
</generatorConfiguration>

Related articles: