1.pom.xml中添加依赖
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
2.pom.xml中添加插件
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
3.编写mbg配置文件
在src/main/resources下创建文件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>
<context id="MySQLTables" targetRuntime="MyBatis3">
<!-- 抑制注释生成 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- mvn mybatis-generator:generate 配置数据库位置 ,配置虚拟机上的mysql ip地址;不采用安全协议连接,否则无法逆向生成 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://IP地址/数据库名?useSSL=false"
userId="数据库账户" password="数据库密码">
</jdbcConnection>
<!--整数型不强制映射-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- javaBean生成在哪里 -->
<javaModelGenerator
targetPackage="com.lzl.demo.bean"
targetProject="..\项目名称\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- sqlMap sql映射文件(xml mapper文件) -->
<sqlMapGenerator targetPackage="mybatis.mapper"
targetProject="..\项目名称\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- javaClient:java接口生成的地方 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.lzl.demo.mapper"
targetProject="..\项目名称\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 会逆向数据库中所有的表 -->
<table schema="" tableName="%"></table>
<!-- 指定数据库中的表进行逆向 -->
<!-- <table schema="TPermission" tableName="t_permission"></table> -->
</context>
</generatorConfiguration>
4.运行命令逆向生成
使用maven build... 执行如下命令:
mybatis-generator:generate
完成逆向操作