一、创建generatorConfig.xml文件
可以选择在resource下创建generatorConfig.xml文件,也可以创建子文件夹,在其下创建generatorConfig.xml文件(在pom.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>
<!-- 数据库驱动包位置 -->
<!-- 由于在pom.xml中加入插件时已经配置数据库驱动包,所以此处不必配置了-->
<!--<classPathEntry location="mysql-connector-java-5.1.39.jar"/>-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 忽略所有注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库链接URL、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/fir_mybatis?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai"
userId="username"
password="password">
</jdbcConnection>
<!-- 如果数据库列的类型是DECIMAL或NUMERIC, Java类型解析器将始终使用Java .math. bigdecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成实体类的包名和位置 -->
<javaModelGenerator targetPackage="com.tywl.entity" targetProject="./src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成的映射xml文件包名和位置 -->
<sqlMapGenerator targetPackage="mapping" targetProject="./src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.tywl.dao" targetProject="./src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
<table tableName="user" domainObjectName="User" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true" />
</context>
</generatorConfiguration>
Java .math. bigdecimal(感谢作者):(3条消息) java 中 BigDecimal 详解_代码君的博客-CSDN博客_bigdecimal
注意:若数据库中有两个命名相同的表,会针对最后一个表进行逆向生成。
二、在pom.xml中进行相关配置
在插件部分进行相关配置:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- mybatis maven generator 逆向工程插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<!--数据库连接-->
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
</dependencies>
<!--generatorConfig.xml配置 注意路径对应关系-->
<configuration>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
三、逆向生成
IDEA编辑器点击右上方的maven,找到插件plugins中的mybatis-generator插件,点击mybatis-generator:generate即可。
结果如图:
OK!!