在intellij新建一个maven工程,在pom.xml文件中加上以下插件:
<code><plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>
src/main/resources/generatorConfig.xml
</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>
</plugins></code>
再配置generatorConfig.xm:
<code><?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"></code>
<code><generatorConfiguration>
<classPathEntry
location="E:/study/java/maven/repo/mysql/mysql-connector-java/5.1.35/mysql-connector-java-5.1.35.jar"/>
<context id="my" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="false"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/estate-customer?characterEncoding=utf-8"
userId="xxxx"
password="xxxx"/>
<javaModelGenerator targetPackage="com.zgz.entity"
targetProject="E:/study/project/SpringMVC/src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.zgz.entity.xml"
targetProject="E:/study/project/SpringMVC/src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator targetPackage="com.zgz.entity.mapper"
targetProject="E:/study/project/SpringMVC/src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="t_role" domainObjectName="Role"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="t_function" domainObjectName="Function"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration></code>
设置好自动数据库连接,生成xml,entity,mapper的路径以及对应的表
不过用mybatis-generator-core自动生成xml文件,会有重复的代码,好像重复了两次,所以会导致以下错误,去掉重复部分即可。
<code>Result Maps collection already contains value for xxx</code>
在以上部分设置好了,就可以使用配置的该插件进行自动生成代码了: