1.把pom.xml中的build块改为如下代码
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- mybatis代码自动生成插件,即逆向工程 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<verbose>false</verbose>
<overwrite>false</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<!-- 此配置不可缺,否则mybatis的Mapper.xml将会丢失 -->
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--指定资源的位置-->
<resource>
<directory>src/main/</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
2.在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="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释。true:去除 false:不去除 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- Mysql数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/imdbtop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8"
userId="root"
password="xxx">
</jdbcConnection>
<!-- Oracle数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:xxxx"
userId="root"
password="root">
</jdbcConnection> -->
<!-- 默认false,把JDBC DECIMAL NUMERIC类型解析为Integer
为true,把JDBC DECIMAL NUMERIC类型解析为java.math.BigDecimal类型
-->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.yd.test.po" targetProject="./src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值,清理值前后的空格,去除首尾空格-->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.yd.test.mapper" targetProject="./src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.yd.test.mapper" targetProject="./src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<!--<table schema="springmvc_db" tableName="Admin"/>
<table schema="springmvc_db" tableName="Comment"/>
<table schema="springmvc_db" tableName="Grade"/>
<table schema="springmvc_db" tableName="News"/>-->
<table schema="springmvc_db" tableName="imdbtop250"/>
<!--<table schema="springmvc_db" tableName="Type"/>
<table schema="springmvc_db" tableName="User"/>-->
</context>
</generatorConfiguration>
3.在application.properties中添加
mybatis.config-location=classpath:generatorConfig.xml
mybatis.mapper-locations=classpath:com/yd/test/mapper/*.xml
最后运行maven中的插件即可逆向工程成功
注意:!!!逆向工程完成之后,把第三步的那两句话删除,否则在运行时候会报错
另外项目运行的时候可能会出现
Result Maps collection already contains value for com.demo2.demo.mapper.CustomTableMapper.BaseResultMap
的错误,这是因为逆向工程生成的xml中多了重复的代码,将重复的代码删除就没有错误了