1. 程式人生 > >MyBatis Generator 使用 詳解

MyBatis Generator 使用 詳解

MyBatis Generator工具用於將資料庫中的表反轉成對應的 Domain、XML檔案 和 對應的 DAO  Java檔案 注 : 訪問路徑不能有中文字元 1> generator.xml 配置檔案 <?xml version="1.0"encoding="UTF-8"?> <!DOCTYPEgeneratorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" <generatorConfiguration> <!-- 資料庫驅動包位置 -->     <!-- <classPathEntry location="D:\software\lib\mysql-connector-java-5.1.21.jar" /> -->
<classPathEntrylocation="/Users/mew/.m2/repository/mysql/mysql-connector-java/5.1.9/mysql-connector-java-5.1.9.jar"/> <!-- id:資料庫方言 --> <contextid="mysqlGen"targetRuntime="MyBatis3"> <propertyname="javaFileEncoding"value="UTF-8"/> <commentGenerator> <propertyname="suppressDate"
value="true"/> <!-- 是否去除自動生成的註釋.true:是;false:否 --> <propertyname="suppressAllComments"value="true"/> </commentGenerator> <!-- 資料庫連結URL、使用者名稱、密碼 --> <jdbcConnectiondriverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://localhost:3306/sy"userId="sypro"password="sypro"
> <!-- <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa"> --> </jdbcConnection> <!-- default false              Java type resolver will always use java.math.BigDecimal if the database column is of type DECIMAL or NUMERIC. --> <javaTypeResolver> <propertyname="forceBigDecimals"value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置              指定包名以及生成的目標地址(可以自定義地址,但是路徑不存在不會自動建立 .使用MAVEN將預設生成在target目錄下,會自動建立路徑)--> <javaModelGeneratortargetPackage="sy.model"targetProject="D:\study\mybatis\src"> <propertyname="enableSubPackages"value="true"/> <propertyname="trimStrings"value="true"/> </javaModelGenerator> <!-- 生成的對映檔案包名和位置              指定包名以及生成的目標地址(可以自定義地址,但是路徑不存在不會自動建立 .使用MAVEN將預設生成在target目錄下,會自動建立路徑) --> <sqlMapGeneratortargetPackage="sy.mapping"targetProject="D:\study\mybatis\src"> <propertyname="enableSubPackages"value="true"/> </sqlMapGenerator> <!--             生成DAO的包名和位置             指定包名以及生成的目標地址(可以自定義地址,但是路徑不存在不會自動建立 .使用MAVEN將預設生成在target目錄下,會自動建立路徑)           --> <javaClientGeneratortype="XMLMAPPER"targetPackage="sy.dao"targetProject="D:\study\mybatis\src"> <propertyname="enableSubPackages"value="true"/> </javaClientGenerator> <!-- 要生成那些表(更改tableName和domainObjectName就可以,對於後面幾個屬性可以防止產生其他不需要的配置) --> <tabletableName="tbug"domainObjectName="Bug"enableCountByExample="false"enableUpdateByExample="false" enableDeleteByExample="false"enableSelectByExample="false"selectByExampleQueryId="false"/> <tabletableName="tmenu"domainObjectName="Menu"enableCountByExample="false"enableUpdateByExample="false" enableDeleteByExample="false"enableSelectByExample="false"selectByExampleQueryId="false"/> <tabletableName="tonline"domainObjectName="Online"enableCountByExample="false"enableUpdateByExample="false" enableDeleteByExample="false"enableSelectByExample="false"selectByExampleQueryId="false"/> </context> </generatorConfiguration> 2> 生成對應的 xml 和 Java檔案 方法一 : 使用 java指令 進入到含有 mybatis-generator-core-1.3.2.jar 和 generator.xml 的資料夾,使用指令 $> java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite 方法二 : 使用 mybatis-generator-maven-plugin 外掛生成 在 maven 工程的 pom.xml 中新增  <dependency>     <groupId>org.mybatis.generator</groupId>     <artifactId>mybatis-generator-core</artifactId>     <version>1.3.2</version> </dependency> <build>     <finalName>mybatis_generator</finalName>     <plugins>         <plugin>             <groupId>org.mybatis.generator</groupId>             <artifactId>mybatis-generator-maven-plugin</artifactId>             <version>1.3.2</version>         </plugin>     </plugins> </build> 安裝成功後,將 generator.xml 放入 resources目錄中,此時在 maven 的外掛中找到 mybatis-generator外掛,雙擊就會在 target 資料夾下產生需要的檔案