自動生成代碼方式兩種:
1、命令形式生成代碼,詳細講解每一個配置參數。
2、Eclipse利用插件形式生成代碼。
安裝插件方式:
eclipse插件安裝地址:http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/
附件有link安裝包,link安裝方式參考http://maimode.iteye.com/admin/blogs/1164524
MyBatis Generator詳細介紹參見:http://code.google.com/p/mybatis/wiki/Generator
安裝插件的過程就不說了,安裝完后,eclipse中File-》new-》other中會發現多了mybatis選項說明插件安裝成功
如何使用插件
在任意項目中利用上圖中的向導創建generatorConfig.xml文件(名稱可修改)然后修改文件內容,主要是設置連接數據的相關參數:
generator自動生成mybatis的xml配置、model、map等信息:
1、下載mybatis-generator-core-1.3.2.jar包。
網址:http://code.google.com/p/mybatis/downloads/list?can=3&q=Product%3DGenerator,下載mybatis-generator-core-1.3.2-bundle.zip,將mybatis-generator-core-1.3.2.jar放到項目中
2、編寫generatorConfig的xml文件,名下:generatorConfig.xml
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="init.properties"/> <!-- 指定數據連接驅動jar地址 --> <classPathEntry location="E:\workspace\mysql-connector-java-5.1.24-bin.jar"/> <!-- 一個數據庫一個context --> <context id="infoGuardian" targetRuntime="MyBatis3"> <!-- 注釋 --> <commentGenerator > <property name="suppressAllComments" value="false"/><!-- 是否取消注釋 --> <property name="suppressDate" value="true" /> <!-- 是否生成注釋代時間戳--> </commentGenerator> <!-- jdbc連接 --> <jdbcConnection driverClass="${jdbc_driver}" connectionURL="${jdbc_url}" userId="${jdbc_user}" password="${jdbc_password}" /> <!-- 類型轉換 --> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自動轉化以下類型(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成實體類地址 --> <javaModelGenerator targetPackage="com.common.user.model" targetProject="basic-user-web\src\main\java" > <!-- 是否在當前路徑下新加一層schema,eg:fase路徑com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="true"/> <!-- 是否針對string類型的字段在set的時候進行trim調用 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成mapxml文件 --> <sqlMapGenerator targetPackage="com.common.user.Mapper" targetProject="basic-user-web\src\main\java" > <!-- 是否在當前路徑下新加一層schema,eg:fase路徑com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成mapxml對應client,也就是接口dao --> <javaClientGenerator targetPackage="com.user.model" targetProject="basic-user-web\src\main\resources" type="XMLMAPPER" > <!-- 是否在當前路徑下新加一層schema,eg:fase路徑com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 配置表信息 --> <!--<table schema="cap_common" tableName="mosf_common_user" domainObjectName="User"> enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> --> <!-- schema即為數據庫名 tableName為對應的數據庫表 domainObjectName是要生成的實體類 enable*ByExample是否生成 example類 --> <!-- 忽略列,不生成bean 字段 --> <!-- <ignoreColumn column="FRED" /> --> <!-- 指定列的java數據類型 --> <!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> --> <!-- </table> --> <table schema="" tableName="mosf_common_user" domainObjectName="UserModel" delimitIdentifiers="true" delimitAllColumns="true" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> </context> </generatorConfiguration>
table其他屬性:
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"
schema即為數據庫名, tableName為對應的數據庫表, domainObjectName是要生成的實體類,
如果想要mapper配置文件加入sql的where條件查詢, 可以將enableCountByExample等設為true,
這樣就會生成一個對應domainObjectName的Example類, enableCountByExample等設為false時,
就不會生成對應的Example類了.
如果table里邊不配置property,默認字段都生成為類屬性。
<ignoreColumn column="FRED" />//忽略字段
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />//無論字段是什么類型,生成的類屬性都是varchar。
點擊generatorConfig.xml右鍵上的選項,如果配置正確,便自動創建相關文件了。
文件主要有三類:
client包,mapper 接口文件
model包,實體bean文件
mapper包,mapper xml文件