src/main/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePlugin.java
@Override
    public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
        if (this.logicalDeleteColumn != null) {
            // 1. 逻辑删除ByExample
            XmlElement logicalDeleteByExample = new XmlElement("update");
            logicalDeleteByExample.addAttribute(new Attribute("id", METHOD_LOGICAL_DELETE_BY_EXAMPLE));
            logicalDeleteByExample.addAttribute(new Attribute("parameterType", "map"));
            commentGenerator.addComment(logicalDeleteByExample);

            StringBuilder sb = new StringBuilder();
            sb.append("update ");
            sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
            sb.append(" set ");
            // 更新逻辑删除字段
            sb.append(this.logicalDeleteColumn.getActualColumnName());
            sb.append(" = ");
            sb.append(XmlElementGeneratorTools.generateLogicalDeleteColumnValue(this.logicalDeleteColumn, this.logicalDeleteValue));

            logicalDeleteByExample.addElement(new TextElement(sb.toString()));

            logicalDeleteByExample.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));
            document.getRootElement().addElement(logicalDeleteByExample);
            logger.debug("itfsw(逻辑删除插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加方法logicalDeleteByExample的实现。");
            // hook
            PluginTools.getHook(ILogicalDeletePluginHook.class).sqlMapLogicalDeleteByExampleElementGenerated(document, logicalDeleteByExample, this.logicalDeleteColumn, this.logicalDeleteValue, introspectedTable);

            // 2. 判断是否有主键，生成主键删除方法
            if (introspectedTable.hasPrimaryKeyColumns()) {
                XmlElement logicalDeleteByPrimaryKey = new XmlElement("update");
                logicalDeleteByPrimaryKey.addAttribute(new Attribute("id", METHOD_LOGICAL_DELETE_BY_PRIMARY_KEY));

                String parameterClass;
                if (introspectedTable.getRules().generatePrimaryKeyClass()) {
                    parameterClass = introspectedTable.getPrimaryKeyType();
                } else {
                    // PK fields are in the base class. If more than on PK
                    // field, then they are coming in a map.
                    if (introspectedTable.getPrimaryKeyColumns().size() > 1) {
                        parameterClass = "map";
                    } else {
                        parameterClass = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType().toString();
                    }
                }
                logicalDeleteByPrimaryKey.addAttribute(new Attribute("parameterType", parameterClass));
                commentGenerator.addComment(logicalDeleteByPrimaryKey);

                StringBuilder sb1 = new StringBuilder();
                sb1.append("update ");
                sb1.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
                sb1.append(" set ");
                // 更新逻辑删除字段
                sb1.append(this.logicalDeleteColumn.getActualColumnName());
                sb1.append(" = ");
                sb1.append(XmlElementGeneratorTools.generateLogicalDeleteColumnValue(this.logicalDeleteColumn, this.logicalDeleteValue));

                logicalDeleteByPrimaryKey.addElement(new TextElement(sb1.toString()));

                XmlElementGeneratorTools.generateWhereByPrimaryKeyTo(logicalDeleteByPrimaryKey, introspectedTable.getPrimaryKeyColumns());

                document.getRootElement().addElement(logicalDeleteByPrimaryKey);
                logger.debug("itfsw(逻辑删除插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加方法logicalDeleteByPrimaryKey的实现。");
                // hook
                PluginTools.getHook(ILogicalDeletePluginHook.class).sqlMapLogicalDeleteByPrimaryKeyElementGenerated(document, logicalDeleteByPrimaryKey, this.logicalDeleteColumn, this.logicalDeleteValue, introspectedTable);


                // 3. 增强selectByPrimaryKey
                XmlElement selectByPrimaryKey = new XmlElement("select");
                commentGenerator.addComment(selectByPrimaryKey);

                selectByPrimaryKey.addAttribute(new Attribute("id", METHOD_SELECT_BY_PRIMARY_KEY_WITH_LOGICAL_DELETE));
                if (introspectedTable.getRules().generateResultMapWithBLOBs()) {
                    selectByPrimaryKey.addAttribute(new Attribute("resultMap", introspectedTable.getResultMapWithBLOBsId()));
                } else {
                    selectByPrimaryKey.addAttribute(new Attribute("resultMap", introspectedTable.getBaseResultMapId()));
                }

                selectByPrimaryKey.addAttribute(new Attribute("parameterType", "map"));

                sb = new StringBuilder();
                sb.append("select ");

                if (stringHasValue(introspectedTable.getSelectByPrimaryKeyQueryId())) {
                    sb.append('\'');
                    sb.append(introspectedTable.getSelectByPrimaryKeyQueryId());
                    sb.append("' as QUERYID,");
                }
                selectByPrimaryKey.addElement(new TextElement(sb.toString()));
                selectByPrimaryKey.addElement(XmlElementGeneratorTools.getBaseColumnListElement(introspectedTable));
                if (introspectedTable.hasBLOBColumns()) {
                    selectByPrimaryKey.addElement(new TextElement(","));
                    selectByPrimaryKey.addElement(XmlElementGeneratorTools.getBlobColumnListElement(introspectedTable));
                }

                sb.setLength(0);
                sb.append("from ");
                sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
                selectByPrimaryKey.addElement(new TextElement(sb.toString()));

                XmlElementGeneratorTools.generateWhereByPrimaryKeyTo(selectByPrimaryKey, introspectedTable.getPrimaryKeyColumns());

                // 逻辑删除的判断
                sb.setLength(0);
                sb.append("  and ");
                sb.append(MyBatis3FormattingUtilities.getAliasedEscapedColumnName(this.logicalDeleteColumn));
                sb.append(" = ");
                selectByPrimaryKey.addElement(new TextElement(sb.toString()));

                XmlElement chooseEle = new XmlElement("choose");
                XmlElement whenEle = new XmlElement("when");
                whenEle.addAttribute(new Attribute("test", PARAMETER_LOGICAL_DELETED));
                whenEle.addElement(new TextElement(XmlElementGeneratorTools.generateLogicalDeleteColumnValue(this.logicalDeleteColumn, this.logicalDeleteValue)));
                chooseEle.addElement(whenEle);
                XmlElement otherwiseEle = new XmlElement("otherwise");
                otherwiseEle.addElement(new TextElement(XmlElementGeneratorTools.generateLogicalDeleteColumnValue(this.logicalDeleteColumn, this.logicalUnDeleteValue)));
                chooseEle.addElement(otherwiseEle);
                selectByPrimaryKey.addElement(chooseEle);

                FormatTools.addElementWithBestPosition(document.getRootElement(), selectByPrimaryKey);
            }
        }
        return true;
    }
