MyBatis Generator XML 配置参考

最常见的用例中,代码生成器(MBG)是由一个XML配置文件驱动。配置文件告诉MBG:

  • 如何连接到数据库
  • 生成什么对象,以及如何生成它们
  • 那些表生成对象

下面是一个MBG配置文件的例子。查阅每个元素单独的页面查看更多有关元素的更多信息和属性值。

<?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>
  <classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />

  <context id="DB2Tables" targetRuntime="MyBatis3">
    <jdbcConnection driverClass="COM.ibm.db2.jdbc.app.DB2Driver"
        connectionURL="jdbc:db2:TEST"
        userId="db2admin"
        password="db2admin">
    </jdbcConnection>

    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>

    <javaModelGenerator targetPackage="test.model" targetProject="\MBGTestProject\src">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>

    <extjsGenerator appName="ESSM" targetPackage="test"
            targetProject="\MBGTestProject\src\main\webapp\static\app">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </extjsGenerator>

    <sqlMapGenerator targetPackage="test.xml"  targetProject="\MBGTestProject\src">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

    <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao"  targetProject="\MBGTestProject\src">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>

    <table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >
      <property name="useActualColumnNames" value="true"/>
      <generatedKey column="ID" sqlStatement="DB2" identity="true" />
      <columnOverride column="DATE_FIELD" property="startDate" />
      <ignoreColumn column="FRED" />
      <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
    </table>

  </context>
</generatorConfiguration>

有关此文件的重要说明:

  • 该文件指定旧式DB2 CLI驱动程序将用于连接到数据库,并指定在何处可以找到驱动程序。
  • Java类型解析器不应该强制型对象字段BigDecimal的使用,这意味着整数类型(短、整型、长等)如果可能的话将被取代。 此功能是为了使数据库DECIMAL和NUMERIC列容易处理。
  • Java模型生成器应该使用子包。 这意味着在这种情况下生成的模型对象将被放置在名为 test.model.db2admin 的包中(因为表在 DB2ADMIN schema中)。 如果 enableSubPackages 属性设置为 false, 那么包名将会是 test.model。 Java模型生成器也应该对字符串进行trim操作。 这意味着任何字符串属性的setter方法将调用trim方法 - 如果您的数据库可能会在字符末尾返回空白符,这是非常有用的。
  • SQL映射生成器将使用子包。这意味着这种情况下生成的XML文件将被放置在名为 test.xml.db2admin 的包中(因为表在 DB2ADMIN schema中)。 如果 enableSubPackages 属性设置为 false, 那么包名将会是 test.xml
  • DAO生成器将使用子包。这意味着这种情况下生成的DAO类都会被放置在名为 test.dao.db2admin 的包中(因为表在 DB2ADMIN schema中)。 如果 enableSubPackages 属性设置为 false, 那么包名将会是 test.dao。 DAO生成器会生成一些引用Mybatis XML配置的mapper接口。
  • The file specifies only one table will be introspected, but many more could be specified. Important notes about the specified table include:
    • The generated objects will be based on the name Customer (CustomerKey, Customer, CustomerMapper, etc.) - rather than on the table name.
    • Actual column names will be used as properties. If this property were set to false (or not specified), then MBG would attempt to camel case the column names. In either case, the name can be overridden by the <columnOverride> element
    • The column has a generated key, it is an identity column, and the database type is DB2. This will cause MBG to generate the proper <selectKey> element in the generated <insert> statement so that the newly generated key can be returned (using DB2 specific SQL).
    • The column DATE_FIELD will be mapped to a property called startDate. This will override the default property which would be DATE_FIELD in this case, or dateField if the useActualColumnNames property was set to false.
    • The column FRED will be ignored. No SQL will list the field, and no Java property will be generated.
    • The column LONG_VARCHAR_FIELD will be treated as a VARCHAR field, regardless of the actual data type.