在idea中使用mybatis generator執行後沒生成相關程式碼也沒任何控制檯檔案輸出
阿新 • • 發佈:2019-07-31
- 解決處理
建立一個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&g