mybatis当中如何自动生成Model和映射程序与配置文件和所需要的类

3.自动生成Model和映射程序与配置文件:【新手略过】

利用mybatis编写的MyBatisGenerator,我们可以生成我们所需要的类和配置文件。


例 1.3

package com;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
public class MybatisMain {
    public static void mybatisCreate() {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        String projectBasePath = new File("").getAbsolutePath();
        System.out.println(projectBasePath);
        File fileConfigFile = new File(projectBasePath
                + "/src/generatorConfig.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        try {
            Configuration config = cp.parseConfiguration(fileConfigFile);
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            try {
                MyBatisGenerator myBatisGenerator = new MyBatisGenerator(
                        config, callback, warnings);
                try {
                    myBatisGenerator.generate(null);
                    System.out.println("after myBatisGenerator.generate(null); ");
                } catch (SQLException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } catch (InvalidConfigurationException e) {
                e.printStackTrace();
            }
更多内容请见原文,原文转载自:https://blog.csdn.net/qq_44639795/article/details/100141518

posted @ 2022-01-14 17:38  malala  阅读(171)  评论(0编辑  收藏  举报