第一步:导入如下依赖
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
第二步:导入mbg.xml
目录结构
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<!--suppress ALL -->
<generatorConfiguration>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!--jdbcConnection :数据库连接-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/xx"
userId="root"
password="xxxxx">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- javaModelGenerator :生成pojo的
targetPackage:生成的pojo放在哪个包
targetProject:工程目录 就是放在哪个工程下
-->
<javaModelGenerator targetPackage="com.aaa.pojo"
targetProject="D:\java-code\vue\code\mypros\ssm_vue\src\main\java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--
sqlMapGenerator:sql映射文件生成器,
targetPackage:放到那个包;指定生成xml的地方
-->
<sqlMapGenerator targetPackage="com.aaa.dao"
targetProject="D:\java-code\vue\code\mypros\ssm_vue\src\main\java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--javaClientGenerator :dao接口生成的地方-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.aaa.dao"
targetProject="D:\java-code\vue\code\mypros\ssm_vue\src\main\java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--
一个table 指定要逆向生成的那个表
tableName 写表名
domainObjectName:写这个表对应的对象名就是JavaBean
-->
<table tableName="t_product" domainObjectName="Product"/>
</context>
</generatorConfiguration>
最后一步:(这里我使用的java的方式运行的逆向工程)
按照如下配置即可,
public class MbgTest {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("mbg.xml的完整路径就是在那个盘符下的那个文件夹下的那个项目中的mbg.xml文件");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}