|
1 | 1 | package com.xh.basic.utils; |
2 | 2 |
|
| 3 | +import org.mybatis.generator.api.MyBatisGenerator; |
| 4 | +import org.mybatis.generator.config.Configuration; |
| 5 | +import org.mybatis.generator.config.xml.ConfigurationParser; |
| 6 | +import org.mybatis.generator.exception.InvalidConfigurationException; |
| 7 | +import org.mybatis.generator.exception.XMLParserException; |
| 8 | +import org.mybatis.generator.internal.DefaultShellCallback; |
| 9 | +import org.springframework.util.ResourceUtils; |
| 10 | + |
| 11 | +import java.io.File; |
| 12 | +import java.io.IOException; |
| 13 | +import java.net.URL; |
| 14 | +import java.sql.SQLException; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | + |
3 | 18 | /** |
4 | 19 | * @author szq |
5 | 20 | * @Package com.xh.basic.utils |
6 | | - * @Description: to do ... |
| 21 | + * @Description: mybatis generator 代码生成 |
7 | 22 | * @date 2018/4/2517:16 |
8 | 23 | */ |
9 | 24 | public class MyBatisGeneratorTool { |
| 25 | + public static void main(String[] args){ |
| 26 | + List<String> warnings = new ArrayList<String>(); |
| 27 | + boolean overwrite = true; |
| 28 | + String genCfg = "generatorConfig.xml"; |
| 29 | + URL url = MyBatisGeneratorTool.class.getResource(genCfg); |
| 30 | + //File configFile = new File(MyBatisGeneratorTool.class.getResource(genCfg).getFile()); |
| 31 | + File configFile = new File(genCfg); |
| 32 | + ConfigurationParser cp = new ConfigurationParser(warnings); |
| 33 | + Configuration config = null; |
| 34 | + try{ |
| 35 | + config = cp.parseConfiguration(configFile); |
| 36 | + }catch (IOException e){ |
| 37 | + e.printStackTrace(); |
| 38 | + }catch (XMLParserException e){ |
| 39 | + e.printStackTrace(); |
| 40 | + } |
| 41 | + |
| 42 | + DefaultShellCallback callback = new DefaultShellCallback(overwrite); |
| 43 | + MyBatisGenerator myBatisGenerator = null; |
| 44 | + try{ |
| 45 | + myBatisGenerator = new MyBatisGenerator(config, callback, warnings); |
| 46 | + }catch(InvalidConfigurationException e){ |
| 47 | + e.printStackTrace(); |
| 48 | + } |
| 49 | + |
| 50 | + try{ |
| 51 | + myBatisGenerator.generate(null); |
| 52 | + }catch (SQLException e){ |
| 53 | + e.printStackTrace(); |
| 54 | + }catch (IOException e){ |
| 55 | + e.printStackTrace(); |
| 56 | + }catch (InterruptedException e){ |
| 57 | + e.printStackTrace(); |
| 58 | + } |
| 59 | + } |
10 | 60 | } |
0 commit comments