Mybatis generator修改Mapper.java文件實現(xiàn)詳解
源碼分析:
我寫的代碼生成插件Gitee地址 同樣是在擴展 Mybatis generator插件的時候,有這樣一個需求是需要在生成的,那么 如何修改Mapper.java文件? 跟著Mybatis generator 源碼去找一找 哪里可以擴展
源碼入口:Context.generateFiles()
public void generateFiles(ProgressCallback callback,
List<GeneratedJavaFile> generatedJavaFiles,
List<GeneratedXmlFile> generatedXmlFiles, List<String> warnings)
throws InterruptedException {
if (introspectedTables != null) {
for (IntrospectedTable introspectedTable : introspectedTables) {
callback.checkCancel();
introspectedTable.initialize();
introspectedTable.calculateGenerators(warnings, callback);
//這里是 javaFiles的組裝地方,主要去看一下introspectedTable
.getGeneratedJavaFiles()方法
generatedJavaFiles.addAll(introspectedTable
.getGeneratedJavaFiles());
//
generatedXmlFiles.addAll(introspectedTable
.getGeneratedXmlFiles());
//這里預留了插件來生成JavaFile文件;
generatedJavaFiles.addAll(pluginAggregator
.contextGenerateAdditionalJavaFiles(introspectedTable));
//這里預留了插件來生成Xml文件;
generatedXmlFiles.addAll(pluginAggregator
.contextGenerateAdditionalXmlFiles(introspectedTable));
}
}
generatedJavaFiles.addAll(pluginAggregator
.contextGenerateAdditionalJavaFiles());
generatedXmlFiles.addAll(pluginAggregator
.contextGenerateAdditionalXmlFiles());
}
然后進入introspectedTable.getGeneratedJavaFiles()方法
@Override
public List<GeneratedJavaFile> getGeneratedJavaFiles() {
List<GeneratedJavaFile> answer = new ArrayList<GeneratedJavaFile>();
//javaModelGenerators 存的是 JavaModel 和 JavaModelExample 類
for (AbstractJavaGenerator javaGenerator : javaModelGenerators) {
//這一行才是重點,因為所有的準備數(shù)據(jù)都是在這個方法里面
List<CompilationUnit> compilationUnits = javaGenerator
.getCompilationUnits();
for (CompilationUnit compilationUnit : compilationUnits) {
GeneratedJavaFile gjf = new GeneratedJavaFile(compilationUnit,
context.getJavaModelGeneratorConfiguration()
.getTargetProject(),
context.getProperty(PropertyRegistry.CONTEXT_JAVA_FILE_ENCODING),
context.getJavaFormatter());
answer.add(gjf);
}
}
// clientGenerators 然后javaModelGenerators 存的是 JavaMapper.java文件
for (AbstractJavaGenerator javaGenerator : clientGenerators) {
//這一行才是重點,因為所有的準備數(shù)據(jù)都是在這個方法里面
List<CompilationUnit> compilationUnits = javaGenerator
.getCompilationUnits();
for (CompilationUnit compilationUnit : compilationUnits) {
GeneratedJavaFile gjf = new GeneratedJavaFile(compilationUnit,
context.getJavaClientGeneratorConfiguration()
.getTargetProject(),
context.getProperty(PropertyRegistry.CONTEXT_JAVA_FILE_ENCODING),
context.getJavaFormatter());
answer.add(gjf);
}
}
return answer;
}
重點方法:javaGenerator.getCompilationUnits();
這個方法是真正填充數(shù)據(jù)的地方 AbstractJavaGenerator 這個是抽象類,主要是用來生成Java文件的 下面有很多實現(xiàn)類; 比如生成 JavaModel 文件的BaseRecordGenerator JavaModelExample文件的ExampleGenerator Mapper.java文件的JavaMapperGenerator 這個實現(xiàn)類都實現(xiàn)了getCompilationUnits方法;這些方法都在為即將生成的文件組裝數(shù)據(jù) 我們看一下JavaMapperGenerator 中的實現(xiàn)
@Override
public List<CompilationUnit> getCompilationUnits() {
progressCallback.startTask(getString("Progress.17", //$NON-NLS-1$
introspectedTable.getFullyQualifiedTable().toString()));
CommentGenerator commentGenerator = context.getCommentGenerator();
FullyQualifiedJavaType type = new FullyQualifiedJavaType(
introspectedTable.getMyBatis3JavaMapperType());
Interface interfaze = new Interface(type);
interfaze.setVisibility(JavaVisibility.PUBLIC);
//看到這里喜出望外,這里就是擴展點了;因為它把inerfaze給傳進去了,那我們可以在這里做一些我們想做的事情
commentGenerator.addJavaFileComment(interfaze);
//省略無關......
修改Mapper.java文件
在前幾篇文章中我們已經(jīng)創(chuàng)建了CommentGenerator對象了,那我們可以在這里面來做擴展
@Override
public void addJavaFileComment(CompilationUnit compilationUnit) {
//生成的是 JavaModel 和 JavaModelExample 文件
if(compilationUnit instanceof TopLevelClass){
//這里可以修改 JavaModel 和 JavaModelExample 文件
/*TopLevelClass topLevelClass = (TopLevelClass)compilationUnit;
String shortName = compilationUnit.getType().getShortName();
topLevelClass.addAnnotation("@Resource");
topLevelClass.addImportedType("javax.annotation.Resource");*/
}
//生成的是Mapper.java 文件
if(compilationUnit instanceof Interface){
Interface anInterface = (Interface)compilationUnit;
//下面的可以給JavaFile 添加注釋
//topLevelClass.addFileCommentLine("/**generator by Shirc generator common.....**/");
String shortName = compilationUnit.getType().getShortName();
if(shortName!=null||shortName.endsWith("Mapper"))return;
//只給JavaModel添加注解就行了,Example不需要
anInterface.addAnnotation("@Resource");
anInterface.addImportedType(new FullyQualifiedJavaType("javax.annotation.Resource"));
}
}
上面的代碼中 給Mapper.java 文件添加了注解,如果想改更多,可以按照它的格式來做;
以上就是Mybatis generator修改Mapper.java文件實現(xiàn)詳解的詳細內容,更多關于Mybatis generator修改Mapper.java的資料請關注腳本之家其它相關文章!
相關文章
Spring@Autowired與@Resource的區(qū)別有哪些
這篇文章主要為大家詳細介紹了@Autowired與@Resource的區(qū)別,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02
mybatisplus?selectOne查詢,有數(shù)據(jù),但返回為null問題
這篇文章主要介紹了mybatisplus?selectOne查詢,有數(shù)據(jù),但返回為null問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
java抓取網(wǎng)頁數(shù)據(jù)獲取網(wǎng)頁中所有的鏈接實例分享
java抓取網(wǎng)頁數(shù)據(jù)獲取網(wǎng)頁中所有的鏈接實例分享,使用方法,只要實例化HtmlParser時傳入網(wǎng)頁地址就可以了2013-12-12
詳解手把手Maven搭建SpringMVC+Spring+MyBatis框架(超級詳細版)
本篇文章主要介紹了手把手Maven搭建SpringMVC+Spring+MyBatis框架(超級詳細版),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12

