2020import com .itfsw .mybatis .generator .plugins .utils .FormatTools ;
2121import com .itfsw .mybatis .generator .plugins .utils .IntrospectedTableTools ;
2222import com .itfsw .mybatis .generator .plugins .utils .JavaElementGeneratorTools ;
23+ import org .mybatis .generator .api .CommentGenerator ;
2324import org .mybatis .generator .api .IntrospectedColumn ;
2425import org .mybatis .generator .api .IntrospectedTable ;
2526import org .mybatis .generator .api .dom .java .*;
2627import org .mybatis .generator .internal .util .StringUtility ;
2728
29+ import java .math .BigDecimal ;
2830import java .util .ArrayList ;
2931import java .util .LinkedHashMap ;
3032import java .util .List ;
@@ -48,7 +50,7 @@ public class EnumTypeStatusPlugin extends BasePlugin {
4850 public final static String REMARKS_PATTERN = ".*\\ s*\\ [\\ s*(\\ w+\\ s*\\ (\\ s*[\\ u4e00-\\ u9fa5_a-zA-Z0-9]+\\ s*\\ )\\ s*:\\ s*[\\ u4e00-\\ u9fa5_a-zA-Z0-9]+\\ s*\\ ,?\\ s*)+\\ s*\\ ]\\ s*.*" ;
4951 public final static String NEED_PATTERN = "\\ [\\ s*((\\ w+\\ s*\\ (\\ s*[\\ u4e00-\\ u9fa5_a-zA-Z0-9]+\\ s*\\ )\\ s*:\\ s*[\\ u4e00-\\ u9fa5_a-zA-Z0-9]+\\ s*\\ ,?\\ s*)+)\\ s*\\ ]" ;
5052 public final static String ITEM_PATTERN = "(\\ w+)\\ s*\\ (\\ s*([\\ u4e00-\\ u9fa5_a-zA-Z0-9]+)\\ s*\\ )\\ s*:\\ s*([\\ u4e00-\\ u9fa5_a-zA-Z0-9]+)" ;
51- private Map <IntrospectedColumn , List < EnumItemInfo > > enumColumns ;
53+ private Map <String , EnumInfo > enumColumns ;
5254
5355 /**
5456 * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
@@ -83,30 +85,35 @@ public void initialized(IntrospectedTable introspectedTable) {
8385 } else if (!(Short .class .getTypeName ().equals (javaType )
8486 || Integer .class .getTypeName ().equals (javaType )
8587 || Long .class .getTypeName ().equals (javaType )
88+ || Boolean .class .getTypeName ().equals (javaType )
89+ || Double .class .getTypeName ().equals (javaType )
90+ || Float .class .getTypeName ().equals (javaType )
91+ || BigDecimal .class .getTypeName ().equals (javaType )
92+ || Byte .class .getTypeName ().equals (javaType )
8693 || String .class .getTypeName ().equals (javaType ))) {
87- warnings .add ("itfsw:插件" + EnumTypeStatusPlugin .class .getTypeName () + "找到column为" + enumColumnsStr .trim () + "对应Java类型不是Short、Integer、Long、String !" );
94+ warnings .add ("itfsw:插件" + EnumTypeStatusPlugin .class .getTypeName () + "找到column为" + enumColumnsStr .trim () + "对应Java类型不在支持范围内 !" );
8895 } else {
8996 // 提取信息
9097 Pattern pattern = Pattern .compile (NEED_PATTERN );
9198 Matcher matcher = pattern .matcher (remarks );
92- List < EnumItemInfo > itemInfos = new ArrayList <>( );
99+ EnumInfo enumInfo = new EnumInfo ( column );
93100 if (matcher .find () && matcher .groupCount () == 2 ) {
94101 String enumInfoStr = matcher .group (1 );
95102 // 根据逗号切分
96103 String [] enumInfoStrs = enumInfoStr .split ("," );
97104
98105 // 提取每个节点信息
99- for (String enumInfo : enumInfoStrs ) {
106+ for (String enumInfoItemStr : enumInfoStrs ) {
100107 pattern = Pattern .compile (ITEM_PATTERN );
101- matcher = pattern .matcher (enumInfo .trim ());
108+ matcher = pattern .matcher (enumInfoItemStr .trim ());
102109 if (matcher .find () && matcher .groupCount () == 3 ) {
103- itemInfos . add ( new EnumItemInfo ( column , matcher .group (1 ), matcher .group (3 ), matcher .group (2 ) ));
110+ enumInfo . addItem ( matcher .group (1 ), matcher .group (3 ), matcher .group (2 ));
104111 }
105112 }
106113 }
107114
108- if (itemInfos . size () > 0 ) {
109- this .enumColumns .put (column , itemInfos );
115+ if (enumInfo . hasItems () ) {
116+ this .enumColumns .put (column . getJavaProperty (), enumInfo );
110117 }
111118 }
112119 }
@@ -132,63 +139,13 @@ public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass, Intros
132139 * @param introspectedTable
133140 */
134141 private void generateModelEnum (TopLevelClass topLevelClass , IntrospectedTable introspectedTable ) {
135- this .enumColumns .forEach ((introspectedColumn , enumItemInfos ) -> {
136- // 枚举跟随字段走
137- for (Field field : topLevelClass .getFields ()) {
138- if (introspectedColumn .getJavaProperty ().equals (field .getName ())) {
139- String enumName = this .upperCaseFirst (field .getName ());
140-
141- InnerEnum innerEnum = new InnerEnum (new FullyQualifiedJavaType (enumName ));
142- commentGenerator .addEnumComment (innerEnum , introspectedTable );
143- innerEnum .setVisibility (JavaVisibility .PUBLIC );
144- innerEnum .setStatic (true );
145-
146- // 生成枚举
147- for (EnumItemInfo item : enumItemInfos ) {
148- innerEnum .addEnumConstant (item .getName () + "(" + item .getValue () + ", " + item .getComment () + ")" );
149- }
150-
151- // 生成属性和构造函数
152- Field fValue = new Field ("value" , field .getType ());
153- fValue .setVisibility (JavaVisibility .PRIVATE );
154- fValue .setFinal (true );
155- commentGenerator .addFieldComment (fValue , introspectedTable );
156- innerEnum .addField (fValue );
157-
158- Field fName = new Field ("name" , FullyQualifiedJavaType .getStringInstance ());
159- fName .setVisibility (JavaVisibility .PRIVATE );
160- fName .setFinal (true );
161- commentGenerator .addFieldComment (fName , introspectedTable );
162- innerEnum .addField (fName );
163-
164- Method mInc = new Method (enumName );
165- mInc .setConstructor (true );
166- mInc .addBodyLine ("this.value = value;" );
167- mInc .addBodyLine ("this.name = name;" );
168- mInc .addParameter (new Parameter (fValue .getType (), "value" ));
169- mInc .addParameter (new Parameter (fName .getType (), "name" ));
170- commentGenerator .addGeneralMethodComment (mInc , introspectedTable );
171- FormatTools .addMethodWithBestPosition (innerEnum , mInc );
172-
173- // 获取value的方法
174- Method mValue = JavaElementGeneratorTools .generateGetterMethod (fValue );
175- commentGenerator .addGeneralMethodComment (mValue , introspectedTable );
176- FormatTools .addMethodWithBestPosition (innerEnum , mValue );
177-
178- Method mValue1 = JavaElementGeneratorTools .generateGetterMethod (fValue );
179- mValue1 .setName ("value" );
180- commentGenerator .addGeneralMethodComment (mValue1 , introspectedTable );
181- FormatTools .addMethodWithBestPosition (innerEnum , mValue1 );
182-
183- // 获取name的方法
184- Method mName = JavaElementGeneratorTools .generateGetterMethod (fName );
185- commentGenerator .addGeneralMethodComment (mName , introspectedTable );
186- FormatTools .addMethodWithBestPosition (innerEnum , mName );
187-
188- topLevelClass .addInnerEnum (innerEnum );
189- }
142+ // 枚举跟随字段走
143+ for (Field field : topLevelClass .getFields ()) {
144+ if (this .enumColumns .get (field .getName ()) != null ) {
145+ InnerEnum innerEnum = this .enumColumns .get (field .getName ()).generateEnum (commentGenerator , introspectedTable );
146+ topLevelClass .addInnerEnum (innerEnum );
190147 }
191- });
148+ }
192149 }
193150
194151 /**
@@ -204,66 +161,131 @@ public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, Intros
204161 return super .modelBaseRecordClassGenerated (topLevelClass , introspectedTable );
205162 }
206163
207- /**
208- * 首字母大写
209- * @param s
210- * @return
211- */
212- private String upperCaseFirst (String s ) {
213- if (Character .isUpperCase (s .charAt (0 ))) {
214- return s ;
215- } else {
216- return (new StringBuilder ()).append (Character .toUpperCase (s .charAt (0 ))).append (s .substring (1 )).toString ();
217- }
218- }
219164
220-
221- private class EnumItemInfo {
165+ public static class EnumInfo {
166+ private List < EnumItemInfo > items = new ArrayList <>();
222167 private IntrospectedColumn column ;
223- private String name ;
224- private String comment ;
225- private String value ;
226168
227- public EnumItemInfo (IntrospectedColumn column , String name , String comment , String value ) {
169+ public EnumInfo (IntrospectedColumn column ) {
228170 this .column = column ;
229- this .name = name ;
230- this .comment = comment ;
231- this .value = value ;
232171 }
233172
234173 /**
235- * Getter method for property <tt>comment</tt>.
236- * @return property value of comment
237- * @author hewei
174+ * 添加Enum Item
175+ * @param name
176+ * @param comment
177+ * @param value
238178 */
239- public String getComment ( ) {
240- return " \" " + comment . trim () + " \" " ;
179+ public void addItem ( String name , String comment , String value ) {
180+ items . add ( new EnumItemInfo ( this . column , name , comment , value )) ;
241181 }
242182
243183 /**
244- * Getter method for property <tt>name</tt>.
245- * @return property value of name
246- * @author hewei
184+ * 判断是否有节点
185+ * @return
247186 */
248- public String getName () {
249- return name . trim (). toUpperCase () ;
187+ public boolean hasItems () {
188+ return items . size () > 0 ;
250189 }
251190
252- /**
253- * Getter method for property <tt>value</tt>.
254- * @return property value of value
255- * @author hewei
256- */
257- public String getValue () {
258- String javaType = this .column .getFullyQualifiedJavaType ().getFullyQualifiedName ();
259- if (Short .class .getTypeName ().equals (javaType )) {
260- return "(short)" + value ;
261- } else if (Long .class .getTypeName ().equals (javaType )) {
262- return value + "L" ;
263- } else if (String .class .getTypeName ().equals (javaType )) {
264- return "\" " + value + "\" " ;
191+ public InnerEnum generateEnum (CommentGenerator commentGenerator , IntrospectedTable introspectedTable ) {
192+ String enumName = FormatTools .upFirstChar (column .getJavaProperty ());
193+
194+ InnerEnum innerEnum = new InnerEnum (new FullyQualifiedJavaType (enumName ));
195+ commentGenerator .addEnumComment (innerEnum , introspectedTable );
196+ innerEnum .setVisibility (JavaVisibility .PUBLIC );
197+ innerEnum .setStatic (true );
198+
199+ // 生成枚举
200+ for (EnumItemInfo item : this .items ) {
201+ innerEnum .addEnumConstant (item .getName () + "(" + item .getValue () + ", " + item .getComment () + ")" );
202+ }
203+
204+ // 生成属性和构造函数
205+ Field fValue = new Field ("value" , column .getFullyQualifiedJavaType ());
206+ fValue .setVisibility (JavaVisibility .PRIVATE );
207+ fValue .setFinal (true );
208+ commentGenerator .addFieldComment (fValue , introspectedTable );
209+ innerEnum .addField (fValue );
210+
211+ Field fName = new Field ("name" , FullyQualifiedJavaType .getStringInstance ());
212+ fName .setVisibility (JavaVisibility .PRIVATE );
213+ fName .setFinal (true );
214+ commentGenerator .addFieldComment (fName , introspectedTable );
215+ innerEnum .addField (fName );
216+
217+ Method mInc = new Method (enumName );
218+ mInc .setConstructor (true );
219+ mInc .addBodyLine ("this.value = value;" );
220+ mInc .addBodyLine ("this.name = name;" );
221+ mInc .addParameter (new Parameter (fValue .getType (), "value" ));
222+ mInc .addParameter (new Parameter (fName .getType (), "name" ));
223+ commentGenerator .addGeneralMethodComment (mInc , introspectedTable );
224+ FormatTools .addMethodWithBestPosition (innerEnum , mInc );
225+
226+ // 获取value的方法
227+ Method mValue = JavaElementGeneratorTools .generateGetterMethod (fValue );
228+ commentGenerator .addGeneralMethodComment (mValue , introspectedTable );
229+ FormatTools .addMethodWithBestPosition (innerEnum , mValue );
230+
231+ Method mValue1 = JavaElementGeneratorTools .generateGetterMethod (fValue );
232+ mValue1 .setName ("value" );
233+ commentGenerator .addGeneralMethodComment (mValue1 , introspectedTable );
234+ FormatTools .addMethodWithBestPosition (innerEnum , mValue1 );
235+
236+ // 获取name的方法
237+ Method mName = JavaElementGeneratorTools .generateGetterMethod (fName );
238+ commentGenerator .addGeneralMethodComment (mName , introspectedTable );
239+ FormatTools .addMethodWithBestPosition (innerEnum , mName );
240+
241+ return innerEnum ;
242+ }
243+
244+
245+ private class EnumItemInfo {
246+ private IntrospectedColumn column ;
247+ private String name ;
248+ private String comment ;
249+ private String value ;
250+
251+ public EnumItemInfo (IntrospectedColumn column , String name , String comment , String value ) {
252+ this .column = column ;
253+ this .name = name .trim ();
254+ this .comment = comment .trim ();
255+ this .value = value .trim ();
256+ }
257+
258+ /**
259+ * Getter method for property <tt>comment</tt>.
260+ * @return property value of comment
261+ * @author hewei
262+ */
263+ public String getComment () {
264+ return "\" " + comment + "\" " ;
265+ }
266+
267+ /**
268+ * Getter method for property <tt>name</tt>.
269+ * @return property value of name
270+ * @author hewei
271+ */
272+ public String getName () {
273+ return name .toUpperCase ();
274+ }
275+
276+ /**
277+ * Getter method for property <tt>value</tt>.
278+ * @return property value of value
279+ * @author hewei
280+ */
281+ public String getValue () {
282+ String javaType = this .column .getFullyQualifiedJavaType ().getShortName ();
283+ if ("NULL" .equalsIgnoreCase (value )) {
284+ return "null" ;
285+ } else {
286+ return "new " + javaType + "(\" " + value + "\" )" ;
287+ }
265288 }
266- return value ;
267289 }
268290 }
269291}
0 commit comments