欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis generator修改Mapper.java文件實現(xiàn)詳解

 更新時間:2022年09月30日 09:39:10   作者:石臻臻的雜貨鋪  
這篇文章主要為大家介紹了Mybatis generator修改Mapper.java文件實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

源碼分析:

我寫的代碼生成插件Gitee地址 同樣是在擴(kuò)展 Mybatis generator插件的時候,有這樣一個需求是需要在生成的,那么 如何修改Mapper.java文件? 跟著Mybatis generator 源碼去找一找 哪里可以擴(kuò)展

源碼入口: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());
				//這里預(yù)留了插件來生成JavaFile文件;
                generatedJavaFiles.addAll(pluginAggregator
                        .contextGenerateAdditionalJavaFiles(introspectedTable));
                //這里預(yù)留了插件來生成Xml文件;
                generatedXmlFiles.addAll(pluginAggregator
                        .contextGenerateAdditionalXmlFiles(introspectedTable));
            }
        }
        generatedJavaFiles.addAll(pluginAggregator
                .contextGenerateAdditionalJavaFiles());
        generatedXmlFiles.addAll(pluginAggregator
                .contextGenerateAdditionalXmlFiles());
    }

然后進(jìn)入introspectedTable.getGeneratedJavaFiles()方法

@Override
    public List<GeneratedJavaFile> getGeneratedJavaFiles() {
        List<GeneratedJavaFile> answer = new ArrayList<GeneratedJavaFile>();
		//javaModelGenerators 存的是 JavaModel 和 JavaModelExample 類
        for (AbstractJavaGenerator javaGenerator : javaModelGenerators) {
        //這一行才是重點,因為所有的準(zhǔn)備數(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) {
         //這一行才是重點,因為所有的準(zhǔn)備數(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);
        //看到這里喜出望外,這里就是擴(kuò)展點了;因為它把inerfaze給傳進(jìn)去了,那我們可以在這里做一些我們想做的事情
        commentGenerator.addJavaFileComment(interfaze);
	    //省略無關(guān)......

修改Mapper.java文件

在前幾篇文章中我們已經(jīng)創(chuàng)建了CommentGenerator對象了,那我們可以在這里面來做擴(kuò)展

	@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)詳解的詳細(xì)內(nèi)容,更多關(guān)于Mybatis generator修改Mapper.java的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論