src/main/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePlugin.java
@Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        if (this.logicalDeleteColumn != null) {
            // 1. 逻辑删除ByExample
            Method mLogicalDeleteByExample = JavaElementGeneratorTools.generateMethod(
                    METHOD_LOGICAL_DELETE_BY_EXAMPLE,
                    JavaVisibility.DEFAULT,
                    FullyQualifiedJavaType.getIntInstance(),
                    new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example", "@Param(\"example\")")
            );

            // 添加方法说明
            commentGenerator.addGeneralMethodComment(mLogicalDeleteByExample, introspectedTable);
            // interface 增加方法
            FormatTools.addMethodWithBestPosition(interfaze, mLogicalDeleteByExample);
            logger.debug("itfsw(逻辑删除插件):" + interfaze.getType().getShortName() + "增加方法logicalDeleteByExample。");
            // hook
            PluginTools.getHook(ILogicalDeletePluginHook.class).clientLogicalDeleteByExampleMethodGenerated(mLogicalDeleteByExample, interfaze, introspectedTable);

            // 2. 判断是否有主键，生成主键删除方法
            if (introspectedTable.hasPrimaryKeyColumns()) {
                // 2.1. 逻辑删除ByExample
                Method mLogicalDeleteByPrimaryKey = JavaElementGeneratorTools.generateMethod(
                        METHOD_LOGICAL_DELETE_BY_PRIMARY_KEY,
                        JavaVisibility.DEFAULT,
                        FullyQualifiedJavaType.getIntInstance()
                );
                commentGenerator.addGeneralMethodComment(mLogicalDeleteByPrimaryKey, introspectedTable);

                // 2.2 增强selectByPrimaryKey
                Method mSelectByPrimaryKey = JavaElementGeneratorTools.generateMethod(
                        METHOD_SELECT_BY_PRIMARY_KEY_WITH_LOGICAL_DELETE,
                        JavaVisibility.DEFAULT,
                        introspectedTable.getRules().calculateAllFieldsClass()
                );

                // 添加参数
                Set<FullyQualifiedJavaType> importedTypes = new TreeSet<>();
                importedTypes.add(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Param"));

                if (introspectedTable.getRules().generatePrimaryKeyClass()) {
                    FullyQualifiedJavaType type1 = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType());
                    importedTypes.add(type1);
                    mLogicalDeleteByPrimaryKey.addParameter(new Parameter(type1, "key"));
                    mSelectByPrimaryKey.addParameter(new Parameter(type1, "key", "@Param(\"key\")"));
                } else {
                    // no primary key class - fields are in the base class
                    // if more than one PK field, then we need to annotate the
                    // parameters
                    // for MyBatis
                    List<IntrospectedColumn> introspectedColumns = introspectedTable.getPrimaryKeyColumns();
                    boolean annotate = introspectedColumns.size() > 1;
                    StringBuilder sb = new StringBuilder();
                    for (IntrospectedColumn introspectedColumn : introspectedColumns) {
                        FullyQualifiedJavaType type1 = introspectedColumn.getFullyQualifiedJavaType();
                        importedTypes.add(type1);
                        Parameter parameter = new Parameter(type1, introspectedColumn.getJavaProperty());
                        if (annotate) {
                            sb.setLength(0);
                            sb.append("@Param(\"");
                            sb.append(introspectedColumn.getJavaProperty());
                            sb.append("\")");
                            parameter.addAnnotation(sb.toString());
                        }
                        mLogicalDeleteByPrimaryKey.addParameter(parameter);


                        Parameter parameter1 = new Parameter(type1, introspectedColumn.getJavaProperty());
                        sb.setLength(0);
                        sb.append("@Param(\"");
                        sb.append(introspectedColumn.getJavaProperty());
                        sb.append("\")");
                        parameter1.addAnnotation(sb.toString());

                        mSelectByPrimaryKey.addParameter(parameter1);
                    }
                }

                // interface 增加方法
                interfaze.addImportedTypes(importedTypes);
                FormatTools.addMethodWithBestPosition(interfaze, mLogicalDeleteByPrimaryKey);
                logger.debug("itfsw(逻辑删除插件):" + interfaze.getType().getShortName() + "增加方法logicalDeleteByPrimaryKey。");
                // hook
                PluginTools.getHook(ILogicalDeletePluginHook.class).clientLogicalDeleteByPrimaryKeyMethodGenerated(mLogicalDeleteByPrimaryKey, interfaze, introspectedTable);

                // 增强selectByPrimaryKey
                mSelectByPrimaryKey.addParameter(new Parameter(
                        FullyQualifiedJavaType.getBooleanPrimitiveInstance(),
                        PARAMETER_LOGICAL_DELETED,
                        "@Param(\"" + PARAMETER_LOGICAL_DELETED + "\")"
                ));
                commentGenerator.addGeneralMethodComment(mSelectByPrimaryKey, introspectedTable);
                FormatTools.addMethodWithBestPosition(interfaze, mSelectByPrimaryKey);
            }
        }
        return true;
    }
@Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        if (this.logicalDeleteColumn != null) {
            // 1. 逻辑删除ByExample
            Method mLogicalDeleteByExample = JavaElementGeneratorTools.generateMethod(
                    METHOD_LOGICAL_DELETE_BY_EXAMPLE,
                    JavaVisibility.DEFAULT,
                    FullyQualifiedJavaType.getIntInstance(),
                    new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example", "@Param(\"example\")")
            );

            // 添加方法说明
            commentGenerator.addGeneralMethodComment(mLogicalDeleteByExample, introspectedTable);
            // interface 增加方法
            FormatTools.addMethodWithBestPosition(interfaze, mLogicalDeleteByExample);
            logger.debug("itfsw(逻辑删除插件):" + interfaze.getType().getShortName() + "增加方法logicalDeleteByExample。");
            // hook
            PluginTools.getHook(ILogicalDeletePluginHook.class).clientLogicalDeleteByExampleMethodGenerated(mLogicalDeleteByExample, interfaze, introspectedTable);

            // 2. 判断是否有主键，生成主键删除方法
            if (introspectedTable.hasPrimaryKeyColumns()) {
                // 2.1. 逻辑删除ByExample
                Method mLogicalDeleteByPrimaryKey = JavaElementGeneratorTools.generateMethod(
                        METHOD_LOGICAL_DELETE_BY_PRIMARY_KEY,
                        JavaVisibility.DEFAULT,
                        FullyQualifiedJavaType.getIntInstance()
                );
                commentGenerator.addGeneralMethodComment(mLogicalDeleteByPrimaryKey, introspectedTable);

                // 2.2 增强selectByPrimaryKey
                Method mSelectByPrimaryKey = JavaElementGeneratorTools.generateMethod(
                        METHOD_SELECT_BY_PRIMARY_KEY_WITH_LOGICAL_DELETE,
                        JavaVisibility.DEFAULT,
                        introspectedTable.getRules().calculateAllFieldsClass()
                );

                // 添加参数
                Set<FullyQualifiedJavaType> importedTypes = new TreeSet<>();
                importedTypes.add(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Param"));

                if (introspectedTable.getRules().generatePrimaryKeyClass()) {
                    FullyQualifiedJavaType type1 = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType());
                    importedTypes.add(type1);
                    mLogicalDeleteByPrimaryKey.addParameter(new Parameter(type1, "key"));
                    mSelectByPrimaryKey.addParameter(new Parameter(type1, "key", "@Param(\"key\")"));
                } else {
                    // no primary key class - fields are in the base class
                    // if more than one PK field, then we need to annotate the
                    // parameters
                    // for MyBatis
                    List<IntrospectedColumn> introspectedColumns = introspectedTable.getPrimaryKeyColumns();
                    boolean annotate = introspectedColumns.size() > 1;
                    StringBuilder sb = new StringBuilder();
                    for (IntrospectedColumn introspectedColumn : introspectedColumns) {
                        FullyQualifiedJavaType type1 = introspectedColumn.getFullyQualifiedJavaType();
                        importedTypes.add(type1);
                        Parameter parameter = new Parameter(type1, introspectedColumn.getJavaProperty());
                        if (annotate) {
                            sb.setLength(0);
                            sb.append("@Param(\"");
                            sb.append(introspectedColumn.getJavaProperty());
                            sb.append("\")");
                            parameter.addAnnotation(sb.toString());
                        }
                        mLogicalDeleteByPrimaryKey.addParameter(parameter);


                        Parameter parameter1 = new Parameter(type1, introspectedColumn.getJavaProperty());
                        sb.setLength(0);
                        sb.append("@Param(\"");
                        sb.append(introspectedColumn.getJavaProperty());
                        sb.append("\")");
                        parameter1.addAnnotation(sb.toString());

                        mSelectByPrimaryKey.addParameter(parameter1);
                    }
                }

                // interface 增加方法
                interfaze.addImportedTypes(importedTypes);
                FormatTools.addMethodWithBestPosition(interfaze, mLogicalDeleteByPrimaryKey);
                logger.debug("itfsw(逻辑删除插件):" + interfaze.getType().getShortName() + "增加方法logicalDeleteByPrimaryKey。");
                // hook
                PluginTools.getHook(ILogicalDeletePluginHook.class).clientLogicalDeleteByPrimaryKeyMethodGenerated(mLogicalDeleteByPrimaryKey, interfaze, introspectedTable);

                // 增强selectByPrimaryKey
                mSelectByPrimaryKey.addParameter(new Parameter(
                        FullyQualifiedJavaType.getBooleanPrimitiveInstance(),
                        PARAMETER_LOGICAL_DELETED,
                        "@Param(\"" + PARAMETER_LOGICAL_DELETED + "\")"
                ));
                commentGenerator.addGeneralMethodComment(mSelectByPrimaryKey, introspectedTable);
                FormatTools.addMethodWithBestPosition(interfaze, mSelectByPrimaryKey);
            }
        }
        return true;
    }
