mybatis-generator配置和使用流程

MyBatis Generator 是 MyBatis 提供的一个代码生成工具。可以帮我们生成 表对应的持久化对象(po)、操作数据库的接口(dao)、CRUD sql的xml(mapper)。
MyBatis Generator 是一个独立工具,你可以下载它的jar包来运行、也可以在 Ant 和 maven 运行。

使用环境

引入插件

配置插件

MyBatis Generator 插件需要根据一个 MyBatis Generator config 文件,来具体运行
配置如下,版本我用的是目前最新的版本 1.4.0


配置 MyBatis Generator Config

MyBatis Generator 插件启动后,会根据你在 pom 中配置都路径找到该配置文件。
这个配置文件才是详细都配置 MyBatis Generator 生成代码的各种细节。
其中最重要的就是 context ,你的配置文件至少得包含一个context




    

    
        
        
        
        
        
        
        
        
        
        
        

        
            
            
            
            
        


        
            
            
            
            
        

        
        
            
            
            
            
            
            
        

        
        
            
        
        
        
            
        

        

使用 MyBatis Generator

配置好后,双击 maven 中的 MyBatis Generator 运行



也可以使用工具类运行

public class MybatisGeneratorUtil {
    /**
     * 通过执行本方法,调用 Mybatis Generator 工具,生成相应代码。
     * 避免 Eclipse、IDEA 之间插件使用的差异。
     * @param args
     * @return
     */
    public static void main(String[] args) throws Exception {
        List warnings = new ArrayList<>();
        String configFilePath = MybatisGeneratorUtil.class.getClassLoader().getResource("generatorConfig.xml").getFile(); // mybatis-generator.xml 配置文件放在 resources 目录里。
        File configFile = new File(configFilePath);
        ConfigurationParser parser = new ConfigurationParser(warnings);
        Configuration config = parser.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(true); // 存在则覆盖
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }
}
生成文件

你可能感兴趣的:(mybatis-generator配置和使用流程)