package com.ruoyi;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;


public class GeneratorUtil {
    public static void generate() {
    try {
        List<String> warnings = new ArrayList<>();
        boolean overwrite = true;

        // 获取 generatorConfig.xml 文件路径
        URL resourceUrl = GeneratorUtil.class.getResource("/generatorConfig.xml");
        String configFilePath = URLDecoder.decode(resourceUrl.getFile(), "UTF-8");

        File configFileObj = new File(configFilePath);
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFileObj);

        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

    public static void main(String[] args) {
        generate();
    }
}
