这里我想说的不是一般情况下的自定义注解生成器的生成步骤,如果想了解一般情况下的可以到 http://blog.youkuaiyun.com/qq_21251983/article/details/50731368 了解。
这里我想说的是使用MyBatis-Generator和通用Mapper整合时注意的问题,关于通用Mapper的用法可以浏览 http://git.oschina.net/free/Mapper?spm=5176.100239.blogcont5831.6.p7abUt
当MyBatis-Generator和通用Mapper整合时默认的注解生成器就不是org.mybatis.generator.internal.DefaultCommentGenerator,而是tk.mybatis.mapper.generator.MapperCommentGenerator,这是通用Mapper自带的注解生成器,里面主要是在原先的注释上添加一些有关数据库的注释,例如@Column,@ID等等,有兴趣的可以自己去查看。
所以generatorConfig.xml文件里的 commentGenerator 标签就会变得没用了,所以这个时候想自定义注解生成器就有点麻烦了。
若想自定义注解生成器的话,最简单直接的方法就是集成重写通用Mapper这个插件类(注意这里的重写是把这个插件类MapperPlugin的代码完全复制到一个全新的类上),然后在里面添加一个属性 commentGeneratorType
//注释生成器类
private String commentGeneratorType;
然后在 setProperties 方法上添加如下代码
//设置自定义的注释生成器
commentGeneratorType = this.properties.getProperty("commentGeneratorType");
if (!StringUtil.isEmpty(commentGeneratorType)) {
commentCfg = new CommentGeneratorConfiguration();
commentCfg.setConfigurationType(commentGeneratorType);
context.setCommentGeneratorConfiguration(commentCfg);
}
另外generatorConfig.xml文件配置的 plugin 要修改为
<!--使用通用Mapper插件-->
<plugin type="com.meng.mybatis.common.MyCommonMapperPlugin">
<!-- 生成的Mapper都要继承的BaseMapper类 -->
<property name="mappers" value="com.meng.mybatis.common.MyBaseMapper"/>
<!-- 设置自定义注释生成器,默认是 MapperCommentGenerator -->
<property name="commentGeneratorType" value="com.meng.mybatis.common.MyMapperCommentGenerator"/>
</plugin>
你可以通过修改commentGeneratorType属性来设置自定义的注释生成器,可以通过继承默认的注解生成器MapperCommentGenerator ,修改里面的方法,生成你喜欢的注释。
还有一点要注意的是如果你是使用maven插件来运行mybatis-generator,需要把这些你自定义的这些类放到把成jar包,并且在mybatis-generator-maven-plugin插件中添加相关依赖,否则会报找不到这些类的错误。
而使用Java方式运行MBG可以避免该问题。
本人新手,若是哪里说错了,还请大神们指教指教。
转载请附上地址。