src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
private void generateXmlElementWithSelective(Document document, IntrospectedTable introspectedTable) {
        List<IntrospectedColumn> columns = ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getAllColumns());

        // ====================================== upsertSelective ======================================
        XmlElement insertEle = new XmlElement("insert");
        insertEle.addAttribute(new Attribute("id", METHOD_UPSERT_SELECTIVE));
        // 添加注释(!!!必须添加注释，overwrite覆盖生成时，@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点，例子：@mbg.generated)
        commentGenerator.addComment(insertEle);

        // 参数类型
        insertEle.addAttribute(new Attribute("parameterType", introspectedTable.getRules().calculateAllFieldsClass().getFullyQualifiedName()));

        // 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
        XmlElementGeneratorTools.useGeneratedKeys(insertEle, introspectedTable);

        // insert
        insertEle.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
        XmlElement insertColumnsEle = XmlElementGeneratorTools.generateKeysSelective(columns);
        insertEle.addElement(insertColumnsEle);
        insertEle.addElement(new TextElement("values"));
        XmlElement insertValuesEle = XmlElementGeneratorTools.generateValuesSelective(columns);
        insertEle.addElement(insertValuesEle);
        insertEle.addElement(new TextElement("on duplicate key update "));
        // set
        XmlElement setsEle = XmlElementGeneratorTools.generateSetsSelective(columns);
        insertEle.addElement(setsEle);

        // hook
        if (PluginTools.getHook(IUpsertPluginHook.class).sqlMapUpsertSelectiveElementGenerated(insertEle, columns, insertColumnsEle, insertValuesEle, setsEle, introspectedTable)) {
            document.getRootElement().addElement(insertEle);
            logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。");
        }

        if (this.allowMultiQueries) {
            // ====================================== upsertByExampleSelective ======================================
            XmlElement updateEle = new XmlElement("update");
            updateEle.addAttribute(new Attribute("id", METHOD_UPSERT_BY_EXAMPLE_SELECTIVE));
            // 参数类型
            updateEle.addAttribute(new Attribute("parameterType", "map"));
            // 添加注释(!!!必须添加注释，overwrite覆盖生成时，@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点，例子：@mbg.generated)
            commentGenerator.addComment(updateEle);

            // 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
            // XmlElementGeneratorTools.useGeneratedKeys(updateEle, introspectedTable, "record.");
            // upsertByExample 先执行的update，所以没法获取id

            // update
            updateEle.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
            updateEle.addElement(new TextElement("set"));
            // set 操作增加增量插件支持
            setsEle = XmlElementGeneratorTools.generateSetsSelective(columns, "record.");
            updateEle.addElement(setsEle);

            // update where
            updateEle.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));

            // multiQueries
            updateEle.addElement(new TextElement(";"));

            // insert
            updateEle.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
            insertColumnsEle = XmlElementGeneratorTools.generateKeysSelective(columns, "record.");
            updateEle.addElement(insertColumnsEle);

            insertValuesEle = XmlElementGeneratorTools.generateValuesSelective(columns, "record.", false);
            // 查询值
            this.generateExistsClause(introspectedTable, updateEle, Arrays.asList(insertValuesEle));

            // hook
            if (PluginTools.getHook(IUpsertPluginHook.class).sqlMapUpsertByExampleSelectiveElementGenerated(updateEle, columns, insertColumnsEle, insertValuesEle, setsEle, introspectedTable)) {
                document.getRootElement().addElement(updateEle);
                logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。");
            }
        }
    }
