1、 maven pom.xml文件中引入plugin插件java
<plugins>mysql
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!-- mybatis-generator 配置文件存放地址 -->
<configurationFile>src/test/resources/generator/mybatis-generator.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
</dependencies>
</plugin>sql
<plugins>數據庫
2、在建立mybatis-generator.xml配置文件api
注意:mybatis-generator.xml文件建立位置與pom.xml文件中plugin插件中指定的配置文件相匹配mybatis
建立 mybatis-generator.xml文件app
<?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="generatorTables" targetRuntime="MyBatis3">
<property name="autoDelimitKeywords" value="false" />
<property name="javaFileEncoding" value="UTF-8" />
<property name="javaFormatter"
value="org.mybatis.generator.api.dom.DefaultJavaFormatter" />
<property name="xmlFormatter"
value="org.mybatis.generator.api.dom.DefaultXmlFormatter" />
<plugin type="org.mybatis.generator.plugins.MapperConfigPlugin">
<property name="fileName" value="mybatis-config.xml" />
<property name="targetPackage" value="/" />
<property name="targetProject" value="src/main/resources" />
</plugin>
<!-- <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">
<property name="searchString" value="Example" /> <property name="replaceString"
value="Criteria" /> </plugin> -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
</commentGenerator> dom
<!-- 數據庫鏈接地址 -->eclipse
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://192.168.0.1:3306/test" userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 指定自動生成model的位置 -->
<javaModelGenerator targetPackage="com.test.dal.dao.mybatis.model"
targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 指定自動生成mapper的位置 -->
<sqlMapGenerator targetPackage="com.test.dal.dao.mybatis.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 指定自動生成mapper接口的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.test.dal.dao.mybatis" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--
<table tableName="student" domainObjectName="Student"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
-->
<!--
<table tableName="teacher" domainObjectName="Teacher"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true" />
<property name="mergeable" value="true" />
</table>
-->
</context>
</generatorConfiguration>maven
注意:具體配置標籤內容請查看mybatis-generator開源文檔或google搜索
3、生成代碼
(1)若是是在eclipse 中,選擇項目或者pom.xml文件,
鼠標單擊右鍵先擇Run AS——>Maven Build… ——>
在Goals框中輸入:mybatis-generator:generate
(2)若是在命令行輸入Maven命令便可
注意:必定是當前項目目錄下運行該命令:
mvn mybatis-generator:generate
注意:若是使用generator已經生成過一次代碼,那麼在下次修改數據庫表結構後,再次生成對應表時,必定要把原來的***Mapper.xml文件和其餘對應的文件進行刪除,不然在***Mapper.xml文件中會生成多個代碼,在運行過程當中會報錯。
代碼生成完,大功告別成。