1. 程式人生 > >【Mybatis】Mybatis generator如何修改Mapper.java檔案

【Mybatis】Mybatis generator如何修改Mapper.java檔案

我寫的程式碼生成外掛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) {
        //這一行才是重點,因為所有的準備資料都是在這個方法裡面
            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) {
         //這一行才是重點,因為所有的準備資料都是在這個方法裡面
            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();

這個方法是真正填充資料的地方
AbstractJavaGenerator 這個是抽象類,主要是用來生成Java檔案的 下面有很多實現類;
比如生成
JavaModel 檔案的BaseRecordGenerator
JavaModelExample檔案的ExampleGenerator
Mapper.java檔案的JavaMapperGenerator
這個實現類都實現了getCompilationUnits方法;這些方法都在為即將生成的檔案組裝資料
我們看一下JavaMapperGenerator 中的實現

 @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檔案


在前幾篇文章中我們已經建立了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 檔案添加了註解,如果想改更多,可以按照它的格式來做;