【Mybatis】Mybatis-Generator-Oracle(批量更新)方法插件

版权声明:小哥哥小姐姐们,本文为小博主原创文章,转载请附上博主博文网址,并标注作者谢谢~~。违者必究 https://blog.csdn.net/HuHui_/article/details/85150558

系列

  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工具类

前言

PG批量更新插件,核心贴出来,有需要的看我的GITHUB,准备更新微服务BLOG

github上针对mybatis-generator还有很多不同类型的插件,我实在懒的看,而且太多复杂的功能,我简单点搞自己需要的。

装备

  1. 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>
    

Core-Code

mybatis-implement

 <update id="batchUpdate" parameterType="java.util.List">
    begin
    <foreach collection="recordList" index="index" item="item" separator=";">
      update T_HUI_ORDER
      <set>
         ORDER_ID = #{item.orderId,jdbcType=VARCHAR},
         ORDER_NAME = #{item.orderName,jdbcType=VARCHAR},
         PRODUCT_ID = #{item.productId,jdbcType=VARCHAR},
         BUY_QUANTITY = #{item.buyQuantity,jdbcType=DECIMAL},
         CREATED_TIME = #{item.createdTime,jdbcType=TIMESTAMP}
      </set>
      where ORDER_ID = #{item.orderId,jdbcType=VARCHAR}
    </foreach>
    ;end;
  </update>

plugin-code

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

    private final static String BATCH_UPDATE = "batchUpdate";

    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().equals(IntrospectedTable.TargetRuntime.MYBATIS3)) {
            MethodGeneratorTool.defaultBatchInsertOrUpdateMethodGen(MethodGeneratorTool.UPDATE,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);
    }


    public void addSqlMapper(Document document, IntrospectedTable introspectedTable) {
        String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime();
        List<IntrospectedColumn> columnList = introspectedTable.getAllColumns();
        //primaryKey的JDBC名字
        String primaryKeyName = introspectedTable.getPrimaryKeyColumns().get(0).getActualColumnName();
        //primaryKey的JAVA名字
        String primaryKeyJavaName = introspectedTable.getPrimaryKeyColumns().get(0).getJavaProperty();

        String keyParameterClause = MyBatis3FormattingUtilities.getParameterClause(introspectedTable.getPrimaryKeyColumns().get(0), "item.");
        XmlElement baseElement = SqlMapperGeneratorTool.baseElementGenerator(SqlMapperGeneratorTool.UPDATE,
                BATCH_UPDATE,
                FullyQualifiedJavaType.getNewListInstance());

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

        foreachElement.addElement(new TextElement( String.format("update %s", tableName)));

        XmlElement setElement = new XmlElement("set");

        StringBuilder columnInfo = new StringBuilder();
        StringBuilder valuesInfo = new StringBuilder();

        for (int i = 0; i < columnList.size(); i++) {

            IntrospectedColumn introspectedColumn = columnList.get(i);
            if (introspectedColumn.isIdentity()) {
                continue;
            }
            columnInfo.append(introspectedColumn.getActualColumnName());
            valuesInfo.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, "item."));
            if (i != (columnList.size() - 1)) {
                valuesInfo.append(",");
            }
            String setSql = String.format(" %s = %s" ,columnInfo,valuesInfo);

            setElement.addElement(new TextElement(setSql));

            valuesInfo.delete(0, valuesInfo.length());
            columnInfo.delete(0, columnInfo.length());

        }

        foreachElement.addElement(setElement);

        String whereSql = String.format("where %s = %s",primaryKeyName,keyParameterClause);

        foreachElement.addElement(new TextElement(whereSql));

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

        baseElement.addElement(foreachElement);

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

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

plugin-common-tool

插件通用方法/工具类,上面生成方法基于了一个工具类,下面博客有源码,真正需要的可以看看github

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

使用方法

generatorConfig.xml

<!--MySql批量更新-->
<plugin type="com.hui.mybatis.plugins.OracleBatchUpdatePlugin"/>

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>

Github

https://github.com/ithuhui/hui-mybatis-plugins/tree/master/src/main/java/com/hui/mybatis/plugins

作者

 作者:HuHui
 转载:欢迎一起讨论web和大数据问题,转载请注明作者和原文链接,感谢

猜你喜欢

转载自blog.csdn.net/HuHui_/article/details/85150558