private void generateXmlElementWithSelective(Document document, IntrospectedTable introspectedTable) {
        List<IntrospectedColumn> columns = ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getAllColumns());

        // ====================================== upsertSelective ======================================
        XmlElement insertEle = new XmlElement("insert");
        insertEle.addAttribute(new Attribute("id", METHOD_UPSERT_SELECTIVE));
        // 添加注释(!!!必须添加注释，overwrite覆盖生成时，@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点，例子：@mbg.generated)
        commentGenerator.addComment(insertEle);

        // 参数类型
        insertEle.addAttribute(new Attribute("parameterType", introspectedTable.getRules().calculateAllFieldsClass().getFullyQualifiedName()));

        // 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
        XmlElementGeneratorTools.useGeneratedKeys(insertEle, introspectedTable);

        // insert
        insertEle.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
        XmlElement insertColumnsEle = XmlElementGeneratorTools.generateKeysSelective(columns);
        insertEle.addElement(insertColumnsEle);
        insertEle.addElement(new TextElement("values"));
        XmlElement insertValuesEle = XmlElementGeneratorTools.generateValuesSelective(columns);
        insertEle.addElement(insertValuesEle);
        insertEle.addElement(new TextElement("on duplicate key update "));
        // set
        XmlElement setsEle = XmlElementGeneratorTools.generateSetsSelective(columns);
        insertEle.addElement(setsEle);

        // hook
        if (PluginTools.getHook(IUpsertPluginHook.class).sqlMapUpsertSelectiveElementGenerated(insertEle, columns, insertColumnsEle, insertValuesEle, setsEle, introspectedTable)) {
            document.getRootElement().addElement(insertEle);
            logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。");
        }

        if (this.allowMultiQueries) {
            // ====================================== upsertByExampleSelective ======================================
            XmlElement updateEle = new XmlElement("update");
            updateEle.addAttribute(new Attribute("id", METHOD_UPSERT_BY_EXAMPLE_SELECTIVE));
            // 参数类型
            updateEle.addAttribute(new Attribute("parameterType", "map"));
            // 添加注释(!!!必须添加注释，overwrite覆盖生成时，@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点，例子：@mbg.generated)
            commentGenerator.addComment(updateEle);

            // 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
            // XmlElementGeneratorTools.useGeneratedKeys(updateEle, introspectedTable, "record.");
            // upsertByExample 先执行的update，所以没法获取id

            // update
            updateEle.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
            updateEle.addElement(new TextElement("set"));
            // set 操作增加增量插件支持
            setsEle = XmlElementGeneratorTools.generateSetsSelective(columns, "record.");
            updateEle.addElement(setsEle);

            // update where
            updateEle.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));

            // multiQueries
            updateEle.addElement(new TextElement(";"));

            // insert
            updateEle.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
            insertColumnsEle = XmlElementGeneratorTools.generateKeysSelective(columns, "record.");
            updateEle.addElement(insertColumnsEle);

            insertValuesEle = XmlElementGeneratorTools.generateValuesSelective(columns, "record.", false);
            // 查询值
            this.generateExistsClause(introspectedTable, updateEle, Arrays.asList(insertValuesEle));

            // hook
            if (PluginTools.getHook(IUpsertPluginHook.class).sqlMapUpsertByExampleSelectiveElementGenerated(updateEle, columns, insertColumnsEle, insertValuesEle, setsEle, introspectedTable)) {
                document.getRootElement().addElement(updateEle);
                logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。");
            }
        }
    }
