1、在Eclipse安裝mybatis generator自動生成工具
(1)下載下傳mybatis生成架包工具MyBatis_Generator_1.3.1.zip,解壓jar包把features、plugins檔案夾下的架包分别拷貝到eclipse安裝目錄下的features、plugins檔案夾(複制和替換eclipse原有的這兩個檔案夾)。然後重新開機eclipse。

(2)在pom.xml中添加mybatis-generator-maven-plugin的jar包、以及連接配接資料庫的包
<!-- mybatis generator自動生成jar包 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<type>maven-plugin</type>
</dependency>
<!-- java連接配接資料庫的包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-Java</artifactId>
<version>5.1.6</version>
</dependency>
(3)配置generator.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>
<!-- 資料庫驅動包位置 (改成自己存放的具體路徑) -->
<classPathEntry location="D:/softwaretools/someNeeded/mysql-connector-java-5.1.34.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 資料庫連結URL、使用者名、密碼 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/student" userId="root" password="root"> </jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成模型的包名和位置 ,targetPackage:包名,targetProject:你的項目名-->
<javaModelGenerator targetPackage="com.danni.model.entity" targetProject="mybatis-maven">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成的映射檔案包名和位置 -->
<sqlMapGenerator targetPackage="com.danni.model.dao" targetProject="mybatis-maven">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.danni.model.dao" targetProject="mybatis-maven">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- tableName:資料庫中的表名,domainObjectName:表名對應的類名 -->
<table tableName="admins" domainObjectName="Admins" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="employee" domainObjectName="Employee" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="department" domainObjectName="Department" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
(4)選中generator.xml,右鍵選擇Generate MyBatis/IBATIS Artifacts,點選即可生成代碼檔案。