mybatis之mybatis-generator
mybatis-generator的基本配置文件
<?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 > <propertyname="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配置文件的的根元素;本身不具備任何屬性;有三個子元素:
- <properties>(0 or 1)
- <classPathEntry>(0~N)
- <context>(0~N)
<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包的位置;無子元素;例如:
<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />
<context>元素用於生成一組對象指定的環境。子元素用於指定要連接的數據庫、要生成的對象的類型以及要內省的表
- 必須的屬性:id
- 可選的屬性:
- defaultModelType(設置默認生成的模型類型)
- conditional(默認值)
- flat
- hierarchical
- targetRuntime(此屬性用於指定生成的代碼運行的目標)
- MyBatis3(默認值)
- MyBatis3Simple
- Ibatis2Java2
- Ibatis2Java5
- introspectedColumnlmpl
- 子元素
- <property>(0~N)
- <plugin>(0~N)
- <commentGenerator>(0 or 1)
- <connection>
- <jdbcConnection>
- <javaTypeResolver>(0 or 1)
- <javaModelGenerator>(1 Required)
- <sqlMapGenerator>(0 or 1)
- <javaClienGenerator>(0 or 1)
- <table>(1~N)
<property>元素用於指定許多其他元素的屬性;無屬性;無子元素;例如:
<property name="forceBigDecimals" value="false" />
<plugin>元素用於定義(引用)一個插件
<commentGenerator>元素用於在在創建class時,對註釋進行控制;
支持的屬性:
- suppressAllComments:生成註釋
- false(默認)
- true
- suppressDate:生成的註釋包含時間戳
- false(默認)
- true
- addRemarkComments:生成的註釋包含數據庫表
- false(默認)
- true
- dateFormat:將日期寫入生成的註釋時使用的日期的格式,默認情況下,日期字符串將從tostring()轉化為指定格式的方法
例如:
<commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator>
<jdbcConnection>元素用於jdbc的數據庫連接;
- 必需的屬性
- driverClass:JDBC驅動的類名
- connectionURL:JDBC連接的URL來訪問數據庫
- 可選的屬性
- userId:連接數據庫用的用戶名
- password:連接數據庫用的密碼
例如:
<jdbcConnection driverClass="COM.ibm.db2.jdbc.app.DB2Driver" connectionURL="jdbc:db2:MBGTEST" userId="db2admin" password="db2admin"> </jdbcConnection>
<javaTypeResolver>元素用於在數據庫類型和java類型之間的轉換控制;(可選)
支持的屬性:forceBigDecimals
- false
- true
例如:
<javaTypeResolver> <property name="forceBigDecimals" value="true" /> </javaTypeResolver>
<javaModelGenerator>元素用於定義的java模式發生器性能。java模型生成器生成主鍵類,記錄類,並通過實例類的反思表匹配查詢。此元素是一個必需的<context>元素的子元素;
- 必須的屬性
- targetPackage:生成的類所放置的包
- targetProject:生成對象的目標項目和源文件夾
- 支持的屬性
- constructorBased:為每一個生成的類創建一個構造方法
- enableSubPackages:是否準許子包
- immutable:建立的Model對象是否不可改變(是否生成setter方法),默認為false
- rootClass:指定所有已生成的java模型對象的根類
- trimStrings:是否對類CHAR類型的列的數據進行trim操作
例如:
<javaModelGenerator targetPackage="test.model" targetProject="\MyProject\src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator>
<sqlMapGenerator>元素用於定義SQLMap發生器性能;
- 必需的屬性
- targetPackage:生成的類所放置的包
- targetProject:生成對象的目標項目和源文件夾
- 支持的屬性
- enableSuPackages:是否再生成一層package;默認為false(不生成);
例如:
<sqlMapGenerator targetPackage="test.model" targetProject="\MyProject\src"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator>
<javaClientGenerator>元素用於定義java客戶端的發生器性能;
- 必需的屬性
- type:選擇預定義得客戶端代碼生成器
- MyBatis3
- MyBatis3Simple
- Ibatis2Java or Ibatis2Java5
- targetPackage:生成實體類存放的包名,一般就是放在該包下
- targetProject:指定目標項目路徑,實現類就會生成在這個包中
- 可選的屬性
- implementationPackage:如果指定的話,實現類將放在這個包中
- 支持的屬性(不常用,自己查看文檔)
- enableSubPackages
- exampleMethodVisbility
- methodNameCalculator
- rootInterface:可以為所有生成的接口添加一個父接口,MBG只負責生成,不負責檢查
- userLeacyBuilder
例如:
<javaClientGenerator targetPackage="test.model" targetProject="\MyProject\src" type="XMLMAPPER"> <property name="enableSubPackages" value="true" /> </javaClientGenerator>
<table>元素用於選擇數據庫中的數據表進行處理
- 必需的屬性
- tableName:數據庫表的名稱
- 可選的屬性
- schema:數據庫的schema,可以使用SQL通配符匹配。如果設置了該值,生成的SQL的表名會變成如:schema.tableName的形式
- catalog:數據庫的catalog,如果設置了該值,生成的SQL的表名會變成如:catalog.tableName的形式
- alias:添加數據庫表的別名
- domainObjectName:生成對象的基本名稱,如果沒有指定,MGB會自動根據表名來生成名稱
- enableInsert:是否生成insert語句,默認為true
- enableSelectByPriMaryKey:是否生成按照主鍵查詢對象的語句(getBy or get),默認為true
- enableSelectByExample:是否生成動態查詢語句,默認為true(MyBatis3Simple為false)
- enableUpdataByPrimaryKey:是否生成按照主鍵修改對象的語句,默認為true
- enableDeleteByPrimaryKey:是否生成按照主鍵刪除對象的語句,默認為true
- enableDeleteByExample:是否生成動態刪除語句,默認為true(MyBatis3Simple為false)
- enableCountByExample:是否生成生成動態查詢總條數語句(用於分頁的總條數查詢),默認為true(MyBatis3Simple為false)
- enableUpdataByExample:是否生成動態修改語句(只修改對象中不為空的屬性),默認為true
- seletByPrimaryKeyQueryId:DAO跟蹤工具會用到
- selectByExampleQueryId:DAO跟蹤工具會用到
- modelType:與<context>中的<defaultModelType>元素一樣,可以覆蓋掉<defaultModelType>元素
- escapeWildcards:查詢時是否對schema和表名中的(‘_’ and ‘%’)進行轉義,默認為false
- delimitIdentifiers:是否給標識符增加分隔符,默認為false(如果schema和catlog或tableName為空,默認為true)
- delimitAllColumns:是否所有生成的SQL中的列名都使用標識符引起來,默認為false
- 支持的屬性
- constructorBased:為每一個生成的類創建一個構造方法
- ignoreQualifiersAtRuntime:默認為false,如果為true,在生成的SQL中,table名字不會加上catlog或schema
- immutable:建立的Model對象是否不可改變(是否生成setter方法)
- modelOnly:此屬性用於配置是否為表只生成實體類,默認為false
- rootClass:指定所有已生成的java模型對象的根類
- rootInterface:可以為所有生成的接口添加一個父接口,MBG只負責生成,不負責檢查
- runtimeCatlog:運行時的catlog,當生成表和運行環境的表的tableName不一樣的時候可以使用該屬性進行配置
- runtimeSchema:運行時的schema,當生成表和運行環境的表的schema不一樣的時候可以使用改屬性進行配置
- runtimeTableName:運行時的tableName,當生成表和運行環境的表的tableName不一樣的時候可以使用該屬性進行配置
- selectAllOrderByClause:該屬性值會追加到selectAll方法後的SQL中,會直接跟order by拼接後添加到SQL末尾(MyBatis3Simple時可用)
- useActualColumnNames:
如果設置為true,生成的model類會直接使用column本身的名字,而不會再使用駝峰命名方法;
- useColumnIndexes:如果是true,MBG生成resultMaps的時候會使用列的索引,而不是結果中列名的順序
- useCompoundPropertyNames:如果true,那麽MBG將使用由contatenating列名和列reparks生成屬性名稱
- 子元素
- <generatedKey>:自動生成主鍵的屬性
- column:主鍵的列名
- jdbcType:jdbc類型
例如:
<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>
mybatis之mybatis-generator