In the most common use case, MyBatis Generator (MBG) is driven by an XML configuration file.(在最常见的情况下,MyBatis生成器(MBG)是由XML配置文件驱动的) The configuration file tells MBG:(这配置文件告诉MBG

·         How to connect to the database(怎么去连接数据库)

·         What objects to generate, and how to generate them(什么对象需要生成,怎么去生成它们)

·         What tables should be used for object generation(为了生成对象要用到什么表)

The following is an example MBG configuration file. See the individual pages for each element for more information about the elements and the values of the attributes.(下面是一个MBG配置文件的例子,看每个元素介绍的单独页面,页面提供了更多关于元素的信息和属性值)

 
  
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE generatorConfiguration 
  3.   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 
  4.   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 
  5.  
  6. <generatorConfiguration> 
  7.   <classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" /> 
  8.  
  9.   <context id="DB2Tables" targetRuntime="MyBatis3"> 
  10.     <jdbcConnection driverClass="COM.ibm.db2.jdbc.app.DB2Driver" 
  11.         connectionURL="jdbc:db2:TEST" 
  12.         userId="db2admin" 
  13.         password="db2admin"> 
  14.     </jdbcConnection> 
  15.  
  16.     <javaTypeResolver > 
  17.       <property name="forceBigDecimals" value="false" /> 
  18.     </javaTypeResolver> 
  19.  
  20.     <javaModelGenerator targetPackage="test.model" targetProject="\MBGTestProject\src"> 
  21.       <property name="enableSubPackages" value="true" /> 
  22.       <property name="trimStrings" value="true" /> 
  23.     </javaModelGenerator> 
  24.  
  25.     <sqlMapGenerator targetPackage="test.xml"  targetProject="\MBGTestProject\src"> 
  26.       <property name="enableSubPackages" value="true" /> 
  27.     </sqlMapGenerator> 
  28.  
  29.     <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao"  targetProject="\MBGTestProject\src"> 
  30.       <property name="enableSubPackages" value="true" /> 
  31.     </javaClientGenerator> 
  32.  
  33.     <table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" > 
  34.       <property name="useActualColumnNames" value="true"/> 
  35.       <generatedKey column="ID" sqlStatement="DB2" identity="true" /> 
  36.       <columnOverride column="DATE_FIELD" property="startDate" /> 
  37.       <ignoreColumn column="FRED" /> 
  38.       <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> 
  39.     </table> 
  40.  
  41.   </context> 
  42. </generatorConfiguration> 

 Important notes about this file follow:(下面是关于这个文件的注意事项)

  • The file specifies that the legacy DB2 CLI driver will be used to connect to the database, and also specifies where the driver can be found.(这个文件指明了DB2 CLI驱动将被用来连接数据库和去×××驱动)

  • The Java Type Resolver should not force the use of BigDecimal fields - this means that integral types (Short, Integer, Long, etc.) will be substituted if possible.Java Type Resolver不会强迫使用BigDecimal属性,这意味着整数类型比如Short,IntegerLong等如果可以将会作为大数据的替代类型) This feature is an attempt to make database DECIMAL and NUMERIC columns easier to deal with.(这个特点是尝试是数据库的DECIMALNUMERIC更容易处理)

  • The Java model generator should use sub-packages.java模型生成器必须使用子包) This means that the generated model objects will be placed in a package called test.model.db2admin in this case (because the table is in the DB2ADMIN schema). (这意味这这些生成的模型对象必须放在一个包中,在这个例子中这个包叫做test.model.db2admin,因为这个表是在DB2ADMIN数据库中)If the enableSubPackages attribute was set to false, then the package would be test.model. (如果enableSubPackages 被设置为false,那么这个包将会是test.modelThe Java model generator should also trim strings. (这个java模型生成器将会去除字符串空格)This means that the setters for any String properties will call the trim function - this is useful if your database might return blank characters at the end of character columns.(这意味着任何String属性的setter方法都会调用trim方法,如果你的数据库将会在最后的字符串的列返回空白的字符这是很有用的)

  • The SQL Map generator should use sub-packages. This means that the generated XML files will be placed in a package called test.xml.db2admin in this case (because the table is in the DB2ADMIN schema). If the enableSubPackages attribute was set to false, then the package would be test.xml.SQL Map生成器也需要子包,这意味着生成的xml文件会被放在一个包中,在这个例子中这个包叫做test.xml.db2admin,如果enableSubPackages 属性被设置成false,那么这些生成的文件放的包是test.xml

  • The DAO generator should use sub-packages. (DAO的生成器需要子包)This means that the generated DAO classes will be placed in a package called test.dao.db2admin in this case (because the table is in the DB2ADMIN schema). (这意味着生成的DAO类将会放在一个包中,在这个例子中这个包叫做test.dao.db2admin(因为这个表是在DB2ADMIN数据库中If the enableSubPackages attribute was set to false, then the package would be test.dao. The DAO generator should generate mapper interfaces that reference an XML configuration for MyBatis.(如果enableSubPackages 属性被设置成false,那么这个包名将会是test.dao,这DAO的生成器将会根据XML配置文件生成匹配的接口,)

  • 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.(这个生成的对象是基于名字Customer,比起基于表名更好)

  • 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.(如果属性被设置成false或者没有指定,这个MBG会尝试把这个列名驼峰书写并命名为类的属性) In either case, the name can be overridden by the <columnOverride> element(其他种情况下,这个名字可以被 <columnOverride>元素重写

  • The column has a generated key, it is an identity column, and the database type is DB2.(这个列有一个要生成的关键字,这是一个标识列,数据库的类型是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).(这会导致MBG<insert>语句中生成合适的<selectKey>,为了使新生成的关键字可以被返回,使用DB2的特定SQL语句

  • The column DATE_FIELD will be mapped to a property called startDate.(这列 DATE_FIEL会和叫做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.(在这个例子中这会覆盖默认的属性DATE_FIELD或者如果useActualColumnNames 被设置为false则是dateField 

  • The column FRED will be ignored. No SQL will list the field, and no Java property will be generated.(这列FRED 将会被忽视。没有sql语句会列出这个域,没有java属性将会生成

  • The column LONG_VARCHAR_FIELD will be treated as a VARCHAR field, regardless of the actual data type.(不管它真正的类型,LONG_VARCHAR_FIELD将会被当作VARCHAR 类型属性

Copyright © 2010-2012 MyBatis.org. All Rights Reserved.

 

11