pom文件添加generator所需启动插件及其核心依赖:
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<!--
default,automatically to search file called
generatorConfig.xml at '/src/main/resources/generatorConfig.xml'
if we put it to other places or named the file with other name
add the following configuration
-->
<!--=============**指明指定文件,否则会默认到'/src/main/resources/‘目录找名为generatorConfig.xml的文件**==================-->
<configuration>
<configurationFile>src/main/resources/BG.xml</configurationFile>
</configuration>
<!--==============================-->
</plugin>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
写好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>
<properties resource="db.properties"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator >
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="${jdbc.url}"
userId="${jdbc.username}"
password="${jdbc.password}">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="pojo" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="book_info" domainObjectName="Book_info" />
<table tableName="book_type" domainObjectName="Book_type"/>
</context>
</generatorConfiguration>
运行:
你可能遇到错误:
Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate failed:
Exception getting JDBC
Driver: com.mysql.jdbc.Driver (mybatis逆向工程)
解决:把pom文件的mysql驱动依赖复制一份加到generator启动插件中去,注意是复制
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<!--
default,automatically to search file called
generatorConfig.xml at '/src/main/resources/generatorConfig.xml'
if we put it to other places or named the file with other name
add the following configuration
-->
<!--===============================-->
<configuration>
<configurationFile>src/main/resources/BG.xml</configurationFile>
</configuration>
<!--============**在这里添加**==================-->
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
</dependencies>
</plugin>
- Maven插件内看不到mybatis-generator插件
- 解决:只需要在pom.xml中将pluginManagement和generator所在的plugins同级就可以看到插件了