美文网首页程序员
Mybatis-generator自动生成代码工具嵌入maven

Mybatis-generator自动生成代码工具嵌入maven

作者: 李北北 | 来源:发表于2018-03-08 13:53 被阅读0次

阅读本文之前请先阅读我的另一篇介绍Mybatis-generator自动生成代码工具嵌入maven项目的文章:
Mybatis-generator自动生成代码工具嵌入Maven项目中,实现一键生成数据库表对应的java、xml文件

文中介绍的方法在eclipse中运行main方法执行程序是没问题的,但如果用的是intelliJ编辑器,那么运行时候将会因为缺少maven子工程路径(我们的生成文件是放在xxx-common子工程中),而将文件生成到主工程下。在这里做了一个兼容,下面贴上代码。

1、兼容代码

     /**
     * 处理生成文件路径,主要是解决intellij中运行取不到子项目路径问题
     * @author 北北
     * @date 2018年1月30日下午1:35:19
     * @param config
     */
    private void dealFilePath(Configuration config) {
        //提取子项目名称
        String rootPath = System.getProperty("user.dir");
        String projectName = rootPath.substring(rootPath.lastIndexOf("\\") + 1);
        if(!projectName.contains("common")){
            projectName = projectName + "-common";
        }
        
        List<Context> contextList = config.getContexts();
        for (Context context : contextList) {
            //提取生成实体类、example类、mapper类、xml文件相关配置
            SqlMapGeneratorConfiguration sqlConfig = context.getSqlMapGeneratorConfiguration();
            JavaModelGeneratorConfiguration modelConfig = context.getJavaModelGeneratorConfiguration();
            JavaClientGeneratorConfiguration mapperConfig = context.getJavaClientGeneratorConfiguration();
            String sqlProject = sqlConfig.getTargetProject();
            String sqlPackage = sqlConfig.getTargetPackage();
            List<TableConfiguration> tableList = context.getTableConfigurations();
            if(tableList.size() > 0){
                TableConfiguration table = tableList.get(0);
                //提取文件名
                String fileName = table.getDomainObjectName();
                if(fileName == null){
                    fileName = StringUtil.camelName(table.getTableName());
                }
                fileName += "Mapper.xml";
                
                //根据xml文件相对路径创建file对象
                String xmlPath = sqlProject + "/" + sqlPackage + "/" + StringUtil.upperCaseFirst(fileName);
                File xmlFile = new File(xmlPath);
                
                //如果绝对路径中没有子项目目录,则加上(因为Intellij中创建的file对象缺少子项目目录datacenter-common)
                String sqlPath = xmlFile.getAbsolutePath();
                if(!sqlPath.contains(projectName)){
                    //生成xml文件路径加上子项目目录
                    sqlProject = projectName + "/" + sqlProject;
                    sqlConfig.setTargetProject(sqlProject);
                    context.setSqlMapGeneratorConfiguration(sqlConfig);
                    
                    //提取java文件所在的项目路径,加上子项目目录
                    String javaProject = projectName + "/" + modelConfig.getTargetProject();
                    
                    //生成实体类、example类文件路径加上子项目目录
                    modelConfig.setTargetProject(javaProject);
                    context.setJavaModelGeneratorConfiguration(modelConfig);
                    
                    //生成mapper类文件路径加上子项目目录
                    mapperConfig.setTargetProject(javaProject);
                    context.setJavaClientGeneratorConfiguration(mapperConfig);
                }
            }
        }
    }

2、在执行生成方法(完整代码看前面引用的文章)中调用上面的方法

/**
     * 根据配置的数据库表生成源码
     * @author 北北
     * @date 2018年1月17日上午11:04:26
     * @throws Exception
     */
    public void generator() throws Exception{

        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        //指定 逆向工程配置文件
//      File configFile = new File("myGeneratorConfig.xml"); 
        
        //读取配置文件
        File configFile = ResourceUtils.getFile("classpath:myGeneratorConfig.xml");// new File("classpath: myGeneratorConfig.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        
        //处理生成文件路径
        this.dealFilePath(config);
        
        //删除需要生成的xml文件
        this.deleteOldXmlFile(config);
        
        //对Mapper文件备份处理
        Map<String, String> mapperFileValue = this.backupMapperFile(config);
        
        //生成代码执行
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
        
        //恢复已经存在的Mapper文件
        this.recoverMapperFile(mapperFileValue);
    }

相关文章

网友评论

    本文标题:Mybatis-generator自动生成代码工具嵌入maven

    本文链接:https://www.haomeiwen.com/subject/jjkfzxtx.html