整体架构和数据流
整体来说,mbg的结构比较简单,清晰。
相对特点:
- 代码的生成没有采用模板,而是把代码和xml的结构定义成了对应的java类
- 支持kotlin语言 (侧面印证kotlin火热)
- mbg设计之初就充分考虑了扩展性,设计了插件机制
mbg扩展机制
MyBatis Generator (MBG) is designed for extensibility. All code generation is performed using a simple DOM representation of Java and XML elements.
The Java DOM is included in the package org.mybatis.generator.api.dom.java
The XML DOM is included in the package org.mybatis.generator.api.dom.xml
Although there are a number of different extension points shown on this page, in most cases it will be far easier to extend MBG through the use of a plugin. See the Implementing Plugins reference page for more information.
The primary extension point for code generation is org.mybatis.generator.api.IntrospectedTable. Implementing a code generator is a non-trivial task and should only be contemplated when you want to completely replace the code generation activities of MBG. In the years since the original release of Abator, very few enhancement requests have appeared that could not be handled by a plugin.
点出扩展方式:
1.通过IntrospectedTable
2.实现plugin接口或者继承PluginAdapter
- Plugin
- PluginAdapter
- CompositePlugin
小结和思考:
- mbg整体架构设计很灵活,学习到做工具的一些思考面和技巧
- 通过mbg生成的代码,也是很好的mybatis的技巧学习辅助,类似通过hibernate生成sql可以学习高效sql一样
- mbg 解决了新生成mapper xml时,可以不覆盖老的,说明作者对用户体验还是很重视,当然既然产品解决这个问题,说明这也是使用mybatis时的一个痛苦点
简单梳理到此,后续写一下扩展的实现。