步驟:
1. 新建一個Maven項目:

然后導入maven依賴:
<dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> </dependencies> <build> <plugins> <!--mybatis自動生成代碼插件配置--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
其中:
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
是反向自動生成代碼的配置文件位置!
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="D:\jar\mysql-connector-java-5.1.38.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <!-- 生成的Java文件的編碼 --> <property name="javaFileEncoding" value="UTF-8" /> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自動生成的注釋 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> <property name="useActualColumnNames" value="true" /> </commentGenerator> <!--數據庫鏈接URL,用戶名、密碼 1.一般jdbc數據庫的版本6.x以上,都是com.mysql.cj.jdbc.Driver 其他的低版本就是com.mysql.jdbc.Driver --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/ssm_01?serverTimezone=GMT" userId="root" password="root"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置 要生成的文件位置 --> <javaModelGenerator targetPackage="com.bean" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成映射文件的包名和位置 UserMapper.xml對應的xml文件--> <sqlMapGenerator targetPackage="com.mapper.mapping" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置 UserMapper.java對應的mapper文件 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成的表 tableName是數據庫中的表名或視圖名 domainObjectName是實體類名--> <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="t_commodity" domainObjectName="Commodity" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
這里會有幾個問題:
1)"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" 會報紅,這個時候需要如下操作:


如此就操作完成了:

2)驅動包的導入:
<!-- 數據庫驅動:選擇你的本地硬盤上面的數據庫驅動包-->
<classPathEntry location="D:\jar\mysql-connector-java-5.1.38.jar"/>
這個文件是我們的本地文件,需要我們自己下載導入:
下載鏈接(MySQL):https://mvnrepository.com/artifact/mysql/mysql-connector-java



這時就會下載,然后把下載完的文件的路徑放到代碼中:
<!-- 數據庫驅動:選擇你的本地硬盤上面的數據庫驅動包-->
<classPathEntry location="你下載的文件路徑"/>
3)數據庫連接的數據:
<!--數據庫鏈接URL,用戶名、密碼
1.一般jdbc數據庫的版本6.x以上,都是com.mysql.cj.jdbc.Driver 其他的低版本就是com.mysql.jdbc.Driver
-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/你的數據庫名稱?serverTimezone=GMT" userId="你的賬號" password="你的密碼">
這個標簽的所有東西都要相應改成你的信息
4)在數據庫中創建表,然后在generatorConfig.xml中進行修改:
<!-- 要生成的表 tableName是數據庫中的表名或視圖名 domainObjectName是實體類名-->
<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>
<table tableName="t_commodity" domainObjectName="Commodity" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>
下面是我一個表的截圖:
5)關於生成文件的位置請仔細查看generatorConfig.xml 中相應的注釋,這里不在詳細說明
項目結構圖如下:

最后,在idea中:

如果沒有該按鈕,請點擊如下:


代碼就自動生成了:

!!!請看
關於 逆向代理:由於每次都要生成一次,暫時沒有想到其它辦法來阻止它在每次啟動的時候來生成代碼,我目前的做法是把它注釋掉(pom.xml中的插件)
如果不注釋,那么mapper.xml文件中,每次都會生成相同的代碼!
附錄:
mybatis.org/dtd/mybatis-generator-config_1_0.dtd標紅

