1.在pom中加入以下配置
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.3</version> <configuration> <configurationFile>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> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.23</version> </dependency> </dependencies> </plugin>
2. 配置文件
<?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> <!-- mybatis-generator:generate --> <context id="base" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true" /> <property name="suppressDate" value="false" /> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/testMybatis" userId="root" password="123456"> // 反向工程目标数据库连接设置 </jdbcConnection> <javaModelGenerator targetPackage="com.m1.module" targetProject="src\main\java"> // 原型类目标配置 <property name="enableSubPackages" value="false" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.m1.mapper" targetProject="src\main\java"> // 映射文件目标配置 <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <javaClientGenerator targetPackage="com.m1.dao" targetProject="src\main\java" type="XMLMAPPER"> // dao层目标配置 <property name="enableSubPackages" value="false" /> </javaClientGenerator> <table schema="testMybatis" tableName="user" domainObjectName="User" //反向的表配置 enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table schema="testMybatis" tableName="dept" domainObjectName="Dept" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
反向表配置中, 指定数据库表,要生成哪些表,就写哪些表。
schema即为数据库名, tableName为对应的数据库表, domainObjectName是要生成的实体类
如果想要mapper配置文件加入sql的where条件查询, 可以将enableCountByExample等设为true,这样就会生成一个对应domainObjectName的Example类, enableCountByExample等设为false时,就不会生成对应的Example类了.