Skip to content

Commit 0e244ee

Browse files
author
hewei
committed
新增注释插件
1 parent ffcbdc1 commit 0e244ee

File tree

2 files changed

+350
-7
lines changed

2 files changed

+350
-7
lines changed

README.md

Lines changed: 325 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
* [Selective选择插入更新增强插件(SelectiveEnhancedPlugin)](#10-selective选择插入更新增强插件)
1717
* [Table增加前缀插件(TablePrefixPlugin)](#11-table增加前缀插件)
1818
* [Table重命名插件(TableRenamePlugin)](#12-table重命名插件)
19+
* [自定义注释插件(CommentPlugin)](#13-自定义注释插件)
1920

2021
---------------------------------------
2122
Maven引用:
2223
```xml
2324
<dependency>
2425
<groupId>com.itfsw</groupId>
2526
<artifactId>mybatis-generator-plugin</artifactId>
26-
<version>1.0.10</version>
27+
<version>1.0.11</version>
2728
</dependency>
2829
```
2930
---------------------------------------
@@ -504,7 +505,7 @@ public class Test {
504505
```
505506
### 10. Selective选择插入更新增强插件
506507
项目中往往需要指定某些字段进行插入或者更新,或者把某些字段进行设置null处理,这种情况下原生xxxSelective方法往往不能达到需求,因为它的判断条件是对象字段是否为null,这种情况下可使用该插件对xxxSelective方法进行增强。
507-
WAREN:配置插件时请把插件配置在所有插件末尾最后执行,这样才能把上面提供的某些插件的Selective方法也同时增强!
508+
>warning:配置插件时请把插件配置在所有插件末尾最后执行,这样才能把上面提供的某些插件的Selective方法也同时增强!
508509
509510
插件:
510511
```xml
@@ -555,12 +556,12 @@ public class Test {
555556
### 11. Table增加前缀插件
556557
项目中有时会遇到配置多数据源对应多业务的情况,这种情况下可能会出现不同数据源出现重复表名,造成异常冲突。
557558
该插件允许为表增加前缀,改变最终生成的Model、Mapper、Example类名以及xml名。
558-
warning: 请原谅我蹩脚的英文水平,一直没发现插件名称错了,1.0.9版本错误命名为TableSuffixPlugin,属性为suffix请新版本用户对应修正!
559+
>warning: 请原谅我蹩脚的英文水平,一直没发现插件名称错了,1.0.9版本错误命名为TableSuffixPlugin,属性为suffix请新版本用户对应修正!
559560
560561
插件:
561562
```xml
562-
<!-- Table增加前缀插件 -->
563563
<xml>
564+
<!-- Table增加前缀插件 -->
564565
<plugin type="com.itfsw.mybatis.generator.plugins.TablePrefixPlugin">
565566
<!-- 这里配置的是全局表前缀,当然在table中配置的值会覆盖该全局配置 -->
566567
<property name="prefix" value="Cm"/>
@@ -594,12 +595,12 @@ public class Test {
594595
这种情况下我们就希望能有类似[columnRenamingRule](http://www.mybatis.org/generator/configreference/columnRenamingRule.html)这种重命名插件来修正最终生成的Model、Mapper等命名。
595596
>该插件解决:使用正则替换table生成的Model、Example、Mapper等命名。
596597
597-
warning:和插件[Table增加前缀插件](#11-table增加前缀插件)联合使用时,需注意顺序问题。
598+
>warning:和插件[Table增加前缀插件](#11-table增加前缀插件)联合使用时,需注意顺序问题。
598599
599600
插件:
600601
```xml
601-
<!-- Table重命名插件 -->
602602
<xml>
603+
<!-- Table重命名插件 -->
603604
<plugin type="com.itfsw.mybatis.generator.plugins.TablePrefixPlugin">
604605
<!-- 可根据具体需求确定是否配置 -->
605606
<property name="searchString" value="^T"/>
@@ -631,4 +632,322 @@ public class Test {
631632
// xml文件名: TBookMapper.xml -> TestDbMapper.xml
632633
}
633634
}
635+
```
636+
### 12. 自定义注释插件
637+
Mybatis Generator是原生支持自定义注释的(commentGenerator配置type属性),但使用比较麻烦需要自己实现CommentGenerator接口并打包配置赖等等。
638+
该插件借助freemarker极佳的灵活性实现了自定义注释的快速配置。
639+
>warning: 下方提供了一个参考模板,需要注意${mgb}的输出,因为Mybatis Generator就是通过该字符串判断是否为自身生成代码进行覆盖重写。
640+
641+
>warning: 请注意拷贝参考模板注释前方空格,idea等工具拷贝进去后自动格式化会造成格式错乱。
642+
643+
>warning: 模板引擎采用的是freemarker所以一些freemarker指令参数(如:<#if xx></#if>、${.now?string("yyyy-MM-dd HH:mm:ss")})都是可以使用的,请自己尝试。
644+
645+
| 注释ID | 传入参数 | 备注 |
646+
| ----- | ----- | ---- |
647+
| addJavaFileComment | mgb<br>compilationUnit | Java文件注释 |
648+
| addComment | mgb<br>xmlElement | Xml节点注释 |
649+
| addRootComment | mgb<br>rootElement | Xml根节点注释 |
650+
| addFieldComment | mgb<br>field<br>introspectedTable<br>introspectedColumn | Java 字段注释(非生成Model对应表字段时,introspectedColumn可能不存在) |
651+
| addModelClassComment | mgb<br>topLevelClass<br>introspectedTable | 表Model类注释 |
652+
| addClassComment | mgb<br>innerClass<br>introspectedTable | 类注释 |
653+
| addEnumComment | mgb<br>innerEnum<br>introspectedTable | 枚举注释 |
654+
| addInterfaceComment | mgb<br>innerInterface<br>introspectedTable | 接口注释(itfsw插件新增类型) |
655+
| addGetterComment | mgb<br>method<br>introspectedTable<br>introspectedColumn | getter方法注释(introspectedColumn可能不存在) |
656+
| addSetterComment | mgb<br>method<br>introspectedTable<br>introspectedColumn | getter方法注释(introspectedColumn可能不存在) |
657+
| addGeneralMethodComment | mgb<br>method<br>introspectedTable | 方法注释 |
658+
659+
插件:
660+
```xml
661+
<xml>
662+
<!-- 自定义注释插件 -->
663+
<plugin type="com.itfsw.mybatis.generator.plugins.CommentPlugin">
664+
<!-- 自定义模板路径 -->
665+
<property name="template" value="src/main/resources/mybatis-generator-comment.ftl" />
666+
</plugin>
667+
</xml>
668+
```
669+
使用(参考模板):
670+
```xml
671+
<?xml version="1.0" encoding="UTF-8"?>
672+
<template>
673+
<!-- #############################################################################################################
674+
/**
675+
* This method is called to add a file level comment to a generated java file. This method could be used to add a
676+
* general file comment (such as a copyright notice). However, note that the Java file merge function in Eclipse
677+
* does not deal with this comment. If you run the generator repeatedly, you will only retain the comment from the
678+
* initial run.
679+
* <p>
680+
*
681+
* The default implementation does nothing.
682+
*
683+
* @param compilationUnit
684+
* the compilation unit
685+
*/
686+
-->
687+
<comment ID="addJavaFileComment"></comment>
688+
689+
<!-- #############################################################################################################
690+
/**
691+
* This method should add a suitable comment as a child element of the specified xmlElement to warn users that the
692+
* element was generated and is subject to regeneration.
693+
*
694+
* @param xmlElement
695+
* the xml element
696+
*/
697+
-->
698+
<comment ID="addComment"><![CDATA[
699+
<!--
700+
WARNING - ${mgb}
701+
This element is automatically generated by MyBatis Generator, do not modify.
702+
@project https://github.com/itfsw/mybatis-generator-plugin
703+
-->
704+
]]></comment>
705+
706+
<!-- #############################################################################################################
707+
/**
708+
* This method is called to add a comment as the first child of the root element. This method could be used to add a
709+
* general file comment (such as a copyright notice). However, note that the XML file merge function does not deal
710+
* with this comment. If you run the generator repeatedly, you will only retain the comment from the initial run.
711+
* <p>
712+
*
713+
* The default implementation does nothing.
714+
*
715+
* @param rootElement
716+
* the root element
717+
*/
718+
-->
719+
<comment ID="addRootComment"></comment>
720+
721+
<!-- #############################################################################################################
722+
/**
723+
* This method should add a Javadoc comment to the specified field. The field is related to the specified table and
724+
* is used to hold the value of the specified column.
725+
* <p>
726+
*
727+
* <b>Important:</b> This method should add a the nonstandard JavaDoc tag "@mbg.generated" to the comment. Without
728+
* this tag, the Eclipse based Java merge feature will fail.
729+
*
730+
* @param field
731+
* the field
732+
* @param introspectedTable
733+
* the introspected table
734+
* @param introspectedColumn
735+
* the introspected column
736+
*/
737+
-->
738+
<comment ID="addFieldComment"><![CDATA[
739+
<#if introspectedColumn??>
740+
/**
741+
<#if introspectedColumn.remarks?? && introspectedColumn.remarks != ''>
742+
* Database Column Remarks:
743+
<#list introspectedColumn.remarks?split("\n") as remark>
744+
* ${remark}
745+
</#list>
746+
</#if>
747+
*
748+
* This field was generated by MyBatis Generator.
749+
* This field corresponds to the database column ${introspectedTable.fullyQualifiedTable}.${introspectedColumn.actualColumnName}
750+
*
751+
* ${mgb}
752+
* @project https://github.com/itfsw/mybatis-generator-plugin
753+
*/
754+
<#else>
755+
/**
756+
* This field was generated by MyBatis Generator.
757+
* This field corresponds to the database table ${introspectedTable.fullyQualifiedTable}
758+
*
759+
* ${mgb}
760+
* @project https://github.com/itfsw/mybatis-generator-plugin
761+
*/
762+
</#if>
763+
]]></comment>
764+
765+
<!-- #############################################################################################################
766+
/**
767+
* Adds a comment for a model class. The Java code merger should
768+
* be notified not to delete the entire class in case any manual
769+
* changes have been made. So this method will always use the
770+
* "do not delete" annotation.
771+
*
772+
* Because of difficulties with the Java file merger, the default implementation
773+
* of this method should NOT add comments. Comments should only be added if
774+
* specifically requested by the user (for example, by enabling table remark comments).
775+
*
776+
* @param topLevelClass
777+
* the top level class
778+
* @param introspectedTable
779+
* the introspected table
780+
*/
781+
-->
782+
<comment ID="addModelClassComment"><![CDATA[
783+
/**
784+
<#if introspectedTable.remarks?? && introspectedTable.remarks != ''>
785+
* Database Table Remarks:
786+
<#list introspectedTable.remarks?split("\n") as remark>
787+
* ${remark}
788+
</#list>
789+
</#if>
790+
*
791+
* This class was generated by MyBatis Generator.
792+
* This class corresponds to the database table ${introspectedTable.fullyQualifiedTable}
793+
*
794+
* ${mgb} do_not_delete_during_merge
795+
* @project https://github.com/itfsw/mybatis-generator-plugin
796+
*/
797+
]]></comment>
798+
799+
<!-- #############################################################################################################
800+
/**
801+
* Adds the inner class comment.
802+
*
803+
* @param innerClass
804+
* the inner class
805+
* @param introspectedTable
806+
* the introspected table
807+
* @param markAsDoNotDelete
808+
* the mark as do not delete
809+
*/
810+
-->
811+
<comment ID="addClassComment"><![CDATA[
812+
/**
813+
* This class was generated by MyBatis Generator.
814+
* This class corresponds to the database table ${introspectedTable.fullyQualifiedTable}
815+
*
816+
* ${mgb}<#if markAsDoNotDelete?? && markAsDoNotDelete> do_not_delete_during_merge</#if>
817+
* @project https://github.com/itfsw/mybatis-generator-plugin
818+
*/
819+
]]></comment>
820+
821+
<!-- #############################################################################################################
822+
/**
823+
* Adds the enum comment.
824+
*
825+
* @param innerEnum
826+
* the inner enum
827+
* @param introspectedTable
828+
* the introspected table
829+
*/
830+
-->
831+
<comment ID="addEnumComment"><![CDATA[
832+
/**
833+
* This enum was generated by MyBatis Generator.
834+
* This enum corresponds to the database table ${introspectedTable.fullyQualifiedTable}
835+
*
836+
* ${mgb}
837+
* @project https://github.com/itfsw/mybatis-generator-plugin
838+
*/
839+
]]></comment>
840+
841+
<!-- #############################################################################################################
842+
/**
843+
* Adds the interface comment.
844+
*
845+
* @param innerInterface
846+
* the inner interface
847+
* @param introspectedTable
848+
* the introspected table
849+
*/
850+
-->
851+
<comment ID="addInterfaceComment"><![CDATA[
852+
/**
853+
* This interface was generated by MyBatis Generator.
854+
* This interface corresponds to the database table ${introspectedTable.fullyQualifiedTable}
855+
*
856+
* ${mgb}
857+
* @project https://github.com/itfsw/mybatis-generator-plugin
858+
*/
859+
]]></comment>
860+
861+
<!-- #############################################################################################################
862+
/**
863+
* Adds the getter comment.
864+
*
865+
* @param method
866+
* the method
867+
* @param introspectedTable
868+
* the introspected table
869+
* @param introspectedColumn
870+
* the introspected column
871+
*/
872+
-->
873+
<comment ID="addGetterComment"><![CDATA[
874+
<#if introspectedColumn??>
875+
/**
876+
* This method was generated by MyBatis Generator.
877+
* This method returns the value of the database column ${introspectedTable.fullyQualifiedTable}.${introspectedColumn.actualColumnName}
878+
*
879+
* @return the value of ${introspectedTable.fullyQualifiedTable}.${introspectedColumn.actualColumnName}
880+
*
881+
* ${mgb}
882+
* @project https://github.com/itfsw/mybatis-generator-plugin
883+
*/
884+
<#else>
885+
/**
886+
* This method was generated by MyBatis Generator.
887+
* This method returns the value of the field ${method.name?replace("get","")?replace("is", "")?uncap_first}
888+
*
889+
* @return the value of field ${method.name?replace("get","")?replace("is", "")?uncap_first}
890+
*
891+
* ${mgb}
892+
* @project https://github.com/itfsw/mybatis-generator-plugin
893+
*/
894+
</#if>
895+
]]></comment>
896+
897+
<!-- #############################################################################################################
898+
/**
899+
* Adds the setter comment.
900+
*
901+
* @param method
902+
* the method
903+
* @param introspectedTable
904+
* the introspected table
905+
* @param introspectedColumn
906+
* the introspected column
907+
*/
908+
-->
909+
<comment ID="addSetterComment"><![CDATA[
910+
<#if introspectedColumn??>
911+
/**
912+
* This method was generated by MyBatis Generator.
913+
* This method sets the value of the database column ${introspectedTable.fullyQualifiedTable}.${introspectedColumn.actualColumnName}
914+
*
915+
* @param ${method.parameters[0].name} the value for ${introspectedTable.fullyQualifiedTable}.${introspectedColumn.actualColumnName}
916+
*
917+
* ${mgb}
918+
* @project https://github.com/itfsw/mybatis-generator-plugin
919+
*/
920+
<#else>
921+
/**
922+
* This method was generated by MyBatis Generator.
923+
* This method sets the value of the field ${method.name?replace("set","")?uncap_first}
924+
*
925+
* @param ${method.parameters[0].name} the value for field ${method.name?replace("set","")?uncap_first}
926+
*
927+
* ${mgb}
928+
* @project https://github.com/itfsw/mybatis-generator-plugin
929+
*/
930+
</#if>
931+
]]></comment>
932+
933+
<!-- #############################################################################################################
934+
/**
935+
* Adds the general method comment.
936+
*
937+
* @param method
938+
* the method
939+
* @param introspectedTable
940+
* the introspected table
941+
*/
942+
-->
943+
<comment ID="addGeneralMethodComment"><![CDATA[
944+
/**
945+
* This method was generated by MyBatis Generator.
946+
* This method corresponds to the database table ${introspectedTable.fullyQualifiedTable}
947+
*
948+
* ${mgb}
949+
* @project https://github.com/itfsw/mybatis-generator-plugin
950+
*/
951+
]]></comment>
952+
</template>
634953
```

0 commit comments

Comments
 (0)