src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
private void generateBatchXmlElementSelective(Document document, IntrospectedTable introspectedTable) {
        XmlElement insertEle = new XmlElement("insert");
        insertEle.addAttribute(new Attribute("id", METHOD_BATCH_UPSERT_SELECTIVE));
        // 添加注释(!!!必须添加注释，overwrite覆盖生成时，@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点，例子：@mbg.generated)
        commentGenerator.addComment(insertEle);

        // 参数类型
        insertEle.addAttribute(new Attribute("parameterType", "map"));

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

        // insert
        insertEle.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime() + " ("));

        XmlElement foreachInsertColumns = new XmlElement("foreach");
        foreachInsertColumns.addAttribute(new Attribute("collection", "selective"));
        foreachInsertColumns.addAttribute(new Attribute("item", "column"));
        foreachInsertColumns.addAttribute(new Attribute("separator", ","));
        foreachInsertColumns.addElement(new TextElement("${column.escapedColumnName}"));

        insertEle.addElement(foreachInsertColumns);

        insertEle.addElement(new TextElement(")"));

        // values
        insertEle.addElement(new TextElement("values"));

        // foreach values
        XmlElement foreachValues = new XmlElement("foreach");
        foreachValues.addAttribute(new Attribute("collection", "list"));
        foreachValues.addAttribute(new Attribute("item", "item"));
        foreachValues.addAttribute(new Attribute("separator", ","));

        foreachValues.addElement(new TextElement("("));

        // foreach 所有插入的列，比较是否存在
        XmlElement foreachInsertColumnsCheck = new XmlElement("foreach");
        foreachInsertColumnsCheck.addAttribute(new Attribute("collection", "selective"));
        foreachInsertColumnsCheck.addAttribute(new Attribute("item", "column"));
        foreachInsertColumnsCheck.addAttribute(new Attribute("separator", ","));

        // 所有表字段
        List<IntrospectedColumn> columns = ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getAllColumns());
        for (int i = 0; i < columns.size(); i++) {
            IntrospectedColumn introspectedColumn = columns.get(i);
            XmlElement check = new XmlElement("if");
            check.addAttribute(new Attribute("test", "'" + introspectedColumn.getActualColumnName() + "'.toString() == column.value"));
            check.addElement(new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, "item.")));

            foreachInsertColumnsCheck.addElement(check);
        }
        foreachValues.addElement(foreachInsertColumnsCheck);

        foreachValues.addElement(new TextElement(")"));

        insertEle.addElement(foreachValues);

        insertEle.addElement(new TextElement("on duplicate key update "));
        // set
        XmlElement foreachSetColumns = new XmlElement("foreach");
        insertEle.addElement(foreachSetColumns);
        foreachSetColumns.addAttribute(new Attribute("collection", "selective"));
        foreachSetColumns.addAttribute(new Attribute("item", "column"));
        foreachSetColumns.addAttribute(new Attribute("separator", ","));
        foreachSetColumns.addElement(new TextElement("${column.escapedColumnName} = values(${column.escapedColumnName})"));

        document.getRootElement().addElement(insertEle);
        logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加batchUpsertSelective实现方法。");
    }
private void generateBatchXmlElementSelective(Document document, IntrospectedTable introspectedTable) {
        XmlElement insertEle = new XmlElement("insert");
        insertEle.addAttribute(new Attribute("id", METHOD_BATCH_UPSERT_SELECTIVE));
        // 添加注释(!!!必须添加注释，overwrite覆盖生成时，@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点，例子：@mbg.generated)
        commentGenerator.addComment(insertEle);

        // 参数类型
        insertEle.addAttribute(new Attribute("parameterType", "map"));

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

        // insert
        insertEle.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime() + " ("));

        XmlElement foreachInsertColumns = new XmlElement("foreach");
        foreachInsertColumns.addAttribute(new Attribute("collection", "selective"));
        foreachInsertColumns.addAttribute(new Attribute("item", "column"));
        foreachInsertColumns.addAttribute(new Attribute("separator", ","));
        foreachInsertColumns.addElement(new TextElement("${column.escapedColumnName}"));

        insertEle.addElement(foreachInsertColumns);

        insertEle.addElement(new TextElement(")"));

        // values
        insertEle.addElement(new TextElement("values"));

        // foreach values
        XmlElement foreachValues = new XmlElement("foreach");
        foreachValues.addAttribute(new Attribute("collection", "list"));
        foreachValues.addAttribute(new Attribute("item", "item"));
        foreachValues.addAttribute(new Attribute("separator", ","));

        foreachValues.addElement(new TextElement("("));

        // foreach 所有插入的列，比较是否存在
        XmlElement foreachInsertColumnsCheck = new XmlElement("foreach");
        foreachInsertColumnsCheck.addAttribute(new Attribute("collection", "selective"));
        foreachInsertColumnsCheck.addAttribute(new Attribute("item", "column"));
        foreachInsertColumnsCheck.addAttribute(new Attribute("separator", ","));

        // 所有表字段
        List<IntrospectedColumn> columns = ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getAllColumns());
        for (int i = 0; i < columns.size(); i++) {
            IntrospectedColumn introspectedColumn = columns.get(i);
            XmlElement check = new XmlElement("if");
            check.addAttribute(new Attribute("test", "'" + introspectedColumn.getActualColumnName() + "'.toString() == column.value"));
            check.addElement(new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, "item.")));

            foreachInsertColumnsCheck.addElement(check);
        }
        foreachValues.addElement(foreachInsertColumnsCheck);

        foreachValues.addElement(new TextElement(")"));

        insertEle.addElement(foreachValues);

        insertEle.addElement(new TextElement("on duplicate key update "));
        // set
        XmlElement foreachSetColumns = new XmlElement("foreach");
        insertEle.addElement(foreachSetColumns);
        foreachSetColumns.addAttribute(new Attribute("collection", "selective"));
        foreachSetColumns.addAttribute(new Attribute("item", "column"));
        foreachSetColumns.addAttribute(new Attribute("separator", ","));
        foreachSetColumns.addElement(new TextElement("${column.escapedColumnName} = values(${column.escapedColumnName})"));

        document.getRootElement().addElement(insertEle);
        logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加batchUpsertSelective实现方法。");
    }
