mybatis-generator源码解读系列(二):主函数逻辑

主函数基本逻辑

1、命令行参数是否合理并解析

2、读取xml配置文件并解析位内存数据以工使用

3、实例化一个shell回调函数,用于执行过程中的回调

4、实例化一个mybatis代码生成对象

5、实例化一个过程回调函数,用于是否输出过程日志等处理(或根本不需要回调)

6、调用生成方法生成代码并保存为相关文件

代码解析
1、命令行参数解析
// 至少要有一个参数。。。执行的层面需要配置文件
        if (args.length == 0) {
            usage();
            System.exit(0);
            return; // only to satisfy compiler, never returns
        }

        Map<String, String> arguments = parseCommandLine(args);

        // 仅仅是帮助信息,有该参数时不生成代码,仅仅显示各个命令行参数的作用
        if (arguments.containsKey(HELP_1)) {
            usage();
            System.exit(0);
            return; // only to satisfy compiler, never returns
        }

        // 必须要有配置文件
        if (!arguments.containsKey(CONFIG_FILE)) {
            writeLine(getString("RuntimeError.0")); //$NON-NLS-1$
            return;
        }

        List<String> warnings = new ArrayList<String>();

        String configfile = arguments.get(CONFIG_FILE);
        File configurationFile = new File(configfile);
        if (!configurationFile.exists()) {
            writeLine(getString("RuntimeError.1", configfile)); //$NON-NLS-1$
            return;
        }

        // table参数,以,分割,从命令读取要生成代码的表
        Set<String> fullyqualifiedTables = new HashSet<String>();
        if (arguments.containsKey(TABLES)) {
            StringTokenizer st = new StringTokenizer(arguments.get(TABLES), ","); //$NON-NLS-1$
            while (st.hasMoreTokens()) {
                String s = st.nextToken().trim();
                if (s.length() > 0) {
                    fullyqualifiedTables.add(s);
                }
            }
        }
        // contextids参数,以,分割,从命令行读取上下数据
        Set<String> contexts = new HashSet<String>();
        if (arguments.containsKey(CONTEXT_IDS)) {
            StringTokenizer st = new StringTokenizer(
                    arguments.get(CONTEXT_IDS), ","); //$NON-NLS-1$
            while (st.hasMoreTokens()) {
                String s = st.nextToken().trim();
                if (s.length() > 0) {
                    contexts.add(s);
                }
            }
        }
2、读取配置并转化为内存对象
ConfigurationParser cp = new ConfigurationParser(warnings);
// 将xml配置文件读入到配置对象中
Configuration config = cp.parseConfiguration(configurationFile);
3、回调函数以及生成代码对象实例化
// 缺省的shell回调对象
DefaultShellCallback shellCallback = new DefaultShellCallback(
                    arguments.containsKey(OVERWRITE));
 // 实例化生成对象
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);

// 实例化处理过程回调函数,如verbose参数未提供,则不为null
 ProgressCallback progressCallback = arguments.containsKey(VERBOSE) ? new VerboseProgressCallback()
4、代码生成
// 生成文件
 myBatisGenerator.generate(progressCallback, contexts, fullyqualifiedTables);
※主要的代码就是配置解析和代码生成,后续的文章里将进一步解析这两个重点!
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值