mybatis之mybatis-generator

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配置文件的的根元素;自己不具有任何屬性;有三個子元素:
  • <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包的位置;無子元素;例如:mysql

<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />

 


 

<context>元素用於生成一組對象指定的環境。子元素用於指定要鏈接的數據庫、要生成的對象的類型以及要內省的表web

  • 必須的屬性: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>元素用於指定許多其餘元素的屬性;無屬性;無子元素;例如:sql

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

 


 

<plugin>元素用於定義(引用)一個插件數據庫

 


 

<commentGenerator>元素用於在在建立class時,對註釋進行控制;mybatis

支持的屬性:app

  • suppressAllComments:生成註釋
    • false(默認)
    • true
  • suppressDate:生成的註釋包含時間戳
    • false(默認)
    • true
  • addRemarkComments:生成的註釋包含數據庫表
    • false(默認)
    • true
  • dateFormat:將日期寫入生成的註釋時使用的日期的格式,默認狀況下,日期字符串將從tostring()轉化爲指定格式的方法

例如:dom

<commentGenerator>
  <property name="suppressDate" value="true"/>
  <property name="suppressAllComments" value="true"/>
</commentGenerator>

 


 

<jdbcConnection>元素用於jdbc的數據庫鏈接;ide

  • 必需的屬性
    • 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>
相關文章
相關標籤/搜索