步驟1:
下載下傳http://mybatis.googlecode.com/files/mybatis-generator-core-1.3.1-bundle.zip 并解壓
步驟2:
建立一個generator.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>
<!-- classPathEntry: 設定mysql驅動路徑 -->
<classPathEntry location="D:\mybatistool\mybatis-generator-core-1.3.1\lib\mysql-connector-java-5.1.16-bin.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- jdbc連接配接資訊 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.0.250:3306/erp" userId="root" password="root">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成vo對象 -->
<javaModelGenerator targetPackage="com.wxws.crmanager.pojos" targetProject="E:\workspaces\test">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成用于查詢的Example對象 -->
<sqlMapGenerator targetPackage="mybatis.crmanager" targetProject="E:\workspaces\test\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的類檔案以及配置檔案 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.wxws.crmanager.dao" targetProject="E:\workspaces\test">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- tableName:想要生成的資料庫表,自動化工具會根據該表的結構生成相應的vo對象 -->
<table tableName="crm_clientinfo" domainObjectName="clientinfo" />
<table tableName="crm_cuslinkman" domainObjectName="cuslinkman" />
<table tableName="crm_cuscontactrecord" domainObjectName="cuscontactrecord" />
</context>
</generatorConfiguration>
儲存,并記錄該檔案的路徑,我的存放路徑為:D:\mybatistool\mybatis-generator-core-1.3.1\genrator.xml
步驟3:
建立檔案存放的工程:我這裡隻建了一個檔案夾,包含一個resources路徑,resources路徑将存放映射的xml檔案。
步驟4:
執行cmd指令,
進入解壓好的mybatis-generator-core-1.3.1.jar所在路徑,我的是:D:\mybatistool\mybatis-generator-core-1.3.1\lib
執行以下指令:
D:\mybatistool\mybatis-generator-core-1.3.1\lib>java -jar mybatis-generator-core-1.3.1.jar -configfile D:\mybatistool\mybatis-generator-core-1.3.1\genrator.xml -overwrite
執行結果如下:
生成檔案如下:
然後可以将生成的代碼移到你的工程目錄,進行對應的編碼。
如果修改了表結構,重新生成代碼時後可能會出現 java.lang.IllegalArgumentException: Result Maps collection already contains value for XXX這個錯誤。
那是因為生成代碼時xml檔案已存在,是以會多出很多重複的代碼,是以必須将目錄下的原檔案删除或者備份,再重新生成。