mybatis generator maven外掛自動生成程式碼__個人記錄
阿新 • • 發佈:2019-02-20
本文只是對於mybatis generator maven外掛使用的個人記錄,作個備忘,同時也希望對進來的人有所幫助!
一.pom檔案中引入外掛
此處我引用的是1.3.5版本的,主要是1.3.4 版本及以上可以設定Mapper(Dao)字尾 如mapperName="LotteryDao"
<plugin> <!-- 用maven mybatis外掛 如果不在plugin裡面新增依賴包得引用的話,會找不到相關得jar包, 在plugin外部得jar包,他不會去找到並執行, 所以要把plugin執行依賴得jar配置都放在裡面 --> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <!-- 引用依賴庫的版本 --> <version>5.1.38</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> </dependencies> </plugin>
注意,如果發現報錯Plugin execution not covered by lifecycle configuration: 需要在plugins的外層加上pluginManagement標籤.
二.新增generatorConfig.xml檔案
在專案中新增Source File 目錄src/main/resources,裡面新建generatorConfig.xml檔案
檔案內容如下:[有待補充及改進]
同樣src/main/resources,裡面新建jdbc.properties檔案,是上個檔案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> <!-- 引入配置檔案 --> <properties resource="jdbc.properties"/> <!-- 一個數據庫一個context --> <context id="infoGuardian"> <!-- 註釋 --> <commentGenerator > <property name="suppressAllComments" value="true"/><!-- 是否取消註釋 --> <property name="suppressDate" value="true" /> <!-- 是否生成註釋代時間戳--> </commentGenerator> <!-- jdbc連線 --> <jdbcConnection driverClass="${jdbc_driverClassName}" connectionURL="${jdbc_url}" userId="${jdbc_username}" password="${jdbc_password}" /> <!-- 型別轉換 --> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自動轉化以下型別(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成實體類地址 --> <javaModelGenerator targetPackage="com.tangdi.production.convenience.domain" targetProject="src/main/java" > <!-- 是否在當前路徑下新加一層schema,eg:fase路徑com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="false"/> <!-- 是否針對string型別的欄位在set的時候進行trim呼叫 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成mapxml檔案 --> <sqlMapGenerator targetPackage="convenience-mapper" targetProject="src/main/resources" > <!-- 是否在當前路徑下新加一層schema,eg:fase路徑com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- 生成mapxml對應client,也就是介面dao --> <javaClientGenerator targetPackage="com.tangdi.production.convenience.dao" targetProject="src/main/java" type="XMLMAPPER" > <!-- 是否在當前路徑下新加一層schema,eg:fase路徑com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 配置表資訊 1.3.4 版本及以上可以設定Mapper(Dao)字尾 如mapperName="LotteryDao" tableName為對應的資料庫表 domainObjectName是要生成的實體類名 enable*ByExample 是否生成 example類 --> <table tableName="convenience_lotteryuser_inf" domainObjectName="LotteryInf" mapperName="LotteryDao" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> <!-- 忽略列,不生成bean 欄位 --> <!-- <ignoreColumn column="FRED" /> --> <!-- 指定列的java資料型別 --> <!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> --> </table> </context> </generatorConfiguration>
jdbc_driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://127.0.0.1:3306/dev
jdbc_username=dev
jdbc_password=*******
三.右鍵pom檔案,Run As 用maven build --> Goals中輸入 mybatis-generator:generate執行即可
四.mybatis generator gui介面工具 推薦使用
另外,有網友基於mybatis generator開發了一款介面工具, 本工具可以使你非常容易及快速生成Mybatis的Java POJO檔案及資料庫Mapping檔案。
地址:https://github.com/astarring/mybatis-generator-gui
經試用,也是非常好用! 一般第一次設定過之後,後面雙擊表名即可!