mybatis-generator的基本配置文件java
<?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>
<generatorConfiguration>是MyBatis Generator配置文件的的根元素;自己不具有任何屬性;有三個子元素:
<generatorConfiguration>包含如下內容:
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<properties>元素只能存在一個或者沒有,它是導入數據庫屬性配置的標籤;例如:
<properties resource="datasource.properties"></properties>
<classPathEntry>元素用於指定特定數據庫的jdbc驅動jar包的位置;無子元素;例如:mysql
<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />
<context>元素用於生成一組對象指定的環境。子元素用於指定要鏈接的數據庫、要生成的對象的類型以及要內省的表web
<property>元素用於指定許多其餘元素的屬性;無屬性;無子元素;例如:sql
<property name="forceBigDecimals" value="false" />
<plugin>元素用於定義(引用)一個插件數據庫
<commentGenerator>元素用於在在建立class時,對註釋進行控制;mybatis
支持的屬性:app
例如:dom
<commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator>
<jdbcConnection>元素用於jdbc的數據庫鏈接;ide
例如:函數
<jdbcConnection driverClass="COM.ibm.db2.jdbc.app.DB2Driver" connectionURL="jdbc:db2:MBGTEST" userId="db2admin" password="db2admin"> </jdbcConnection>
<javaTypeResolver>元素用於在數據庫類型和java類型之間的轉換控制;(可選)
支持的屬性:forceBigDecimals
例如:
<javaTypeResolver> <property name="forceBigDecimals" value="true" /> </javaTypeResolver>
<javaModelGenerator>元素用於定義的java模式發生器性能。java模型生成器生成主鍵類,記錄類,並經過實例類的反思表匹配查詢。此元素是一個必需的<context>元素的子元素;
例如:
<javaModelGenerator targetPackage="test.model" targetProject="\MyProject\src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator>
<sqlMapGenerator>元素用於定義SQLMap發生器性能;
例如:
<sqlMapGenerator targetPackage="test.model" targetProject="\MyProject\src"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator>
<javaClientGenerator>元素用於定義java客戶端的發生器性能;
例如:
<javaClientGenerator targetPackage="test.model" targetProject="\MyProject\src" type="XMLMAPPER"> <property name="enableSubPackages" value="true" /> </javaClientGenerator>
<table>元素用於選擇數據庫中的數據表進行處理
若是設置爲true,生成的model類會直接使用column自己的名字,而不會再使用駝峯命名方法;
例如:
<table tableName="MYTABLE" schema="MYSCHEMA"> <ignoreColumn column="fred"/> <columnOverride column="BEG_DATE" property="startDate"/> </table>
總結:
上面的內容就是我根據MBG官方文檔和一些查閱到的資料聚集到一塊兒的,由於我也是正在學習這個,可能有些地方理解的不那麼深,如看到錯誤請指出來,我會繼續修改,下面是我最近寫的項目用到的MBG配置,但願對須要的人有幫助:
datasource.properties
db.driverLocation = D:\\Project\\Javaweb\\mysql-connector-java-5.1.6-bin.jar
db.driverClassName = com.mysql.jdbc.Driver
db.url = jdbc:mysql://127.0.0.1:3306/mmall?characterEncoding=utf-8
db.username = root
db.password =
generatorConfig.xml
<?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> <!--導入屬性配置--> <properties resource="datasource.properties"></properties> <!--指定特定數據庫的jdbc驅動jar包的位置--> <classPathEntry location="${db.driverLocation}"/> <context id="default" targetRuntime="MyBatis3"> <!-- optional,旨在建立class時,對註釋進行控制 --> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--jdbc的數據庫鏈接 --> <jdbcConnection driverClass="${db.driverClassName}" connectionURL="${db.url}" userId="${db.username}" password="${db.password}"> </jdbcConnection> <!-- 非必需,類型處理器,在數據庫類型和java類型之間的轉換控制--> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- Model模型生成器,用來生成含有主鍵key的類,記錄類 以及查詢Example類 targetPackage 指定生成的model生成所在的包名 targetProject 指定在該項目下所在的路徑 --> <!--<javaModelGenerator targetPackage="com.mmall.pojo" targetProject=".\src\main\java">--> <javaModelGenerator targetPackage="com.mmall.pojo" targetProject="./src/main/java"> <!-- 是否容許子包,即targetPackage.schemaName.tableName --> <property name="enableSubPackages" value="false"/> <!-- 是否對model添加 構造函數 --> <property name="constructorBased" value="true"/> <!-- 是否對類CHAR類型的列的數據進行trim操做 --> <property name="trimStrings" value="true"/> <!-- 創建的Model對象是否 不可改變 即生成的Model對象不會有 setter方法,只有構造方法 --> <property name="immutable" value="false"/> </javaModelGenerator> <!--mapper映射文件生成所在的目錄 爲每個數據庫的表生成對應的SqlMap文件 --> <!--<sqlMapGenerator targetPackage="mappers" targetProject=".\src\main\resources">--> <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 客戶端代碼,生成易於使用的針對Model對象和XML配置文件 的代碼 type="ANNOTATEDMAPPER",生成Java Model 和基於註解的Mapper對象 type="MIXEDMAPPER",生成基於註解的Java Model 和相應的Mapper對象 type="XMLMAPPER",生成SQLMap XML文件和獨立的Mapper接口 --> <!-- targetPackage:mapper接口dao生成的位置 --> <!--<javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject=".\src\main\java">--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject="./src/main/java"> <!-- enableSubPackages:是否讓schema做爲包的後綴 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <table tableName="mmall_shipping" domainObjectName="Shipping" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="mmall_cart" domainObjectName="Cart" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="mmall_cart_item" domainObjectName="CartItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="mmall_category" domainObjectName="Category" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="mmall_order" domainObjectName="Order" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="mmall_order_item" domainObjectName="OrderItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="mmall_pay_info" domainObjectName="PayInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="mmall_product" domainObjectName="Product" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <columnOverride column="detail" jdbcType="VARCHAR" /> <columnOverride column="sub_images" jdbcType="VARCHAR" /> </table> <table tableName="mmall_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <!-- geelynote mybatis插件的搭建 --> </context> </generatorConfiguration>