- 解决处理
创建一个warningList并传递给MyBatisGenerator
List<String> warnings = new ArrayList<>();
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, null, warnings);
前面大家都会注意,但是就是没有输出warnings(List)。输出即可看到错误提示信息
if (warnings.isEmpty()) {
System.out.println("MyBatis文件生成成功!!");
} else {
System.err.println(warnings);
}
- 原因
mybatis generator出现异常不是抛出,而是进行捕获放在传入warnings(List)里面,如果你不传递的话,它会初始化一个,但是在结束时并不会输出任何信息。
开始和大家一样没有输出List,然后一步步断点调试,发现在最后写入文件的时候没有找到路径。例如在MyBatisGenerator.class源码中你会发现捕获的异常处理的情况
private void writeGeneratedJavaFile(GeneratedJavaFile gjf, ProgressCallback callback) throws InterruptedException, IOException { File targetFile; String source; try { File directory = shellCallback.getDirectory(gjf .getTargetProject(), gjf.getTargetPackage()); targetFile = new File(directory, gjf.getFileName()); if (targetFile.exists()) { if (shellCallback.isMergeSupported()) { source = shellCallback.mergeJavaFile(gjf .getFormattedContent(), targetFile, MergeConstants.OLD_ELEMENT_TAGS, gjf.getFileEncoding()); } else if (shellCallback.isOverwriteEnabled()) { source = gjf.getFormattedContent(); warnings.add(getString("Warning.11", //$NON-NLS-1$ targetFile.getAbsolutePath())); } else { source = gjf.getFormattedContent(); targetFile = getUniqueFileName(directory, gjf .getFileName()); warnings.add(getString( "Warning.2", targetFile.getAbsolutePath())); //$NON-NLS-1$ } } else { source = gjf.getFormattedContent(); } callback.checkCancel(); callback.startTask(getString( "Progress.15", targetFile.getName())); //$NON-NLS-1$ writeFile(targetFile, source, gjf.getFileEncoding()); } catch (ShellException e) { warnings.add(e.getMessage()); } }
下面是输出的错误信息
[The specified target project directory .\src/main/java does not exist]
- 最后
提示是路径不存在,在配置文件中填写的是相对路径;有点奇怪,同事使用eclipse就可以使用相对路径,但是我用idea就不行,之前使用maven插件是可以使用相对路径的
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin>