1.在pom.xml配置如下
<!--MBG mybatis逆向工程jar包 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<build> <finalName>chenssm</finalName> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build>
2.配置generatorConfig.xml
曾經出現的經典錯誤
Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate (default-cli) on project ssm: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate failed: Exception getting JDBC Driver: com.mysql.jdbc.Driver -> [Help 1]
沒有指定jdbc驅動,或者路徑不正確!
<?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> <!--指定特定數據庫的jdbc驅動jar包的位置千萬千萬要指定正確,不然就創建不了文件--> <classPathEntry location="F:\STS工具\STS工作空間\chenssm\src\tools\mysql-connector-java-5.1.6-bin.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--配置數據庫連接信息 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/ssm_crud" userId="root" password="kaola"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!--指定javaBean生成的位置 --> <javaModelGenerator targetPackage="com.chencode.pojo" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--指定sql映射文件生成的位置 --> <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!--指定dao接口生成的位置 .mapper接口 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.chencode.dao" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!--table指定每個表得生成策略 --> <table tableName="tbl_emp" domainObjectName="Employee"> </table> <table tableName="tbl_dept" domainObjectName="Department"></table> </context> </generatorConfiguration>
最后要注意一下對應的生成器通用指定的包和路徑不能搞錯一一對應!
注意targetProject里的” . “ 不能省略!
"." 代表的是應用程序的當前目錄 ".." 代表的是應用程序的上級目錄
3.在IDEA里一鍵生成
開始自動生成,最后出現提示 BUILD SUCCESS! 注意如果列表里沒有說你pom.xml里你導入沒有成功,刪除在導入.只有成功導入配置才會出現.