1.在pom.xml中添加插件 mybatis-generator-maven-plugin



<properties>
<mysql.driver.version>8.0.27</mysql.driver.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<!-- mybatisGenerator 的配置 -->
<configuration>
<!-- 在控制台打印执行日志 -->
<verbose>true</verbose>
<!-- generator 工具配置文件的位置 -->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<!-- 是否覆盖 -->
<!-- 此处要特别注意,如果不加这个设置会导致每次运行都会在原目录再次创建-->
<!--注意:生成的xml文件会不断追加-->
<overwrite>true</overwrite>
</configuration>

<!-- 添加一个mysql的依赖,防止等会找不到driverClass -->
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.driver.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>


2.在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" defaultModelType="flat">

<!-- 链接数据库的配置 MySQL8之前的驱动com.mysql.jdbc.Driver,新的驱动com.mysql.cj.jdbc.Driver,并且要加时区-->
<commentGenerator>
<!-- 是否取消自动生成时的注释 -->
<property name="suppressAllComments" value="true"/>
<!-- 是否取消在注释中加上时间 -->
<property name="suppressDate" value="true"/>
</commentGenerator>

<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://192.168.1.113:3306/online-taxi-three?useUnicode=true&characterEncoding=utf-8&tinyInt1isBit=false&allowMultiQueries=true"
userId="root" password="123456"/>

<!-- 公共设置 -->
<javaTypeResolver>
<!-- 默认 false,把 JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true 时解析为 java.math.BigDecimal -->
<property name="forceBigDecimals" value="true" />  
</javaTypeResolver>

<!-- 关于生成实体类的设置 -->
<!-- targetPackage 生成代码的目标目录 -->
<!-- targetProject 目录所属位置 -->
<javaModelGenerator targetPackage="com.dandan.cloudzuul.entity" targetProject="src/main/java">
<!-- 在targetPackge的基础上根据schema再生成一层package 默认flase -->
<property name="enableSubPackages" value="true"/>
<!-- 是否在get方法中 对String类型的字段做空的判断 -->
<property name="trimStrings" value="true"/>
<!-- 是否生成一个包含所有字段的构造器 -->
<property name="constructorBased" value="false"/>
<!-- 是否创建一个不可变类-->
<property name="immutable" value="false"/>
</javaModelGenerator>

<!--关于生成映射文件的设置-->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<!--同上-->
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>

<!--关于生成dao层的设置-->
<javaClientGenerator type="mapper" targetPackage="com.dandan.cloudzuul.dao" targetProject="src/main/java">
<!--同上-->
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>

<!--需要生成的代码对应的表名 tableName 代表你的数据库表,domainObjectName 映射后的对象名称-->
<table tableName="common_gray_rule" schema="" domainObjectName="CommonGrayRule"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>


3.在idea的edit configurations里创建maven运行插件即可。

working dictionary选择项目的位置

command line 中输入 mybatis-generator:generate -e