@ Override public boolean clientGenerated ( Interface interfaze , TopLevelClass topLevelClass , IntrospectedTable introspectedTable ) { if ( this . logicalDeleteColumn != null ) { // 1. 逻辑删除ByExample Method mLogicalDeleteByExample = JavaElementGeneratorTools . generateMethod ( METHOD_LOGICAL_DELETE_BY_EXAMPLE , JavaVisibility . DEFAULT , FullyQualifiedJavaType . getIntInstance ( ) , new Parameter ( new FullyQualifiedJavaType ( introspectedTable . getExampleType ( ) ) , "example" , "@Param(\"example\")" ) ) ; // 添加方法说明 commentGenerator . addGeneralMethodComment ( mLogicalDeleteByExample , introspectedTable ) ; // interface 增加方法 FormatTools . addMethodWithBestPosition ( interfaze , mLogicalDeleteByExample ) ; logger . debug ( "itfsw(逻辑删除插件):" + interfaze g tType().g e tShortN a m e () + "增加方法lo g i a DeleteByExample。"); // hook PluginTools . getHook ( ILogicalDeletePluginHook . class ) . clientLogicalDeleteByExampleMethodGenerated ( mLogicalDeleteByExample , interfaze , introspectedTable ) ; // 2. 判断是否有主键，生成主键删除方法 if ( introspectedTable . hasPrimaryKeyColumns ( ) ) { // 2.1. 逻辑删除ByExample Method mLogicalDeleteByPrimaryKey = JavaElementGeneratorTools . generateMethod ( METHOD_LOGICAL_DELETE_BY_PRIMARY_KEY , JavaVisibility . DEFAULT , FullyQualifiedJavaType . getIntInstance ( ) ) ; commentGenerator . addGeneralMethodComment ( mLogicalDeleteByPrimaryKey , introspectedTable ) ; // 2.2 增强selectByPrimaryKey Method mSelectByPrimaryKey = JavaElementGeneratorTools . generateMethod ( METHOD_SELECT_BY_PRIMARY_KEY_WITH_LOGICAL_DELETE , JavaVisibility . DEFAULT , introspectedTable . getRules ( ) . calculateAllFieldsClass ( ) ) ; // 添加参数 Set < FullyQualifiedJavaType > importedTypes = new TreeSet <> ( ) ; importedTypes . add ( new FullyQualifiedJavaType ( "org.apache.ibatis.annotations.Param" ) ) ; if ( introspectedTable . getRules ( ) . generatePrimaryKeyClass ( ) ) { FullyQualifiedJavaType type1 = new FullyQualifiedJavaType ( introspectedTable . getPrimaryKeyType ( ) ) ; importedTypes . add ( type1 ) ; mLogicalDeleteByPrimaryKey . addParameter ( new Parameter ( type1 , "key" ) ) ; mSelectByPrimaryKey . addParameter ( new Parameter ( type1 , "key" , "@Param(\"key\")" ) ) ; } else { // no primary key class - fields are in the base class // if more than one PK field, then we need to annotate the // parameters // for MyBatis List < IntrospectedColumn > introspectedColumns = introspectedTable . getPrimaryKeyColumns ( ) ; boolean annotate = introspectedColumns . size ( ) > 1 ; StringBuilder sb = new StringBuilder ( ) ; for ( IntrospectedColumn introspectedColumn : introspectedColumns ) { FullyQualifiedJavaType type1 = introspectedColumn . getFullyQualifiedJavaType ( ) ; importedTypes . add ( type1 ) ; Parameter parameter = new Parameter ( type1 , introspectedColumn . getJavaProperty ( ) ) ; if ( annotate ) { sb . setLength ( 0 ) ; sb . append ( "@Param(\"" ) ; sb . append ( introspectedColumn . getJavaProperty ( ) ) ; sb . append ( "\")" ) ; parameter . addAnnotation ( sb . toString ( ) ) ; } mLogicalDeleteByPrimaryKey . addParameter ( parameter ) ; Parameter parameter1 = new Parameter ( type1 , introspectedColumn . getJavaProperty ( ) ) ; sb . setLength ( 0 ) ; sb . append ( "@Param(\"" ) ; sb . append ( introspectedColumn . getJavaProperty ( ) ) ; sb . append ( "\")" ) ; parameter1 . addAnnotation ( sb . toString ( ) ) ; mSelectByPrimaryKey . addParameter ( parameter1 ) ; } } // interface 增加方法 interfaze . addImportedTypes ( importedTypes ) ; FormatTools . addMethodWithBestPosition ( interfaze , mLogicalDeleteByPrimaryKey ) ; logger . debug ( "itfsw(逻辑删除插件):" + interfaze g tType().g e tShortN a m e () + "增加方法lo g i a DeleteByPrimaryKey。"); // hook PluginTools . getHook ( ILogicalDeletePluginHook . class ) . clientLogicalDeleteByPrimaryKeyMethodGenerated ( mLogicalDeleteByPrimaryKey , interfaze , introspectedTable ) ; // 增强selectByPrimaryKey mSelectByPrimaryKey . addParameter ( new Parameter ( FullyQualifiedJavaType . getBooleanPrimitiveInstance ( ) , PARAMETER_LOGICAL_DELETED , "@Param(\"" + PARAMETER_LOGICAL_DELETED + "\")" ) ) ; commentGenerator . addGeneralMethodComment ( mSelectByPrimaryKey , introspectedTable ) ; FormatTools . addMethodWithBestPosition ( interfaze , mSelectByPrimaryKey ) ; } } return true ; }
Java Client Methods 生成
具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
@param interfaze
@param topLevelClass
@param introspectedTable
@return
Java Client Methods 生成 具体执行顺序 http : // www . mybatis . org / generator / reference / pluggingIn . html