private void generateXmlElementWithSelective ( Document document , IntrospectedTable introspectedTable ) { List < IntrospectedColumn > columns = ListUtilities . removeGeneratedAlwaysColumns ( introspectedTable . getAllColumns ( ) ) ; // ====================================== upsertSelective ====================================== XmlElement insertEle = new XmlElement ( "insert" ) ; insertEle . addAttribute ( new Attribute ( "id" , METHOD_UPSERT_SELECTIVE ) ) ; // 添加注释(!!!必须添加注释，overwrite覆盖生成时，@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点，例子：@mbg.generated) commentGenerator . addComment ( insertEle ) ; // 参数类型 insertEle . addAttribute ( new Attribute ( "parameterType" , introspectedTable . getRules ( ) . calculateAllFieldsClass ( ) . getFullyQualifiedName ( ) ) ) ; // 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer XmlElementGeneratorTools . useGeneratedKeys ( insertEle , introspectedTable ) ; // insert insertEle . addElement ( new TextElement ( "insert into " + introspectedTable . getFullyQualifiedTableNameAtRuntime ( ) ) ) ; XmlElement insertColumnsEle = XmlElementGeneratorTools . generateKeysSelective ( columns ) ; insertEle . addElement ( insertColumnsEle ) ; insertEle . addElement ( new TextElement ( "values" ) ) ; XmlElement insertValuesEle = XmlElementGeneratorTools . generateValuesSelective ( columns ) ; insertEle . addElement ( insertValuesEle ) ; insertEle . addElement ( new TextElement ( "on duplicate key update " ) ) ; // set XmlElement setsEle = XmlElementGeneratorTools . generateSetsSelective ( columns ) ; insertEle . addElement ( setsEle ) ; // hook if ( PluginTools . getHook ( IUpsertPluginHook . class ) . sqlMapUpsertSelectiveElementGenerated ( insertEle , columns , insertColumnsEle , insertValuesEle , setsEle , introspectedTable ) ) { document . getRootElement ( ) . addElement ( insertEle ) ; logger . debug ( "itfsw(存在即更新插件):" + introspecte T ble.getMyBatis3Xm l MapperFileName() + "增加upsert S e e tive实现方法。"); } if ( this . allowMultiQueries ) { // ====================================== upsertByExampleSelective ====================================== XmlElement updateEle = new XmlElement ( "update" ) ; updateEle . addAttribute ( new Attribute ( "id" , METHOD_UPSERT_BY_EXAMPLE_SELECTIVE ) ) ; // 参数类型 updateEle . addAttribute ( new Attribute ( "parameterType" , "map" ) ) ; // 添加注释(!!!必须添加注释，overwrite覆盖生成时，@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点，例子：@mbg.generated) commentGenerator . addComment ( updateEle ) ; // 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer // XmlElementGeneratorTools.useGeneratedKeys(updateEle, introspectedTable, "record."); // upsertByExample 先执行的update，所以没法获取id // update updateEle . addElement ( new TextElement ( "update " + introspectedTable . getAliasedFullyQualifiedTableNameAtRuntime ( ) ) ) ; updateEle . addElement ( new TextElement ( "set" ) ) ; // set 操作增加增量插件支持 setsEle = XmlElementGeneratorTools . generateSetsSelective ( columns , "record." ) ; updateEle . addElement ( setsEle ) ; // update where updateEle . addElement ( XmlElementGeneratorTools . getUpdateByExampleIncludeElement ( introspectedTable ) ) ; // multiQueries updateEle . addElement ( new TextElement ( ";" ) ) ; // insert updateEle . addElement ( new TextElement ( "insert into " + introspectedTable . getFullyQualifiedTableNameAtRuntime ( ) ) ) ; insertColumnsEle = XmlElementGeneratorTools . generateKeysSelective ( columns , "record." ) ; updateEle . addElement ( insertColumnsEle ) ; insertValuesEle = XmlElementGeneratorTools . generateValuesSelective ( columns , "record." , false ) ; // 查询值 this . generateExistsClause ( introspectedTable , updateEle , Arrays . asList ( insertValuesEle ) ) ; // hook if ( PluginTools . getHook ( IUpsertPluginHook . class ) . sqlMapUpsertByExampleSelectiveElementGenerated ( updateEle , columns , insertColumnsEle , insertValuesEle , setsEle , introspectedTable ) ) { document . getRootElement ( ) . addElement ( updateEle ) ; logger . debug ( "itfsw(存在即更新插件):" + introspecte T ble.getMyBatis3Xm l MapperFileName() + "增加upsert S e e tive实现方法。"); } } }
当Selective情况
@param document
@param introspectedTable
当Selective情况