Mybatis利用generator工具快速生成對映配置檔案資訊
阿新 • • 發佈:2019-02-05
瞭解一點Mybatis的都知道它的反向對映機制,但是如果自己去動手親自寫的話,資料庫表多了便工作量變得巨大,現在就叫你如何利用generator自動快速生成對映檔案。
第一步:建立資料庫,並建表user_info和account_info用於測試使用;
<?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="D:\dataFile\workDocuments\Development\generator\mysql-connector-java-5.1.34.jar" /> <!-- <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />--> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 資料庫連結URL、使用者名稱、密碼 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/springBoot-demo" userId="root" password="password"> <!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">--> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成模型的包名和位置 --> <javaModelGenerator targetPackage="andy.model" targetProject="D:\dataFile\workDocuments\Development\generator\src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成的對映檔案包名和位置 --> <sqlMapGenerator targetPackage="andy.mapping" targetProject="D:\dataFile\workDocuments\Development\generator\src"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成DAO的包名和位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="andy.dao" targetProject="D:\dataFile\workDocuments\Development\generator\src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 要生成那些表(更改tableName和domainObjectName就可以) --> <table tableName="user_info" domainObjectName="UserInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> <table tableName="account_info" domainObjectName="AccountInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> </context> </generatorConfiguration>
第三步:cmd命令視窗中,將工作盤切換到generator目錄下,輸入java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite命令,看到下圖資訊便表明對映成功;
第四步:開啟scr目錄檢視已對映生成的