1. 程式人生 > >使用maven生成mybatis程式碼

使用maven生成mybatis程式碼

一:準備工作

1.資料庫,資料表,例如:postgres spring(資料庫) user_t(表)
2.maven專案(例如:SSMLTest)

二:開始

第一步:配置Maven 的pom.xml檔案
也就是在pom.xml檔案中加入下面這一段程式碼

<plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin>

第二步:generatorConfig.xml放置路徑
這裡寫圖片描述

第三步:配置generatorConfig.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="C:/Users/witon/.m2/repository/org/postgresql/postgresql/9.4-1201-jdbc41/postgresql-9.4-1201-jdbc41.jar"
>
</classPathEntry> <context id="context" targetRuntime="MyBatis3" > <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自動生成的註釋true 是 FALSE 否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- 資料庫連線URL 使用者名稱,密碼 --> <jdbcConnection driverClass="org.postgresql.Driver" connectionURL="jdbc:postgresql://127.0.0.1:5432/spring" userId="root" password="postgres"/> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置 --> <javaModelGenerator targetPackage="com.zoey.test.domain" targetProject="src/main/java"> </javaModelGenerator> <!-- 生成對映檔案的包名和位置--> <sqlMapGenerator targetPackage="com.zoey.test.mapping" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成Dao的包名和位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.zoey.test.Dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成的表tableName是資料庫中的表名和檢視名 domainobjectName是實體類名 --> <table tableName="user_t" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" > <property name="useActualColumnNames" value="false"/> </table> </context> </generatorConfiguration>

配置檔案中要注意的幾點:

1.驅動的地址,要注意,使用本地的jar包,所以需要絕對路徑
<classPathEntry location="C:/Users/witon/.m2/repository/org/postgresql/postgresql/9.4-1201-jdbc41/postgresql-9.4-1201-jdbc41.jar"></classPathEntry>

2.要注意生成的pojo的名稱domainObjectName屬性

第四步:執行mybatis-generator:generate命令

選擇專案,右鍵->Run As ->Maven Build->Goals輸入mybatis-generator:generate,點選apply,點選Run

這裡寫圖片描述

結果:

[INFO] Scanning for projects...
[INFO] 
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building SSMLTest Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- mybatis-generator-maven-plugin:1.3.2:generate (default-cli) @ SSMLTest ---
[INFO] Connecting to the Database
[INFO] Introspecting table user_t
log4j:WARN No appenders could be found for logger (org.mybatis.generator.internal.db.DatabaseIntrospector).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[INFO] Generating Record class for table user_t
[INFO] Generating Mapper Interface for table user_t
[INFO] Generating SQL Map for table user_t
[INFO] Saving file UserMapper.xml
[INFO] Saving file User.java
[INFO] Saving file UserMapper.java
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.417 s
[INFO] Finished at: 2017-03-03T11:17:05+08:00
[INFO] Final Memory: 7M/116M
[INFO] ------------------------------------------------------------------------

為自己鼓掌,雖然中間出現好多問題,不過幸好都一一克服了!