@Override
    public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
        if (this.logicalDeleteColumn != null) {
            // 1. 逻辑删除ByExample
            XmlElement logicalDeleteByExample = new XmlElement("update");
            logicalDeleteByExample.addAttribute(new Attribute("id", METHOD_LOGICAL_DELETE_BY_EXAMPLE));
            logicalDeleteByExample.addAttribute(new Attribute("parameterType", "map"));
            commentGenerator.addComment(logicalDeleteByExample);

            StringBuilder sb = new StringBuilder();
            sb.append("update ");
            sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
            sb.append(" set ");
            // 更新逻辑删除字段
            sb.append(this.logicalDeleteColumn.getActualColumnName());
            sb.append(" = ");
            sb.append(XmlElementGeneratorTools.generateLogicalDeleteColumnValue(this.logicalDeleteColumn, this.logicalDeleteValue));

            logicalDeleteByExample.addElement(new TextElement(sb.toString()));

            logicalDeleteByExample.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));
            document.getRootElement().addElement(logicalDeleteByExample);
            logger.debug("itfsw(逻辑删除插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加方法logicalDeleteByExample的实现。");
            // hook
            PluginTools.getHook(ILogicalDeletePluginHook.class).sqlMapLogicalDeleteByExampleElementGenerated(document, logicalDeleteByExample, this.logicalDeleteColumn, this.logicalDeleteValue, introspectedTable);

            // 2. 判断是否有主键，生成主键删除方法
            if (introspectedTable.hasPrimaryKeyColumns()) {
                XmlElement logicalDeleteByPrimaryKey = new XmlElement("update");
                logicalDeleteByPrimaryKey.addAttribute(new Attribute("id", METHOD_LOGICAL_DELETE_BY_PRIMARY_KEY));

                String parameterClass;
                if (introspectedTable.getRules().generatePrimaryKeyClass()) {
                    parameterClass = introspectedTable.getPrimaryKeyType();
                } else {
                    // PK fields are in the base class. If more than on PK
                    // field, then they are coming in a map.
                    if (introspectedTable.getPrimaryKeyColumns().size() > 1) {
                        parameterClass = "map";
                    } else {
                        parameterClass = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType().toString();
                    }
                }
                logicalDeleteByPrimaryKey.addAttribute(new Attribute("parameterType", parameterClass));
                commentGenerator.addComment(logicalDeleteByPrimaryKey);

                StringBuilder sb1 = new StringBuilder();
                sb1.append("update ");
                sb1.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
                sb1.append(" set ");
                // 更新逻辑删除字段
                sb1.append(this.logicalDeleteColumn.getActualColumnName());
                sb1.append(" = ");
                sb1.append(XmlElementGeneratorTools.generateLogicalDeleteColumnValue(this.logicalDeleteColumn, this.logicalDeleteValue));

                logicalDeleteByPrimaryKey.addElement(new TextElement(sb1.toString()));

                XmlElementGeneratorTools.generateWhereByPrimaryKeyTo(logicalDeleteByPrimaryKey, introspectedTable.getPrimaryKeyColumns());

                document.getRootElement().addElement(logicalDeleteByPrimaryKey);
                logger.debug("itfsw(逻辑删除插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加方法logicalDeleteByPrimaryKey的实现。");
                // hook
                PluginTools.getHook(ILogicalDeletePluginHook.class).sqlMapLogicalDeleteByPrimaryKeyElementGenerated(document, logicalDeleteByPrimaryKey, this.logicalDeleteColumn, this.logicalDeleteValue, introspectedTable);


                // 3. 增强selectByPrimaryKey
                XmlElement selectByPrimaryKey = new XmlElement("select");
                commentGenerator.addComment(selectByPrimaryKey);

                selectByPrimaryKey.addAttribute(new Attribute("id", METHOD_SELECT_BY_PRIMARY_KEY_WITH_LOGICAL_DELETE));
                if (introspectedTable.getRules().generateResultMapWithBLOBs()) {
                    selectByPrimaryKey.addAttribute(new Attribute("resultMap", introspectedTable.getResultMapWithBLOBsId()));
                } else {
                    selectByPrimaryKey.addAttribute(new Attribute("resultMap", introspectedTable.getBaseResultMapId()));
                }

                selectByPrimaryKey.addAttribute(new Attribute("parameterType", "map"));

                sb = new StringBuilder();
                sb.append("select ");

                if (stringHasValue(introspectedTable.getSelectByPrimaryKeyQueryId())) {
                    sb.append('\'');
                    sb.append(introspectedTable.getSelectByPrimaryKeyQueryId());
                    sb.append("' as QUERYID,");
                }
                selectByPrimaryKey.addElement(new TextElement(sb.toString()));
                selectByPrimaryKey.addElement(XmlElementGeneratorTools.getBaseColumnListElement(introspectedTable));
                if (introspectedTable.hasBLOBColumns()) {
                    selectByPrimaryKey.addElement(new TextElement(","));
                    selectByPrimaryKey.addElement(XmlElementGeneratorTools.getBlobColumnListElement(introspectedTable));
                }

                sb.setLength(0);
                sb.append("from ");
                sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
                selectByPrimaryKey.addElement(new TextElement(sb.toString()));

                XmlElementGeneratorTools.generateWhereByPrimaryKeyTo(selectByPrimaryKey, introspectedTable.getPrimaryKeyColumns());

                // 逻辑删除的判断
                sb.setLength(0);
                sb.append("  and ");
                sb.append(MyBatis3FormattingUtilities.getAliasedEscapedColumnName(this.logicalDeleteColumn));
                sb.append(" = ");
                selectByPrimaryKey.addElement(new TextElement(sb.toString()));

                XmlElement chooseEle = new XmlElement("choose");
                XmlElement whenEle = new XmlElement("when");
                whenEle.addAttribute(new Attribute("test", PARAMETER_LOGICAL_DELETED));
                whenEle.addElement(new TextElement(XmlElementGeneratorTools.generateLogicalDeleteColumnValue(this.logicalDeleteColumn, this.logicalDeleteValue)));
                chooseEle.addElement(whenEle);
                XmlElement otherwiseEle = new XmlElement("otherwise");
                otherwiseEle.addElement(new TextElement(XmlElementGeneratorTools.generateLogicalDeleteColumnValue(this.logicalDeleteColumn, this.logicalUnDeleteValue)));
                chooseEle.addElement(otherwiseEle);
                selectByPrimaryKey.addElement(chooseEle);

                FormatTools.addElementWithBestPosition(document.getRootElement(), selectByPrimaryKey);
            }
        }
        return true;
    }