private void generateBatchXmlElementSelective ( Document document , IntrospectedTable introspectedTable ) { XmlElement insertEle = new XmlElement ( "insert" ) ; insertEle . addAttribute ( new Attribute ( "id" , METHOD_BATCH_UPSERT_SELECTIVE ) ) ; // 添加注释(!!!必须添加注释，overwrite覆盖生成时，@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点，例子：@mbg.generated) commentGenerator . addComment ( insertEle ) ; // 参数类型 insertEle . addAttribute ( new Attribute ( "parameterType" , "map" ) ) ; // 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer // XmlElementGeneratorTools.useGeneratedKeys(insertEle, introspectedTable, "list."); // insert insertEle . addElement ( new TextElement ( "insert into " + introspectedTable . getFullyQualifiedTableNameAtRuntime ( ) + " (" ) ) ; XmlElement foreachInsertColumns = new XmlElement ( "foreach" ) ; foreachInsertColumns . addAttribute ( new Attribute ( "collection" , "selective" ) ) ; foreachInsertColumns . addAttribute ( new Attribute ( "item" , "column" ) ) ; foreachInsertColumns . addAttribute ( new Attribute ( "separator" , "," ) ) ; foreachInsertColumns . addElement ( new TextElement ( "${column.escapedColumnName}" ) ) ; insertEle . addElement ( foreachInsertColumns ) ; insertEle . addElement ( new TextElement ( ")" ) ) ; // values insertEle . addElement ( new TextElement ( "values" ) ) ; // foreach values XmlElement foreachValues = new XmlElement ( "foreach" ) ; foreachValues . addAttribute ( new Attribute ( "collection" , "list" ) ) ; foreachValues . addAttribute ( new Attribute ( "item" , "item" ) ) ; foreachValues . addAttribute ( new Attribute ( "separator" , "," ) ) ; foreachValues . addElement ( new TextElement ( "(" ) ) ; // foreach 所有插入的列，比较是否存在 XmlElement foreachInsertColumnsCheck = new XmlElement ( "foreach" ) ; foreachInsertColumnsCheck . addAttribute ( new Attribute ( "collection" , "selective" ) ) ; foreachInsertColumnsCheck . addAttribute ( new Attribute ( "item" , "column" ) ) ; foreachInsertColumnsCheck . addAttribute ( new Attribute ( "separator" , "," ) ) ; // 所有表字段 List < IntrospectedColumn > columns = ListUtilities . removeGeneratedAlwaysColumns ( introspectedTable . getAllColumns ( ) ) ; for ( int i = 0 ; i < columns . size ( ) ; i ++ ) { IntrospectedColumn introspectedColumn = columns . get ( i ) ; XmlElement check = new XmlElement ( "if" ) ; check . addAttribute ( new Attribute ( "test" , "'" + introspectedColumn . getActualColumnName ( ) + "'.toString() == column.value" ) ) ; check . addElement ( new TextElement ( MyBatis3FormattingUtilities . getParameterClause ( introspectedColumn , "item." ) ) ) ; foreachInsertColumnsCheck . addElement ( check ) ; } foreachValues . addElement ( foreachInsertColumnsCheck ) ; foreachValues . addElement ( new TextElement ( ")" ) ) ; insertEle . addElement ( foreachValues ) ; insertEle . addElement ( new TextElement ( "on duplicate key update " ) ) ; // set XmlElement foreachSetColumns = new XmlElement ( "foreach" ) ; insertEle . addElement ( foreachSetColumns ) ; foreachSetColumns . addAttribute ( new Attribute ( "collection" , "selective" ) ) ; foreachSetColumns . addAttribute ( new Attribute ( "item" , "column" ) ) ; foreachSetColumns . addAttribute ( new Attribute ( "separator" , "," ) ) ; foreachSetColumns . addElement ( new TextElement ( "${column.escapedColumnName} = values(${column.escapedColumnName})" ) ) ; document . getRootElement ( ) . addElement ( insertEle ) ; logger . debug ( "itfsw(存在即更新插件):" + introspecte T ble.getMyBatis3Xm l MapperFileName() + "增加batchU p s r Selective实现方法。"); }
批量
@param document
@param introspectedTable
批量