1、在maven中通过properties节点指定MyBatis Generator配置文件的位置
<properties>
<spring.version>4.1.1.RELEASE</spring.version>
<!--mybatis-generate配置文件位置-->
<mybatis.generator.configurationFile>
${basedir}/src/main/resources/generatorConfig.xml
</mybatis.generator.configurationFile>
</properties>
2、添加Maven插件并设置插件属性
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.0</version>
<!--configuration设置插件属性-->
<configuration>
<!--configurationFile指定配置文件位置-->
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
</configuration>
<!--在maven编译阶段自动生产dao/mapper/bean-->
<!--<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>-->
<!--<configurationFile>${mybatis.generator.configurationFile}</configurationFile>-->
</plugin>
3、对配置文件进行配置(一个实例)
<?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>
<!-- 数据库驱动位置 -->
<classPathEntry location="D:\Program Files\repository\mysql\mysql-connector-java\5.1.25\mysql-connector-java-5.1.25.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 数据库连接地址帐号密码 -->
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/shenyidu"
userId="root"
password="123"/>
<!-- 生成model类文件存放位置 --><!--targetPackage为package名,targetProject为工程目录-->
<javaModelGenerator targetPackage="shop.model" targetProject="src\main\java\"/>
<!-- 生成映射文件存放位置 -->
<sqlMapGenerator targetPackage="shop" targetProject="src\main\resources\mapper"/>
<!-- 生成dao类存放位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="shop.dao" targetProject="src\main\java\">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 对应的数据表和实体类 -->
<table tableName="t_account" domainObjectName="Account" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"/>
<table tableName="t_canteens" domainObjectName="Canteen" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"/>
<table tableName="t_comments" domainObjectName="Comment" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"/>
<table tableName="t_customers" domainObjectName="Customer" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
点击运行: