在使用 idea mybatis.generator 生成的代碼,遇到 生成的代碼很多重復的地方,
雖然代碼是生成的,我們也不應該允許重復的代碼出現,因為這些代碼后期都要來手動維護。
對於生成時間戳注釋、Example類型,xml 等大多都可以通過xml配置來,讓其不生成。
然而 對於一些符合自己編碼習慣的代碼風格再通過配置來生成就不大現實了。對於這種情況,我么可以通過擴展 mybatis.generator 的插件來解決。
插件的編寫說明網上有很多,列出幾個自己看過的
http://generator.sturgeon.mopaas.com/reference/extending.html
http://blog.geyuxu.com/2015/11/02/java/在 Intellij 中添加mybatis-generator分頁插件/
Intellij IDEA 14中使用MyBatis-generator 自動生成MyBatis代碼
mybatis generator生成帶有分頁的Mybatis代碼
很多網友 提供了 對 dto 進行序列化、分頁的各種插件,生成插件后,我們如何簽入到 IDEA 中呢?
step 1.將我們的需要的插件 ,例如:SerializablePlugin、BaseMapperGeneratorPlugin,我們單獨放在項目中生成一個jar包
step 2.在我們的實際項目中,將 step1 中生成的jar 包 放到我們本地的maven倉儲中
具體步驟可參考 這位博友的操作說明,這樣做的目的就是,我們需要將step1 中生成jar 在為 mybatis.generator 插件配置時,直接從本地倉儲能找到改文件。
命令:
install:install-file -Dfile=/Users/geyuxu/Desktop/pagination-0.1.jar -DgroupId=com.geyuxu.utils -DartifactId=pagination -Dversion=0.1 -Dpackaging=jar
step 3: 這一步 就是要為我們的 org.mybatis.generator 插件進行配置,告訴他 到那個jar包中去找 我們提供給他的插件
配置如下(如果不盡心這一步配置,可能會出現如下異常:
Caused by: org.apache.maven.plugin.PluginExecutionException: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate failed: Cannot instantiate object of type com.)
(ps:如果按照以上步驟進行操作,依然出現上述異常,那幾手動把本地maven倉儲中的改jar文件刪掉,項目中的引用也刪掉,重新生成jar 重新引用)
<!--mybatis-generator-maven-plugin插件來完成MyBatis model 和Mapper文件--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <!--Mybatis自動生成規則配置文件地址--> <configurationFile>src/main/resources/mybatis/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <dependencies> <dependency> <artifactId>xxxx-xxx-utils</artifactId> <groupId>xx.xxxx.xxx.xxx</groupId> <version>1.0</version> </dependency> </dependencies> </plugin>