http://www.mybatis.org/generator/configreference/xmlconfig.html <?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> <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字段 - 这意味着如果可能,将替换整数类型(Short,Integer,Long等)。此功能旨在使数据库DECIMAL和NUMERIC列更易于处理。
- Java模型生成器应该使用子包。这意味着在这种情况下,生成的模型对象将放在名为test.model.db2admin的包中(因为该表位于DB2ADMIN模式中)。如果enableSubPackages属性设置为 false,则包将为test.model。Java模型生成器也应修剪字符串。这意味着任何String属性的setter都将调用trim函数 - 如果数据库可能在字符列的末尾返回空白字符,这将非常有用。
- SQL Map生成器应该使用子包。这意味着在这种情况下,生成的XML文件将放在名为test.xml.db2admin的包中(因为该表位于DB2ADMIN模式中)。如果enableSubPackages属性设置为false,那么包将是test.xml。
- DAO生成器应使用子包。这意味着在这种情况下,生成的DAO类将放在名为test.dao.db2admin的包中(因为该表位于DB2ADMIN模式中)。如果enableSubPackages属性设置为false,那么包将是test.dao。DAO生成器应生成映射器接口,该接口引用MyBatis的XML配置。
- 该文件仅指定一个表将被内省,但可以指定更多表。有关指定表的重要说明包括:
- 生成的对象将基于名称Customer(CustomerKey, Customer, CustomerMapper等) - 而不是基于表名。
- 实际列名称将用作属性。如果此属性设置为 false(或未指定),则MBG将尝试使用列名称。在任何一种情况下,名称都可以被<columnOverride>元素覆盖
- 该列具有生成的密钥,它是标识列,数据库类型是DB2。这将导致MBG 在生成的<insert> 语句中生成正确的 <selectKey>元素, 以便可以返回新生成的密钥(使用DB2特定的SQL)。
- DATE_FIELD列将映射到名为startDate的属性 。这将覆盖默认属性,在本例中为DATE_FIELD, 如果useActualColumnNames属性设置为 false,则覆盖 dateField。
- FRED列将被忽略。没有SQL会列出该字段,也不会生成任何Java属性。
- 无论实际数据类型如何,LONG_VARCHAR_FIELD列都将被视为 VARCHAR字段。