1+ import  com .google .common .base .CaseFormat ;
12import  freemarker .template .TemplateExceptionHandler ;
3+ import  org .apache .commons .lang3 .StringUtils ;
24import  org .mybatis .generator .api .MyBatisGenerator ;
35import  org .mybatis .generator .config .*;
46import  org .mybatis .generator .internal .DefaultShellCallback ;
@@ -36,18 +38,34 @@ public class CodeGenerator {
3638
3739    public  static  void  main (String [] args ) {
3840        genCode ("输入表名" );
41+         //genCode("输入表名","输入自定义Model名称"); 
3942    }
4043
44+     /** 
45+      * 通过数据表名称生成代码,Model 名称通过解析数据表名称获得,下划线转大驼峰的形式。 
46+      * 如输入表名称 "t_user_detail" 将生成 TUserDetail、TUserDetailMapper、TUserDetailService ... 
47+      * @param tableNames 数据表名称... 
48+      */ 
4149    public  static  void  genCode (String ... tableNames ) {
4250        for  (String  tableName  : tableNames ) {
43-             //根据需求生成,不需要的注掉,模板有问题的话可以自己修改。 
44-             genModelAndMapper (tableName );
45-             genService (tableName );
46-             genController (tableName );
51+             genCode (tableName , null );
4752        }
4853    }
4954
50-     public  static  void  genModelAndMapper (String  tableName ) {
55+     /** 
56+      * 通过数据表名称,和自定义的 Model 名称生成代码 
57+      * 如输入表名称 "t_user_detail" 和自定义的 Model 名称 "User" 将生成 User、UserMapper、UserService ... 
58+      * @param tableName 数据表名称 
59+      * @param modelName 自定义的 Model 名称 
60+      */ 
61+     public  static  void  genCode (String  tableName , String  modelName ) {
62+         genModelAndMapper (tableName , modelName );
63+         genService (tableName , modelName );
64+         genController (tableName , modelName );
65+     }
66+ 
67+ 
68+     public  static  void  genModelAndMapper (String  tableName , String  modelName ) {
5169        Context  context  = new  Context (ModelType .FLAT );
5270        context .setId ("Potato" );
5371        context .setTargetRuntime ("MyBatis3Simple" );
@@ -84,6 +102,7 @@ public static void genModelAndMapper(String tableName) {
84102
85103        TableConfiguration  tableConfiguration  = new  TableConfiguration (context );
86104        tableConfiguration .setTableName (tableName );
105+         tableConfiguration .setDomainObjectName (modelName );
87106        tableConfiguration .setGeneratedKey (new  GeneratedKey ("id" , "Mysql" , true , null ));
88107        context .addTableConfiguration (tableConfiguration );
89108
@@ -106,21 +125,20 @@ public static void genModelAndMapper(String tableName) {
106125        if  (generator .getGeneratedJavaFiles ().isEmpty () || generator .getGeneratedXmlFiles ().isEmpty ()) {
107126            throw  new  RuntimeException ("生成Model和Mapper失败:"  + warnings );
108127        }
109- 
110-         String  modelName  = tableNameConvertUpperCamel (tableName );
128+         if  (StringUtils .isEmpty (modelName )) modelName  = tableNameConvertUpperCamel (tableName );
111129        System .out .println (modelName  + ".java 生成成功" );
112130        System .out .println (modelName  + "Mapper.java 生成成功" );
113131        System .out .println (modelName  + "Mapper.xml 生成成功" );
114132    }
115133
116-     public  static  void  genService (String  tableName ) {
134+     public  static  void  genService (String  tableName ,  String   modelName ) {
117135        try  {
118136            freemarker .template .Configuration  cfg  = getConfiguration ();
119137
120138            Map <String , Object > data  = new  HashMap <>();
121139            data .put ("date" , DATE );
122140            data .put ("author" , AUTHOR );
123-             String  modelNameUpperCamel  = tableNameConvertUpperCamel (tableName );
141+             String  modelNameUpperCamel  = StringUtils . isEmpty ( modelName ) ?  tableNameConvertUpperCamel (tableName ) :  modelName ;
124142            data .put ("modelNameUpperCamel" , modelNameUpperCamel );
125143            data .put ("modelNameLowerCamel" , tableNameConvertLowerCamel (tableName ));
126144            data .put ("basePackage" , BASE_PACKAGE );
@@ -145,17 +163,17 @@ public static void genService(String tableName) {
145163        }
146164    }
147165
148-     public  static  void  genController (String  tableName ) {
166+     public  static  void  genController (String  tableName ,  String   modelName ) {
149167        try  {
150168            freemarker .template .Configuration  cfg  = getConfiguration ();
151169
152170            Map <String , Object > data  = new  HashMap <>();
153171            data .put ("date" , DATE );
154172            data .put ("author" , AUTHOR );
155-             data . put ( "baseRequestMapping" ,  tableNameConvertMappingPath (tableName )) ;
156-             String   modelNameUpperCamel  =  tableNameConvertUpperCamel ( tableName );
173+             String   modelNameUpperCamel  =  StringUtils . isEmpty ( modelName ) ?  tableNameConvertUpperCamel (tableName ) :  modelName ;
174+             data . put ( "baseRequestMapping" ,  modelNameConvertMappingPath ( modelNameUpperCamel ) );
157175            data .put ("modelNameUpperCamel" , modelNameUpperCamel );
158-             data .put ("modelNameLowerCamel" , tableNameConvertLowerCamel ( tableName ));
176+             data .put ("modelNameLowerCamel" , CaseFormat . UPPER_CAMEL . to ( CaseFormat . LOWER_CAMEL ,  modelNameUpperCamel ));
159177            data .put ("basePackage" , BASE_PACKAGE );
160178
161179            File  file  = new  File (PROJECT_PATH  + JAVA_PATH  + PACKAGE_PATH_CONTROLLER  + modelNameUpperCamel  + "Controller.java" );
@@ -181,30 +199,11 @@ private static freemarker.template.Configuration getConfiguration() throws IOExc
181199    }
182200
183201    private  static  String  tableNameConvertLowerCamel (String  tableName ) {
184-         StringBuilder  result  = new  StringBuilder ();
185-         if  (tableName  != null  && tableName .length () > 0 ) {
186-             tableName  = tableName .toLowerCase ();//兼容使用大写的表名 
187-             boolean  flag  = false ;
188-             for  (int  i  = 0 ; i  < tableName .length (); i ++) {
189-                 char  ch  = tableName .charAt (i );
190-                 if  ("_" .charAt (0 ) == ch ) {
191-                     flag  = true ;
192-                 } else  {
193-                     if  (flag ) {
194-                         result .append (Character .toUpperCase (ch ));
195-                         flag  = false ;
196-                     } else  {
197-                         result .append (ch );
198-                     }
199-                 }
200-             }
201-         }
202-         return  result .toString ();
202+         return  CaseFormat .LOWER_UNDERSCORE .to (CaseFormat .LOWER_CAMEL , tableName .toLowerCase ());
203203    }
204204
205205    private  static  String  tableNameConvertUpperCamel (String  tableName ) {
206-         String  camel  = tableNameConvertLowerCamel (tableName );
207-         return  camel .substring (0 , 1 ).toUpperCase () + camel .substring (1 );
206+         return  CaseFormat .LOWER_UNDERSCORE .to (CaseFormat .UPPER_CAMEL , tableName .toLowerCase ());
208207
209208    }
210209
@@ -213,7 +212,13 @@ private static String tableNameConvertMappingPath(String tableName) {
213212        return  "/"  + (tableName .contains ("_" ) ? tableName .replaceAll ("_" , "/" ) : tableName );
214213    }
215214
215+     private  static  String  modelNameConvertMappingPath (String  modelName ) {
216+         String  tableName  = CaseFormat .UPPER_CAMEL .to (CaseFormat .LOWER_UNDERSCORE , modelName );
217+         return  tableNameConvertMappingPath (tableName );
218+     }
219+ 
216220    private  static  String  packageConvertPath (String  packageName ) {
217221        return  String .format ("/%s/" , packageName .contains ("." ) ? packageName .replaceAll ("\\ ." , "/" ) : packageName );
218222    }
223+ 
219224}
0 commit comments