原文出自:http://blog.csdn.net/itlqi/article/details/49534447
1、新建一個maven項目在pom.xml添加如下:
- <plugins>
- <plugin>
- <groupId>org.mybatis.generator</groupId>
- <artifactId>mybatis-generator-maven-plugin</artifactId>
- <version>1.3.2</version>
- <configuration>
- <verbose>false</verbose>
- <overwrite>false</overwrite>
- </configuration>
- </plugin>
- </plugins>
如果以上插件還無法生成將插件這樣添加【如上也可不用刪除】
- <dependency>
- <groupId>org.mybatis.generator</groupId>
- <artifactId>mybatis-generator-maven-plugin</artifactId>
- <version>1.3.2</version>
- </dependency>
2、在項目src/main/resources 添加generatorConfig.xml文件
generatorConfig.xml 內容:
- <span style="font-size:14px;"><?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="E:\Eclipse\Repository\mysql\mysql-connector-java\5.1.29\mysql-connector-java-5.1.29.jar" />
- <context id="context1" targetRuntime="MyBatis3">
- <commentGenerator>
- <!-- 是否去除自動生成的註釋 true:是 : false:否 -->
- <property name="suppressAllComments" value="true" />
- <!--數據庫連接的信息:驅動類、連接地址、用戶名、密碼 -->
- </commentGenerator>
- <jdbcConnection driverClass="com.mysql.jdbc.Driver"
- connectionURL="jdbc:mysql://localhost:3306/data?useUnicode=true&characterEncoding=UTF-8"
- userId="root" password="123456" />
- <javaModelGenerator targetPackage="org.share.domain.tree.entity"
- targetProject="E:\workspace\share\share-domain\src\main\java" />
- <sqlMapGenerator targetPackage="org.share.domain.tree.mapper"
- targetProject="E:\workspace\share\share-domain\src\main\java" />
- <javaClientGenerator targetPackage="org.share.domain.tree.mapper"
- targetProject="E:\workspace\share\share-domain\src\main\java" type="XMLMAPPER" />
- <!-- shema 數據庫 tableName表明 -->
- <table schema="data" tableName="tree" />
- </context>
- </generatorConfiguration></span>
table其他屬性:
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"
schema即爲數據庫名, tableName爲對應的數據庫表, domainObjectName是要生成的實體類,
如果想要mapper配置文件加入sql的where條件查詢, 可以將enableCountByExample等設爲true,
這樣就會生成一個對應domainObjectName的Example類, enableCountByExample等設爲false時,
就不會生成對應的Example類了.
3、選擇項目中的pom.xml配置文件 右鍵run --》maven bulid --》在對話框中的Goals 輸入mybatis-generator:generate 如圖
4、刷新項目src目錄就可以看到映射的xml 和接口文件了 哈哈哈 祝你好運