【Mybatis】Mybatis-Generator-Oracle-批量新增

系列

  1. 【Mybatis】Mybatis-Generator-Postgresql返回主鍵插件
  2. 【Mybatis】Mybatis-Generator-Oracle返回主鍵插件
  3. 【Mybatis】Mybatis-Generator-batchDelete(批量刪除)方法插件
  4. 【Mybatis】Mybatis-Generator-Postgresql&Mysql(批量新增)方法插件
  5. 【Mybatis】Mybatis-Generator-Oracle(批量新增)方法插件
  6. 【Mybatis】Mybatis-Generator-Mysql(批量更新)方法插件
  7. 【Mybatis】Mybatis-Generator-Postgresql(批量更新)方法插件
  8. 【Mybatis】Mybatis-Generator-Oracle(批量更新)方法插件
  9. 【Mybatis】Mybatis-Generator-Tool 生成Java和Xml的Method工具類

Github

地址:https://github.com/ithuhui/hui-mybatis-generator-plugins
分支:master
位置:com.hui.mybatis.plugins

Note

Oracle批量插入方法插件。有需要的上我github看看

github上針對mybatis-generator還有很多不同類型的插件,我實在懶的看,而且太多複雜的功能,我簡單點搞自己需要的。

Code

maven

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.6</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.3.7</version>
    </dependency>
</dependencies>

mybatis-implement

<insert id="batchInsert" parameterType="java.util.List">
    insert into T_HUI_ORDER (
    ORDER_ID,
    ORDER_NAME,
    PRODUCT_ID,
    BUY_QUANTITY,
    CREATED_TIME
    )
    (
    <foreach collection="recordList" index="index" item="item" separator="union all">
      (
      select
      #{item.orderId,jdbcType=VARCHAR},
      #{item.orderName,jdbcType=VARCHAR},
      #{item.productId,jdbcType=VARCHAR},
      #{item.buyQuantity,jdbcType=DECIMAL},
      #{item.createdTime,jdbcType=TIMESTAMP}
      from dual
      )
    </foreach>
    )
  </insert>

plugin-code

/**
 * <b><code>OracleBatchInsertPlugin</code></b>
 * <p/>
 * Description:
 * <p/>
 * <b>Creation Time:</b> 2018/12/15 1:12.
 *
 * @author HuWeihui
 */
public class OracleBatchInsertPlugin extends PluginAdapter {

    private final static String BATCH_INSERT = "batchInsert";

    private final static String PARAMETER_NAME = "recordList";

    @Override
    public boolean validate(List<String> list) {
        return true;
    }


    @Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        if (introspectedTable.getTargetRuntime() == IntrospectedTable.TargetRuntime.MYBATIS3) {
            MethodGeneratorTool.defaultBatchInsertOrUpdateMethodGen(MethodGeneratorTool.INSERT, interfaze, introspectedTable, context);
        }
        return super.clientGenerated(interfaze, topLevelClass, introspectedTable);
    }

    @Override
    public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
        if (introspectedTable.getTargetRuntime().equals(IntrospectedTable.TargetRuntime.MYBATIS3)) {
            addSqlMapper(document, introspectedTable);
        }
        return super.sqlMapDocumentGenerated(document, introspectedTable);
    }


    private void addSqlMapper(Document document, IntrospectedTable introspectedTable) {
        //1.Batchinsert
        XmlElement baseElement = SqlMapperGeneratorTool.baseElementGenerator(SqlMapperGeneratorTool.INSERT,
                BATCH_INSERT,
                FullyQualifiedJavaType.getNewListInstance());

        XmlElement foreachElement = SqlMapperGeneratorTool.baseForeachElementGenerator(PARAMETER_NAME,
                "item",
                "index",
                "union all");


        //tableName
        baseElement.addElement(new TextElement(String.format("insert into %s (", introspectedTable.getFullyQualifiedTableNameAtRuntime())));

        foreachElement.addElement(new TextElement("("));
        foreachElement.addElement(new TextElement("select"));

        for (int i = 0; i < introspectedTable.getAllColumns().size(); i++) {
            //column信息
            IntrospectedColumn introspectedColumn = introspectedTable.getAllColumns().get(i);

            String columnInfo = "";
            String valueInfo = "";

            columnInfo = introspectedColumn.getActualColumnName();
            valueInfo = MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, "item.");
            if (introspectedColumn.isIdentity()) {
                String nextval = introspectedTable.getFullyQualifiedTableNameAtRuntime()+"_SEQUENCE.nextval" ;
                valueInfo = nextval;
            }
            if (i != (introspectedTable.getAllColumns().size() - 1)) {
                columnInfo += (",");
                valueInfo += ",";
            }
            baseElement.addElement(new TextElement(columnInfo));
            foreachElement.addElement(new TextElement(valueInfo));
        }
        foreachElement.addElement(new TextElement("from dual"));
        foreachElement.addElement(new TextElement(")"));


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

        baseElement.addElement(new TextElement("("));
        baseElement.addElement(foreachElement);
        baseElement.addElement(new TextElement(")"));

        //3.parent Add
        document.getRootElement().addElement(baseElement);
    }

}

plugin-common-tool

插件通用方法/工具類,上面生成方法基於了一個工具類,下面博客有源碼,真正需要的可以看看github

【Mybatis】Mybatis-Generator-Tool 生成Java和Xml的Method工具類

使用方法

generatorConfig.xml

<!--Oracle批量插入-->
<plugin type="com.hui.mybatis.plugins.OracleBatchInsertPlugin"/>

pom.xml

<build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>${mybatis.generator}</version>
                <configuration>
                    <!-- 配置文件 -->
                    <configurationFile>
                        ${basedir}/src/main/resources/generator/generatorConfig.xml
                    </configurationFile>
                    <!-- 允許移動生成的文件 -->
                    <verbose>true</verbose>
                    <!-- 是否覆蓋 -->
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <!-- mybatis-generator-core -->
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>${mybatis.generator}</version>
                    </dependency>

                    <!--插件導入-->
                    <dependency>
                        <groupId>com.hui.mybatis.plugins</groupId>
                        <artifactId>hui-mybatis-plugins</artifactId>
                        <version>0.0.1-SNAPSHOT</version>
                    </dependency>

                    <!--Mysql-->
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.44</version>
                    </dependency>

                    <!--ORACLE-->
                    <dependency>
                        <groupId>com.oracle</groupId>
                        <artifactId>ojdbc6</artifactId>
                        <version>11.1.0.7.0</version>
                    </dependency>

                    <!--PG SQL-->
                    <dependency>
                        <groupId>org.postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>${postgresql.version}</version>
                    </dependency>

                </dependencies>
            </plugin>
        </plugins>
    </build>

作者

 作者:HuHui
 轉載:歡迎一起討論web和大數據問題,轉載請註明作者和原文鏈接,感謝
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章