Mybatis generator automatic code generation tool is embedded in maven project. The path obtained in eclipse and intellij is different

Before reading this article, please read another article about the embedding of mybatis generator into maven project
Mybatis generator automatic code generation tool is embedded in Maven project to generate java and xml files corresponding to database tables with one click

The method introduced in this paper can run the main method in eclipse to execute the program, but if the intelliJ editor is used, the run time will generate the file under the main project due to the lack of maven subproject path (our generated file is placed in the XXX common subproject). I made a compatibility here, and put the code below.

1. Compatibility code

     /**
     * Processing the path of the generated file is mainly to solve the problem that the path of the subproject cannot be obtained by running in intellij
     * @author North
     * @date 2018 1:35:19 PM, January 30, 2010
     * @param config
     */
    private void dealFilePath(Configuration config) {
        //Extract subproject name
        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) {
            //Extract the relevant configuration of generated entity class, example class, mapper class and xml file
            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);
                //Extract file name
                String fileName = table.getDomainObjectName();
                if(fileName == null){
                    fileName = StringUtil.camelName(table.getTableName());
                }
                fileName += "Mapper.xml";
                
                //Create file object according to relative path of xml file
                String xmlPath = sqlProject + "/" + sqlPackage + "/" + StringUtil.upperCaseFirst(fileName);
                File xmlFile = new File(xmlPath);
                
                //If there is no subproject directory in the absolute path, add (because the file object created in Intellij is missing the subproject directory datacenter common)
                String sqlPath = xmlFile.getAbsolutePath();
                if(!sqlPath.contains(projectName)){
                    //Generate xml file path plus subproject directory
                    sqlProject = projectName + "/" + sqlProject;
                    sqlConfig.setTargetProject(sqlProject);
                    context.setSqlMapGeneratorConfiguration(sqlConfig);
                    
                    //Extract the project path of the java file, plus the sub project directory
                    String javaProject = projectName + "/" + modelConfig.getTargetProject();
                    
                    //Generate entity class, example class file path plus subproject directory
                    modelConfig.setTargetProject(javaProject);
                    context.setJavaModelGeneratorConfiguration(modelConfig);
                    
                    //Generate mapper class file path plus subproject directory
                    mapperConfig.setTargetProject(javaProject);
                    context.setJavaClientGeneratorConfiguration(mapperConfig);
                }
            }
        }
    }

2, invoke the above method in executing the generation method (see the preceding code in the full code).

/**
     * Generate source code according to the configured database table
     * @author North
     * @date 2018 11:04:26 am, January 17, 2010
     * @throws Exception
     */
    public void generator() throws Exception{

        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        //Specify reverse engineering profile
//      File configFile = new File("myGeneratorConfig.xml"); 
        
        //Read configuration file
        File configFile = ResourceUtils.getFile("classpath:myGeneratorConfig.xml");// new File("classpath: myGeneratorConfig.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        
        //Process build file path
        this.dealFilePath(config);
        
        //Delete the xml file to be generated
        this.deleteOldXmlFile(config);
        
        //Back up Mapper files
        Map<String, String> mapperFileValue = this.backupMapperFile(config);
        
        //Generate code execution
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
        
        //Restore the Mapper file that already exists
        this.recoverMapperFile(mapperFileValue);
    }

Keywords: xml Maven Java Database

Added by r3n on Thu, 02 Apr 2020 23:13:16 +0300