第一步:下載下傳mybatis-generator工具包
GitHub位址:https://github.com/yundianzixun/mybatis-generator-1.35,如下圖所示:
第二步:修改配置資訊
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> <!-- 資料庫驅動 --> <classPathEntry location="C:\Users\user\.m2\repository\mysql\mysql-connector-java\5.1.26\mysql-connector-java-5.1.26.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <property name="javaFileEncoding" value="UTF-8"/> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://xxx:3306/test?useSSL=false" userId="root" password="123456"/> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置 --> <javaModelGenerator targetPackage="com.virtuousone.generator.po" targetProject="D:\Test\generatorConfig\generator\src\main\java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> <property name="suppressAllComments" value="true"/> </javaModelGenerator> <!-- 生成映射檔案的包名和位置 --> <sqlMapGenerator targetPackage="mapping" targetProject="D:\Test\generatorConfig\generator\src\main\resources"> <property name="enableSubPackages" value="true"/> <property name="suppressAllComments" value="true"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.virtuousone.generator.dao" targetProject="D:\Test\generatorConfig\generator\src\main\java"> <property name="enableSubPackages" value="true"/> <property name="suppressAllComments" value="true"/> </javaClientGenerator> <table tableName="tp_user" domainObjectName="ytWhSkuInventoryLog" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
修改點1:資料庫配置
-
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
-
connectionURL="資料庫URL" userId="資料庫使用者名" password="資料庫密碼"> </jdbcConnection>
修改點2:生成model類存放位置
-
#com.itunion.wxshop.model 可修改為自己項目映射目錄
-
<javaModelGenerator targetPackage="com.itunion.wxshop.model" targetProject="src">
-
<property name="enableSubPackages" value="true"/>
-
<property name="trimStrings" value="false"/>
-
</javaModelGenerator>
修改點3:生成mapping檔案存放位置
-
#targetPackage 報名可以修改
-
<!-- 生成映射檔案存放位置 -->
-
<sqlMapGenerator targetPackage="mapping" targetProject="src">
-
<property name="enableSubPackages" value="true"/>
-
</sqlMapGenerator>
修改點4:生産Dao類存放位置
-
#targetPackage 目錄可修改
-
<javaClientGenerator type="XMLMAPPER" targetPackage="com.itunion.wxshop.mapper" targetProject="src">
-
<property name="enableSubPackages" value="true"/>
-
</javaClientGenerator>
修改點5:生成對應表及類名
-
#對應自己的表資訊(可copy多個)
-
<table tableName="user_info" domainObjectName="UserInfo"
-
enableCountByExample="false"
-
enableUpdateByExample="false"
-
enableDeleteByExample="false"
-
enableSelectByExample="false"
-
selectByExampleQueryId="false">
-
</table>
- 結果檢視
第四步:将生成的檔案放到自己項目中
generatorConfig.xml 檔案裡面的項目路徑配置好了 直接copy就可以用
另外也可以通過設定來執行代碼
1.Edit Configurations
2添加maven
Command Line 填寫
mybatis-generator:generate -e
3 點選下方 Apply
4. 直接run對應的項目
5.運作完成後可以看到自己對應的代碼塊方法
恭喜您完成任務