@ Override public boolean sqlMapDocumentGenerated ( Document document , IntrospectedTable introspectedTable ) { if ( this . logicalDeleteColumn != null ) { // 1. 逻辑删除ByExample XmlElement logicalDeleteByExample = new XmlElement ( "update" ) ; logicalDeleteByExample . addAttribute ( new Attribute ( "id" , METHOD_LOGICAL_DELETE_BY_EXAMPLE ) ) ; logicalDeleteByExample . addAttribute ( new Attribute ( "parameterType" , "map" ) ) ; commentGenerator . addComment ( logicalDeleteByExample ) ; StringBuilder sb = new StringBuilder ( ) ; sb . append ( "update " ) ; sb . append ( introspectedTable . getAliasedFullyQualifiedTableNameAtRuntime ( ) ) ; sb . append ( " set " ) ; // 更新逻辑删除字段 sb . append ( this . logicalDeleteColumn . getActualColumnName ( ) ) ; sb . append ( " = " ) ; sb . append ( XmlElementGeneratorTools . generateLogicalDeleteColumnValue ( this . logicalDeleteColumn , this . logicalDeleteValue ) ) ; logicalDeleteByExample . addElement ( new TextElement ( sb . toString ( ) ) ) ; logicalDeleteByExample . addElement ( XmlElementGeneratorTools . getUpdateByExampleIncludeElement ( introspectedTable ) ) ; document . getRootElement ( ) . addElement ( logicalDeleteByExample ) ; logger . debug ( "itfsw(逻辑删除插件):" + introspec e Table.getMyBatis3 X mlMapperFileName() + "增加方法lo g i a DeleteByExample的实现。"); // hook PluginTools . getHook ( ILogicalDeletePluginHook . class ) . sqlMapLogicalDeleteByExampleElementGenerated ( document , logicalDeleteByExample , this . logicalDeleteColumn , this . logicalDeleteValue , introspectedTable ) ; // 2. 判断是否有主键，生成主键删除方法 if ( introspectedTable . hasPrimaryKeyColumns ( ) ) { XmlElement logicalDeleteByPrimaryKey = new XmlElement ( "update" ) ; logicalDeleteByPrimaryKey . addAttribute ( new Attribute ( "id" , METHOD_LOGICAL_DELETE_BY_PRIMARY_KEY ) ) ; String parameterClass ; if ( introspectedTable . getRules ( ) . generatePrimaryKeyClass ( ) ) { parameterClass = introspectedTable . getPrimaryKeyType ( ) ; } else { // PK fields are in the base class. If more than on PK // field, then they are coming in a map. if ( introspectedTable . getPrimaryKeyColumns ( ) . size ( ) > 1 ) { parameterClass = "map" ; } else { parameterClass = introspectedTable . getPrimaryKeyColumns ( ) . get ( 0 ) . getFullyQualifiedJavaType ( ) . toString ( ) ; } } logicalDeleteByPrimaryKey . addAttribute ( new Attribute ( "parameterType" , parameterClass ) ) ; commentGenerator . addComment ( logicalDeleteByPrimaryKey ) ; StringBuilder sb1 = new StringBuilder ( ) ; sb1 . append ( "update " ) ; sb1 . append ( introspectedTable . getAliasedFullyQualifiedTableNameAtRuntime ( ) ) ; sb1 . append ( " set " ) ; // 更新逻辑删除字段 sb1 . append ( this . logicalDeleteColumn . getActualColumnName ( ) ) ; sb1 . append ( " = " ) ; sb1 . append ( XmlElementGeneratorTools . generateLogicalDeleteColumnValue ( this . logicalDeleteColumn , this . logicalDeleteValue ) ) ; logicalDeleteByPrimaryKey . addElement ( new TextElement ( sb1 . toString ( ) ) ) ; XmlElementGeneratorTools . generateWhereByPrimaryKeyTo ( logicalDeleteByPrimaryKey , introspectedTable . getPrimaryKeyColumns ( ) ) ; document . getRootElement ( ) . addElement ( logicalDeleteByPrimaryKey ) ; logger . debug ( "itfsw(逻辑删除插件):" + introspec e Table.getMyBatis3 X mlMapperFileName() + "增加方法lo g i a DeleteByPrimaryKey的实现。"); // hook PluginTools . getHook ( ILogicalDeletePluginHook . class ) . sqlMapLogicalDeleteByPrimaryKeyElementGenerated ( document , logicalDeleteByPrimaryKey , this . logicalDeleteColumn , this . logicalDeleteValue , introspectedTable ) ; // 3. 增强selectByPrimaryKey XmlElement selectByPrimaryKey = new XmlElement ( "select" ) ; commentGenerator . addComment ( selectByPrimaryKey ) ; selectByPrimaryKey . addAttribute ( new Attribute ( "id" , METHOD_SELECT_BY_PRIMARY_KEY_WITH_LOGICAL_DELETE ) ) ; if ( introspectedTable . getRules ( ) . generateResultMapWithBLOBs ( ) ) { selectByPrimaryKey . addAttribute ( new Attribute ( "resultMap" , introspectedTable . getResultMapWithBLOBsId ( ) ) ) ; } else { selectByPrimaryKey . addAttribute ( new Attribute ( "resultMap" , introspectedTable . getBaseResultMapId ( ) ) ) ; } selectByPrimaryKey . addAttribute ( new Attribute ( "parameterType" , "map" ) ) ; sb = new StringBuilder ( ) ; sb . append ( "select " ) ; if ( stringHasValue ( introspectedTable . getSelectByPrimaryKeyQueryId ( ) ) ) { sb . append ( ' ' ) ; sb . append ( introspectedTable . getSelectByPrimaryKeyQueryId ( ) ) ; sb . append ( "' as QUERYID," ) ; } selectByPrimaryKey . addElement ( new TextElement ( sb . toString ( ) ) ) ; selectByPrimaryKey . addElement ( XmlElementGeneratorTools . getBaseColumnListElement ( introspectedTable ) ) ; if ( introspectedTable . hasBLOBColumns ( ) ) { selectByPrimaryKey . addElement ( new TextElement ( "," ) ) ; selectByPrimaryKey . addElement ( XmlElementGeneratorTools . getBlobColumnListElement ( introspectedTable ) ) ; } sb . setLength ( 0 ) ; sb . append ( "from " ) ; sb . append ( introspectedTable . getAliasedFullyQualifiedTableNameAtRuntime ( ) ) ; selectByPrimaryKey . addElement ( new TextElement ( sb . toString ( ) ) ) ; XmlElementGeneratorTools . generateWhereByPrimaryKeyTo ( selectByPrimaryKey , introspectedTable . getPrimaryKeyColumns ( ) ) ; // 逻辑删除的判断 sb . setLength ( 0 ) ; sb . append ( "  and " ) ; sb . append ( MyBatis3FormattingUtilities . getAliasedEscapedColumnName ( this . logicalDeleteColumn ) ) ; sb . append ( " = " ) ; selectByPrimaryKey . addElement ( new TextElement ( sb . toString ( ) ) ) ; XmlElement chooseEle = new XmlElement ( "choose" ) ; XmlElement whenEle = new XmlElement ( "when" ) ; whenEle . addAttribute ( new Attribute ( "test" , PARAMETER_LOGICAL_DELETED ) ) ; whenEle . addElement ( new TextElement ( XmlElementGeneratorTools . generateLogicalDeleteColumnValue ( this . logicalDeleteColumn , this . logicalDeleteValue ) ) ) ; chooseEle . addElement ( whenEle ) ; XmlElement otherwiseEle = new XmlElement ( "otherwise" ) ; otherwiseEle . addElement ( new TextElement ( XmlElementGeneratorTools . generateLogicalDeleteColumnValue ( this . logicalDeleteColumn , this . logicalUnDeleteValue ) ) ) ; chooseEle . addElement ( otherwiseEle ) ; selectByPrimaryKey . addElement ( chooseEle ) ; FormatTools . addElementWithBestPosition ( document . getRootElement ( ) , selectByPrimaryKey ) ; } } return true ; }
SQL Map Methods 生成
具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
@param document
@param introspectedTable
@return
SQL Map Methods 生成 具体执行顺序 http : // www . mybatis . org / generator / reference / pluggingIn . html