情景介紹
編寫代碼的過程中,我們總希望能有一個插件能快速生成公用的相似的代碼。感覺mybatis-generator用起來不錯。下面就來總結一下它的使用方法。
使用步驟
- 一、新建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>
<!-- 引入配置文件 -->
<!--TODO 注意!!!自動生成代碼 要把這句放開,鏈接數據庫-->
<properties resource="jdbc.properties"/>
<!-- 指定數據連接驅動jar地址 -->
<classPathEntry location="F:\SVN_Info\cloudTree\trustzhyq\src\e3izm\src\main\webapp\WEB-INF\lib\mysql-connector-java-5.1.29.jar"/>
<context id="context" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自動生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<!-- 數據庫的相關配置 -->
<jdbcConnection driverClass="${driverClasss}" connectionURL="${jdbcUrl}"
userId="${username}" password="${password}"/>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 實體類生成的位置 -->
<javaModelGenerator targetPackage="com.trust.e3izm.ressvc.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- *Mapper.xml 文件的位置 ,targetPackage:包名,targetProject:項目下的路徑-->
<sqlMapGenerator targetPackage="ressvc" targetProject="src/main/resources/mapper">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- Mapper 接口文件的位置 -->
<javaClientGenerator targetPackage="com.trust.e3izm.ressvc.dao" targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 配置表信息 -->
<!--第三方服務類型-->
<table schema="e3iz" tableName="THIRDPTYSVC_TYPE"
domainObjectName="Thirdptysvc_type" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false">
</table>
<!--xxtable-->
<!--如果生成n個表,那就將上面的那段table代碼copy n份-->
</context>
</generatorConfiguration>
- 二、在pom.xml導入依賴包
<plugins>
<plugin>
<!--Mybatis-generator插件,用于自動生成Mapper和POJO-->
<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>
注意
1.這段代碼要放到
<build>
<finalName>e3izm</finalName>
<!-- 將上面這段代碼放到pom.xml文件的這個位置-->
</build>
2.maven2下載關于generator,maven依賴包下載不下來,需要更改為maven3才能下載下來
maven3更改.png
-
三、新建maven運行器
maven.png
<!-- 配置的運行命令-->
mybatis-generator:generate -e
好了,大功告成,運行maven運行器即可!
如果有什么問題,可以閱讀官方文檔。
MyBatis生成器官方文檔