新建一個空的XML配置文件,名稱可以隨便取,這裏以generatorConfig.xml爲名。最好將這個文件放在下載後的lib目錄中,如圖:
其中mysql的驅動可以隨便放在非中文路徑的地方,這裏爲了方便就放在lib目錄下。
自動生成最重要的就是配置文件的書寫,現在就開始介紹generatorConfig.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="mysql-connector-java-5.0.6-bin.jar"/>
- <context id="DB2Tables" targetRuntime="MyBatis3">
- <commentGenerator>
- <property name="suppressDate" value="true"/>
- <!-- 是否去除自動生成的註釋 true:是 : false:否 -->
- <property name="suppressAllComments" value="true"/>
- </commentGenerator>
- <!--數據庫鏈接URL,用戶名、密碼 -->
- <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/test" userId="test" password="test">
- </jdbcConnection>
- <javaTypeResolver>
- <property name="forceBigDecimals" value="false"/>
- </javaTypeResolver>
- <!-- 生成模型的包名和位置-->
- <javaModelGenerator targetPackage="test.model" targetProject="src">
- <property name="enableSubPackages" value="true"/>
- <property name="trimStrings" value="true"/>
- </javaModelGenerator>
- <!-- 生成映射文件的包名和位置-->
- <sqlMapGenerator targetPackage="test.mapping" targetProject="src">
- <property name="enableSubPackages" value="true"/>
- </sqlMapGenerator>
- <!-- 生成DAO的包名和位置-->
- <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject="src">
- <property name="enableSubPackages" value="true"/>
- </javaClientGenerator>
- <!-- 要生成哪些表-->
- <table tableName="about" domainObjectName="AboutDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
- <table tableName="user" domainObjectName="UserDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
- <table tableName="syslogs" domainObjectName="SyslogsDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
- </context>
- </generatorConfiguration>
1、其中需要注意的有數據庫驅動、數據庫URL、用戶名、密碼、生成模型的包名和位置、生成映射文件的包名和位置、生成DAO的包名和位置以及最後需要生成的表名和對應的類名。
四、運行
需要通過CMD命令行方式來運行,首先可以先準備一個運行的腳本,這裏使用的腳本是:java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
需要注意的是:mybatis-generator-core-1.3.2.jar爲下載的對應版本的jar,generatorConfig.xml 爲配置文件名,如果不爲這個可以在這裏進行修改。
啓動cmd進入到“F:\soft\mybatis-generator-core-1.3.2\lib”這個目錄下,如圖: