沫沫金-Mybatis工具类,生成dao层xml、mapper文件和实体类entity层

简介:

Mybatis Generator工具使用起来,总感觉不太灵活加上初次环境配置麻烦,特编写java文件 单文件不依赖,直接生成。源码如下(此为Oracle数据库版)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
package  net.icarefx.booking.util;
 
import  java.io.BufferedWriter;
import  java.io.File;
import  java.io.FileOutputStream;
import  java.io.IOException;
import  java.io.OutputStreamWriter;
import  java.sql.Connection;
import  java.sql.DriverManager;
import  java.sql.PreparedStatement;
import  java.sql.ResultSet;
import  java.sql.SQLException;
import  java.util.ArrayList;
import  java.util.List;
 
import  org.apache.commons.lang.StringUtils;
 
/**
  * 代码生成MyBatis的实体类、dao层下实体映射(sqlmap)XML文件和Mapper类
  * @author 沫沫金
  * @email zl0828@yeah.net
  * @WeiXin zl4828
  */
public  class  MybatisGenerator {
     /**
      **********************************使用前必读*******************
      **
      ** 使用前请将moduleName更改为自己数据库表名,全库生成请留空。这是Oracle版本
      **
      ***********************************************************
      */
  
     private  final  String type_str =  "VARCHAR2" ;
     private  final  String type_date =  "DATE" ;
     private  final  String type_decimal =  "NUMBER" ;
  
     private  final  String moduleName =  "ORDERWEB_BED_REC" // 表名称(所有请留空,指定表名只生成单表mapper等)
     
     private  static  String fileName =  "MybatisGenerator" // 生成文件存储根目录文件夹名
     private  final  static  String BASE_PATH =  "D:/" +fileName+ "/" ;
     private  final  static  String OPEN_PATH =  "D:\\" +fileName;
     private  final  String bean_path = BASE_PATH +  "entity" ;
     private  final  String mapper_path = BASE_PATH +  "dao" ;
     private  final  String xml_path = BASE_PATH +  "dao/sqlmap" ;
     private  final  String bean_package =  "net.icarefx.booking.pojo" ;
     private  final  String mapper_package =  "net.icarefx.booking.dao" ;
     /**
      * 数据库配置
      */
     private  final  String driverName =  "oracle.jdbc.driver.OracleDriver" ;
     private  final  String url =  "jdbc:oracle:thin:@127.0.0.1:1521:orcl" ;
     private  final  String user =  "orderweb" ;
     private  final  String password =  "carefx" ;
  
     private  String tableName =  null ;
     private  String beanName =  null ;
     private  String mapperName =  null ;
     private  Connection conn =  null ;
  
  
     private  void  init()  throws  ClassNotFoundException, SQLException {
         Class.forName(driverName);
         conn = DriverManager.getConnection(url, user, password);
     }
  
  
     /**
      *  获取所有的表
      *
      * @return
      * @throws SQLException 
      */
     private  List<String> getTables()  throws  SQLException {
         List<String> tables =  new  ArrayList<String>();
         PreparedStatement pstate =  null ;
         if (StringUtils.isNotBlank(moduleName)){
             pstate = conn.prepareStatement( "select table_name from user_tables where table_name ='" +moduleName+ "'" );
         } else {
             pstate = conn.prepareStatement( "select table_name from user_tables" );
         }
         ResultSet results = pstate.executeQuery();
         while  ( results.next() ) {
             String tableName = results.getString( 1 );
             tables.add(tableName);
         }
         return  tables;
     }
  
  
     private  void  processTable( String table ) {
         StringBuffer sb =  new  StringBuffer(table.length());
         String tableNew = table.toLowerCase();
         String[] tables = tableNew.split( "_" );
         String temp =  null ;
         if (tables.length> 1 ){
             for  int  i =  0  ; i < tables.length ; i++ ) {
                 temp = tables[i].trim();
                 sb.append(temp.substring( 0 1 ).toUpperCase()).append(temp.substring( 1 ));
             }
         } else {
             sb.append(tableNew.substring( 0 1 ).toUpperCase()).append(tableNew.substring( 1 ));
         }
         
         beanName = sb.toString();
         mapperName = beanName +  "Mapper" ;
     }
  
  
     private  String processType( String type ) {
         if  ( type.indexOf(type_str) > - 1  ) {
             return  "String" ;
         } else  if  ( type.indexOf(type_date) > - 1  ) {
             return  "java.util.Date" ;
         } else  if  ( type.indexOf(type_decimal) > - 1  ) {
             return  "java.math.BigDecimal" ;
         }
         return  null ;
     }
  
  
     private  String processField( String field ) {
         StringBuffer sb =  new  StringBuffer(field.length());
         field = field.toLowerCase();
         String[] fields = field.split( "_" );
         String temp =  null ;
         sb.append(fields[ 0 ]);
         if (fields.length> 1 ){
             for  int  i =  1  ; i < fields.length ; i++ ) {
                 temp = fields[i].trim();
                 sb.append(temp.substring( 0 1 ).toUpperCase()).append(temp.substring( 1 ));
             }
         }
         return  sb.toString();
     }
  
  
     /**
      *  将实体类名首字母改为小写
      *
      * @param beanName
      * @return 
      */
     private  String processResultMapId( String beanName ) {
         return  beanName.substring( 0 1 ).toLowerCase() + beanName.substring( 1 );
     }
  
  
     /**
      *  构建类上面的注释
      *
      * @param bw
      * @param text
      * @return
      * @throws IOException 
      */
     private  BufferedWriter buildClassComment( BufferedWriter bw, String text )  throws  IOException {
         bw.newLine();
         bw.newLine();
         bw.write( "/**" );
         bw.newLine();
         bw.write( " * " );
         bw.newLine();
         bw.write( " * "  + text);
         bw.newLine();
         bw.write( " * " );
         bw.newLine();
         bw.write( " **/" );
         return  bw;
     }
  
  
     /**
      *  构建方法上面的注释
      *
      * @param bw
      * @param text
      * @return
      * @throws IOException 
      */
     private  BufferedWriter buildMethodComment( BufferedWriter bw, String text )  throws  IOException {
         bw.newLine();
         bw.write( "\t/**" );
         bw.newLine();
         bw.write( "\t * " );
         bw.newLine();
         bw.write( "\t * "  + text);
         bw.newLine();
         bw.write( "\t * " );
         bw.newLine();
         bw.write( "\t **/" );
         return  bw;
     }
  
  
     /**
      *  生成实体类
      *
      * @param columns
      * @param types
      * @param comments
      * @throws IOException 
      */
     private  void  buildEntityBean( List<String> columns, List<String> types, List<String> comments, String tableComment )
         throws  IOException {
         File folder =  new  File(bean_path);
         if  ( !folder.exists() ) {
             folder.mkdir();
         }
  
         File beanFile =  new  File(bean_path, beanName +  ".java" );
         BufferedWriter bw =  new  BufferedWriter( new  OutputStreamWriter( new  FileOutputStream(beanFile)));
         bw.write( "package "  + bean_package +  ";" );
         bw.newLine();
         bw.write( "import java.io.Serializable;" );
         bw.newLine();
         //bw.write("import lombok.Data;");
         //      bw.write("import javax.persistence.Entity;");
         bw = buildClassComment(bw, tableComment);
         bw.newLine();
         bw.write( "@SuppressWarnings(\"serial\")" );
         bw.newLine();
         //      bw.write("@Entity");
         //bw.write("@Data");
         //bw.newLine();
         bw.write( "public class "  + beanName +  " implements Serializable {" );
         bw.newLine();
         bw.newLine();
         int  size = columns.size();
         for  int  i =  0  ; i < size ; i++ ) {
             bw.write( "\t/**"  + comments.get(i) +  "**/" );
             bw.newLine();
             bw.write( "\tprivate "  + processType(types.get(i)) +  " "  + processField(columns.get(i)) +  ";" );
             bw.newLine();
             bw.newLine();
         }
         bw.newLine();
         // 生成get 和 set方法
         String tempField =  null ;
         String _tempField =  null ;
         String tempType =  null ;
         for  int  i =  0  ; i < size ; i++ ) {
             tempType = processType(types.get(i));
             _tempField = processField(columns.get(i));
             tempField = _tempField.substring( 0 1 ).toUpperCase() + _tempField.substring( 1 );
             bw.newLine();
             //          bw.write("\tpublic void set" + tempField + "(" + tempType + " _" + _tempField + "){");
             bw.write( "\tpublic void set"  + tempField +  "("  + tempType +  " "  + _tempField +  "){" );
             bw.newLine();
             //          bw.write("\t\tthis." + _tempField + "=_" + _tempField + ";");
             bw.write( "\t\tthis."  + _tempField +  " = "  + _tempField +  ";" );
             bw.newLine();
             bw.write( "\t}" );
             bw.newLine();
             bw.newLine();
             bw.write( "\tpublic "  + tempType +  " get"  + tempField +  "(){" );
             bw.newLine();
             bw.write( "\t\treturn this."  + _tempField +  ";" );
             bw.newLine();
             bw.write( "\t}" );
             bw.newLine();
         }
         bw.newLine();
         bw.write( "}" );
         bw.newLine();
         bw.flush();
         bw.close();
     }
  
  
     /**
      *  构建Mapper文件
      *
      * @throws IOException 
      */
     private  void  buildMapper(List<String> columns, List<String> types)  throws  IOException {
         File folder =  new  File(mapper_path);
         if  ( !folder.exists() ) {
             folder.mkdirs();
         }
  
         File mapperFile =  new  File(mapper_path, mapperName +  ".java" );
         BufferedWriter bw =  new  BufferedWriter( new  OutputStreamWriter( new  FileOutputStream(mapperFile),  "utf-8" ));
         bw.write( "package "  + mapper_package +  ";" );
         bw.newLine();
         bw.newLine();
         bw.write( "import "  + bean_package +  "."  + beanName +  ";" );
         bw.newLine();
         bw.write( "import org.apache.ibatis.annotations.Param;" );
         bw.newLine();
         bw.write( "import java.util.List;" );
         bw = buildClassComment(bw, mapperName +  "数据库操作接口类" );
         bw.newLine();
         bw.newLine();
         //      bw.write("public interface " + mapperName + " extends " + mapper_extends + "<" + beanName + "> {");
         bw.write( "public interface "  + mapperName +  "{" );
         bw.newLine();
         bw.newLine();
         // 主键名称
         String pkName = processField(columns.get( 0 ));
         String pkType = processType(types.get( 0 ));
         // ----------定义Mapper中的方法Begin----------
         bw = buildMethodComment(bw,  "查询(根据主键ID查询)" );
         bw.newLine();
         bw.write( "\tpublic "  + beanName +  "  selectByPrimaryKey ( @Param(\"" +pkName+ "\") " +pkType+ " " +pkName+ " );" );
         bw.newLine();
         bw = buildMethodComment(bw,  "删除(根据主键ID删除)" );
         bw.newLine();
         bw.write( "\tpublic "  "int deleteByPrimaryKey ( @Param(\"" +pkName+ "\") " +pkType+ " " +pkName+ " );" );
         bw.newLine();
         bw = buildMethodComment(bw,  "添加" );
         bw.newLine();
         bw.write( "\tpublic "  "int insert( "  + beanName +  " record );" );
         bw.newLine();
         bw = buildMethodComment(bw,  "添加 (匹配有值的字段)" );
         bw.newLine();
         bw.write( "\tpublic "  "int insertSelective( "  + beanName +  " record );" );
         bw.newLine();
         bw = buildMethodComment(bw,  "修改 (匹配有值的字段)" );
         bw.newLine();
         bw.write( "\tpublic "  "int updateByPrimaryKeySelective( "  + beanName +  " record );" );
         bw.newLine();
         bw = buildMethodComment(bw,  "修改(根据主键ID修改)" );
         bw.newLine();
         bw.write( "\tpublic "  "int updateByPrimaryKey ( "  + beanName +  " record );" );
         bw.newLine();
         bw = buildMethodComment(bw,  "集合(匹配有值的字段)" );
         bw.newLine();
         bw.write( "\tpublic List<"  + beanName +  ">  findByList ( "  + beanName +  " record );" );
         bw.newLine();
  
         // ----------定义Mapper中的方法End----------
         bw.newLine();
         bw.write( "}" );
         bw.flush();
         bw.close();
     }
  
  
     /**
      *  构建实体类映射XML文件
      *
      * @param columns
      * @param types
      * @param comments
      * @throws IOException 
      */
     private  void  buildMapperXml( List<String> columns, List<String> types, List<String> comments )  throws  IOException {
         File folder =  new  File(xml_path);
         if  ( !folder.exists() ) {
             folder.mkdirs();
         }
  
         File mapperXmlFile =  new  File(xml_path, beanName +  ".sqlmap.xml" );
         BufferedWriter bw =  new  BufferedWriter( new  OutputStreamWriter( new  FileOutputStream(mapperXmlFile)));
         bw.write( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" );
         bw.newLine();
         bw.write( "<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" " );
         bw.newLine();
         bw.write( "    \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">" );
         bw.newLine();
         bw.write( "<mapper namespace=\""  + mapper_package +  "."  + mapperName +  "\">" );
         bw.newLine();
         bw.newLine();
 
         buildSQL(bw, columns, types);
  
         bw.write( "</mapper>" );
         bw.flush();
         bw.close();
     }
  
  
     private  void  buildSQL( BufferedWriter bw, List<String> columns, List<String> types )  throws  IOException {
         int  size = columns.size();
         // 通用结果列
         bw.write( "\t<!-- 通用查询结果列-->" );
         bw.newLine();
         bw.write( "\t<sql id=\"Base_Column_List\">" );
         bw.newLine();
  
         for  int  i =  0  ; i < size ; i++ ) {
             bw.write( "\t"  + columns.get(i));
             if  ( i != size -  1  ) {
                 bw.write( "," );
             }
         }
  
         bw.newLine();
         bw.write( "\t</sql>" );
         bw.newLine();
         bw.newLine();
  
  
         // 查询(根据主键ID查询)
         bw.write( "\t<!-- 查询(根据主键ID查询) -->" );
         bw.newLine();
         bw.write( "\t<select id=\"selectByPrimaryKey\" resultType=\""
                 + processResultMapId(beanName) +  "\" parameterType=\"java.lang."  + processType(types.get( 0 )) +  "\">" );
         bw.newLine();
         bw.write( "\t\t SELECT" );
         bw.newLine();
         bw.write( "\t\t <include refid=\"Base_Column_List\" />" );
         bw.newLine();
         bw.write( "\t\t FROM "  + tableName);
         bw.newLine();
         bw.write( "\t\t WHERE "  + columns.get( 0 ) +  " = #{"  + processField(columns.get( 0 )) +  "}" );
         bw.newLine();
         bw.write( "\t</select>" );
         bw.newLine();
         bw.newLine();
         // 查询完
  
  
         // 删除(根据主键ID删除)
         bw.write( "\t<!--删除:根据主键ID删除-->" );
         bw.newLine();
         bw.write( "\t<delete id=\"deleteByPrimaryKey\" parameterType=\"java.lang."  + processType(types.get( 0 )) +  "\">" );
         bw.newLine();
         bw.write( "\t\t DELETE FROM "  + tableName);
         bw.newLine();
         bw.write( "\t\t WHERE "  + columns.get( 0 ) +  " = #{"  + processField(columns.get( 0 )) +  "}" );
         bw.newLine();
         bw.write( "\t</delete>" );
         bw.newLine();
         bw.newLine();
         // 删除完
  
  
         // 添加insert方法
         bw.write( "\t<!-- 添加 -->" );
         bw.newLine();
         bw.write( "\t<insert id=\"insert\" parameterType=\""  + processResultMapId(beanName) +  "\">" );
         bw.newLine();
         bw.write( "\t\t INSERT INTO "  + tableName);
         bw.newLine();
         bw.write( " \t\t(" );
         for  int  i =  0  ; i < size ; i++ ) {
             bw.write(columns.get(i));
             if  ( i != size -  1  ) {
                 bw.write( "," );
             }
         }
         bw.write( ") " );
         bw.newLine();
         bw.write( "\t\t VALUES " );
         bw.newLine();
         bw.write( " \t\t(" );
         for  int  i =  0  ; i < size ; i++ ) {
             bw.write( "#{"  + processField(columns.get(i)) +  "}" );
             if  ( i != size -  1  ) {
                 bw.write( "," );
             }
         }
         bw.write( ") " );
         bw.newLine();
         bw.write( "\t</insert>" );
         bw.newLine();
         bw.newLine();
         // 添加insert完
  
  
         //---------------  insert方法(匹配有值的字段)
         bw.write( "\t<!-- 添加 (匹配有值的字段)-->" );
         bw.newLine();
         bw.write( "\t<insert id=\"insertSelective\" parameterType=\""  + processResultMapId(beanName) +  "\">" );
         bw.newLine();
         bw.write( "\t\t INSERT INTO "  + tableName);
         bw.newLine();
         bw.write( "\t\t <trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\" >" );
         bw.newLine();
  
         String tempField =  null ;
         for  int  i =  0  ; i < size ; i++ ) {
             tempField = processField(columns.get(i));
             bw.write( "\t\t\t<if test=\""  + tempField +  " != null\">" );
             bw.newLine();
             bw.write( "\t\t\t\t "  + columns.get(i) +  "," );
             bw.newLine();
             bw.write( "\t\t\t</if>" );
             bw.newLine();
         }
  
         bw.newLine();
         bw.write( "\t\t </trim>" );
         bw.newLine();
  
         bw.write( "\t\t <trim prefix=\"values (\" suffix=\")\" suffixOverrides=\",\" >" );
         bw.newLine();
  
         tempField =  null ;
         for  int  i =  0  ; i < size ; i++ ) {
             tempField = processField(columns.get(i));
             bw.write( "\t\t\t<if test=\""  + tempField +  "!=null\">" );
             bw.newLine();
             bw.write( "\t\t\t\t #{"  + tempField +  "}," );
             bw.newLine();
             bw.write( "\t\t\t</if>" );
             bw.newLine();
         }
  
         bw.write( "\t\t </trim>" );
         bw.newLine();
         bw.write( "\t</insert>" );
         bw.newLine();
         bw.newLine();
         //---------------  完毕
  
  
         // 修改update方法
         bw.write( "\t<!-- 修 改-->" );
         bw.newLine();
         bw.write( "\t<update id=\"updateByPrimaryKeySelective\" parameterType=\""  + processResultMapId(beanName) +  "\">" );
         bw.newLine();
         bw.write( "\t\t UPDATE "  + tableName);
         bw.newLine();
         bw.write( " \t\t <set> " );
         bw.newLine();
  
         tempField =  null ;
         for  int  i =  1  ; i < size ; i++ ) {
             tempField = processField(columns.get(i));
             bw.write( "\t\t\t<if test=\""  + tempField +  " != null\">" );
             bw.newLine();
             bw.write( "\t\t\t\t "  + columns.get(i) +  " = #{"  + tempField +  "}," );
             bw.newLine();
             bw.write( "\t\t\t</if>" );
             bw.newLine();
         }
  
         bw.newLine();
         bw.write( " \t\t </set>" );
         bw.newLine();
         bw.write( "\t\t WHERE "  + columns.get( 0 ) +  " = #{"  + processField(columns.get( 0 )) +  "}" );
         bw.newLine();
         bw.write( "\t</update>" );
         bw.newLine();
         bw.newLine();
         // update方法完毕
  
         // ----- 修改(匹配有值的字段)
         bw.write( "\t<!-- 修 改(匹配有值的字段)-->" );
         bw.newLine();
         bw.write( "\t<update id=\"updateByPrimaryKey\" parameterType=\""  + processResultMapId(beanName) +  "\">" );
         bw.newLine();
         bw.write( "\t\t UPDATE "  + tableName);
         bw.newLine();
         bw.write( "\t\t SET " );
  
         bw.newLine();
         tempField =  null ;
         for  int  i =  1  ; i < size ; i++ ) {
             tempField = processField(columns.get(i));
             bw.write( "\t\t\t "  + columns.get(i) +  " = #{"  + tempField +  "}" );
             if  ( i != size -  1  ) {
                 bw.write( "," );
             }
             bw.newLine();
         }
  
         bw.write( "\t\t WHERE "  + columns.get( 0 ) +  " = #{"  + processField(columns.get( 0 )) +  "}" );
         bw.newLine();
         bw.write( "\t</update>" );
         bw.newLine();
         bw.newLine();
         //---------------  完毕
         
         // ----- 集合(匹配有值的字段)
         bw.write( "\t<!-- 集合(匹配有值的字段)-->" );
         bw.newLine();
         bw.write( "\t<select id=\"findByList\" resultType=\""  + processResultMapId(beanName) +  "\" parameterType=\""  + processResultMapId(beanName) +  "\">" );
         bw.newLine();
         bw.write( "\t\t SELECT" );
         bw.newLine();
         bw.write( "\t\t <include refid=\"Base_Column_List\" />" );
         bw.newLine();
         bw.write( "\t\t FROM "  + tableName);
         bw.newLine();
         bw.write( " \t\t <where> " );
         bw.newLine();
  
         tempField =  null ;
         for  int  i =  0  ; i < size ; i++ ) {
             tempField = processField(columns.get(i));
             bw.write( "\t\t\t<if test=\""  + tempField +  " != null\">" );
             bw.newLine();
             bw.write( "\t\t\t\t "  + columns.get(i) +  " = #{"  + tempField +  "}," );
             bw.newLine();
             bw.write( "\t\t\t</if>" );
             bw.newLine();
         }
  
         bw.newLine();
         bw.write( " \t\t </where>" );
         bw.newLine();
         bw.write( "\t</select>" );
         bw.newLine();
         bw.newLine();
     }
  
  
     public  void  generate()  throws  ClassNotFoundException, SQLException, IOException {
         init();
         String prefix =  "select column_name,data_type from user_tab_columns where  table_name=" ;
         List<String> columns =  null ;
         List<String> types =  null ;
         List<String> comments =  null ;
         PreparedStatement pstate =  null ;
         List<String> tables = getTables();
         for  ( String table : tables ) {
             columns =  new  ArrayList<String>();
             types =  new  ArrayList<String>();
             comments =  new  ArrayList<String>();
             pstate = conn.prepareStatement(prefix + "'" + table + "'" );
             ResultSet results = pstate.executeQuery();
             while  ( results.next() ) {
                 columns.add(results.getString( "column_name" ));
                 types.add(results.getString( "data_type" ));
                 comments.add( "" );
             }
             tableName = table;
             processTable(table);
             buildEntityBean(columns, types, comments,  "数据库表名称:" +tableName);
             buildMapper(columns, types);
             buildMapperXml(columns, types, comments);
         }
         conn.close();
     }
     /**
      * 删除单个文件
      * @param   sPath    被删除文件的文件名
      * @return 单个文件删除成功返回true,否则返回false
      */
     public  static  boolean  deleteFile(String sPath) {
         boolean  flag =  false ;
         File file =  new  File(sPath);
         // 路径为文件且不为空则进行删除
         if  (file.isFile() && file.exists()) {
             file.delete();
             flag =  true ;
         }
         return  flag;
     }
     /**
      * 删除目录(文件夹)以及目录下的文件
      * @param   sPath 被删除目录的文件路径
      * @return  目录删除成功返回true,否则返回false
      */
     public  static  boolean  deleteDirectory(String sPath) {
         //如果sPath不以文件分隔符结尾,自动添加文件分隔符
         if  (!sPath.endsWith(File.separator)) {
             sPath = sPath + File.separator;
         }
         File dirFile =  new  File(sPath);
         //如果dir对应的文件不存在,或者不是一个目录,则退出
         if  (!dirFile.exists() || !dirFile.isDirectory()) {
             return  false ;
         }
         boolean  flag =  true ;
         //删除文件夹下的所有文件(包括子目录)
         File[] files = dirFile.listFiles();
         for  ( int  i =  0 ; i < files.length; i++) {
             //删除子文件
             if  (files[i].isFile()) {
                 flag = deleteFile(files[i].getAbsolutePath());
                 if  (!flag)  break ;
             //删除子目录
             else  {
                 flag = deleteDirectory(files[i].getAbsolutePath());
                 if  (!flag)  break ;
             }
         }
         if  (!flag)  return  false ;
         //删除当前目录
         if  (dirFile.delete()) {
             return  true ;
         else  {
             return  false ;
         }
     }
  
     /**
      * 生成入口
      * @param args
      */
     public  static  void  main( String[] args ) {
         try  {
             // 自动打开生成文件的目录
             File folder =  new  File(BASE_PATH);
             // 判断是否为文件  
             if  (folder.isFile()) {   // 为文件时调用删除文件方法  
                  deleteFile(BASE_PATH);  
             else  {   // 为目录时调用删除目录方法  
                  deleteDirectory(BASE_PATH);  
             }  
             folder.mkdir();
             new  MybatisGenerator().generate();
             Runtime.getRuntime().exec( "cmd /c start explorer " +OPEN_PATH);
         catch  ( ClassNotFoundException e ) {
             e.printStackTrace();
         catch  ( SQLException e ) {
             e.printStackTrace();
         catch  ( IOException e ) {
             e.printStackTrace();
         }
     }
     
}


更多版本:

1、MySql版本http://www.oschina.net/code/snippet_123050_45716


以上,自由复制,更灵活的生成。



本文转自 沫沫金 51CTO博客,原文链接:http://blog.51cto.com/zl0828/1719291,如需转载请自行联系原作者

相关文章
|
19天前
|
XML 数据格式
小米备份descript.xml文件
小米备份descript.xml文件
20 0
|
1月前
|
XML Java 数据库连接
mybatis中在xml文件中通用查询结果列如何使用
mybatis中在xml文件中通用查询结果列如何使用
33 0
|
24天前
|
XML 数据格式 Windows
如何从xml文件创建R语言数据框dataframe
如何从xml文件创建R语言数据框dataframe
|
1天前
|
XML 前端开发 数据格式
BeautifulSoup 是一个 Python 库,用于从 HTML 和 XML 文件中提取数据
BeautifulSoup 是 Python 的一个库,用于解析 HTML 和 XML 文件,即使在格式不规范的情况下也能有效工作。通过创建 BeautifulSoup 对象并使用方法如 find_all 和 get,可以方便地提取和查找文档中的信息。以下是一段示例代码,展示如何安装库、解析 HTML 数据以及打印段落、链接和特定类名的元素。BeautifulSoup 还支持更复杂的查询和文档修改功能。
6 1
|
13天前
|
XML 存储 Java
【OpenCV】—输入输出XML和YAML文件
【OpenCV】—输入输出XML和YAML文件
|
22天前
|
XML C# 数据格式
C# 解析XML文件
C# 解析XML文件
25 1
|
28天前
|
SQL Java 数据库连接
MyBatis精髓揭秘:Mapper代理实现的黑盒探索
MyBatis精髓揭秘:Mapper代理实现的黑盒探索
23 1
|
2月前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
67 1
|
9天前
|
SQL Java 数据库连接
15:MyBatis对象关系与映射结构-Java Spring
15:MyBatis对象关系与映射结构-Java Spring
29 4
|
12天前
|
XML Java 数据库连接
Spring Boot与MyBatis:整合与实战
【4月更文挑战第29天】在现代的Java Web应用开发中,持久化层框架扮演了至关重要的角色。MyBatis作为一款优秀的持久化框架,被广泛应用于Java开发中。Spring Boot提供了简化开发流程的功能,而与MyBatis的整合也变得更加便捷。
23 0