【Mybatis】Mybatis-Generator-Mysql(一括更新)メソッドプラグイン

55088 ワード

記事の目次
  • シリーズ
  • クライテリア
  • 装備
  • Core-Core
  • mybatis-mplement
  • plugin-code
  • plugin-common-tool
  • 使用方法
  • generatoConfig.xml
  • pom.xml
  • Github
  • 著者
  • シリーズ
  • 【Mybatis】Mybatis-Generator-Paost gresqlはメインキープラグイン
  • に戻ります.
  • 【Mybatis】Mybatis-Generator-Oracleは、メインキープラグイン
  • に戻る.
  • 【Mybatis】Mybatis-Generator-batDelete(一括削除)メソッドプラグイン
  • [Mybatis]Mybatis-Generator-Paost gresql&Mysql(一括追加)メソッドプラグイン
  • [Mybatis]Mybatis-Generator-Oracle(一括追加)メソッドプラグイン
  • [Mybatis]Mybatis-Generator-Myysql(一括更新)メソッドプラグイン
  • [Mybatis]Mybatis-Generator-Paost gresql(一括更新)メソッドプラグイン
  • [Mybatis]Mybatis-Generator-Oracle(一括更新)メソッドプラグイン
  • 【Mybatis】Mybatis-Generator-Tool JavaとXmlのMethodツールクラス
  • 前言
    Mysql大量にプラグインを追加して、核心は貼って、必要なのは私のGITHUBを見て、微サービスBLOGを更新するつもりです.
    ギthubではmybatis-generatorに対してまだいろんなタイプのプラグインがあります.本当に怠惰に見ています.そして複雑な機能が多すぎて、私は簡単に自分の必要なものを作ります.
    装備
  • maven
    <dependencies>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.6version>
        dependency>
    
        
        <dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-coreartifactId>
            <version>1.3.7version>
        dependency>
    dependencies>
    
  • Core-Coode
    mybatis-mplement
    <update id="batchUpdate" parameterType="java.util.List">
        update T_HUI_ORDER 
        <trim prefix="set" suffixOverrides=",">
          <trim prefix="ORDER_ID =case ORDER_ID" suffix="end,">
            <foreach collection="recordList" index="index" item="item">
              <if test="item.orderId !=null ">
                when #{item.orderId,jdbcType=VARCHAR} then #{item.orderId,jdbcType=VARCHAR}
              if>
              <if test="item.orderId ==null ">
                when #{item.orderId,jdbcType=VARCHAR} then T_HUI_ORDER.ORDER_ID
              if>
            foreach>
          trim>
          <trim prefix="ORDER_NAME =case ORDER_ID" suffix="end,">
            <foreach collection="recordList" index="index" item="item">
              <if test="item.orderName !=null ">
                when #{item.orderId,jdbcType=VARCHAR} then #{item.orderName,jdbcType=VARCHAR}
              if>
              <if test="item.orderName ==null ">
                when #{item.orderId,jdbcType=VARCHAR} then T_HUI_ORDER.ORDER_NAME
              if>
            foreach>
          trim>
          <trim prefix="PRODUCT_ID =case ORDER_ID" suffix="end,">
            <foreach collection="recordList" index="index" item="item">
              <if test="item.productId !=null ">
                when #{item.orderId,jdbcType=VARCHAR} then #{item.productId,jdbcType=VARCHAR}
              if>
              <if test="item.productId ==null ">
                when #{item.orderId,jdbcType=VARCHAR} then T_HUI_ORDER.PRODUCT_ID
              if>
            foreach>
          trim>
          <trim prefix="BUY_QUANTITY =case ORDER_ID" suffix="end,">
            <foreach collection="recordList" index="index" item="item">
              <if test="item.buyQuantity !=null ">
                when #{item.orderId,jdbcType=VARCHAR} then #{item.buyQuantity,jdbcType=DECIMAL}
              if>
              <if test="item.buyQuantity ==null ">
                when #{item.orderId,jdbcType=VARCHAR} then T_HUI_ORDER.BUY_QUANTITY
              if>
            foreach>
          trim>
          <trim prefix="CREATED_TIME =case ORDER_ID" suffix="end,">
            <foreach collection="recordList" index="index" item="item">
              <if test="item.createdTime !=null ">
                when #{item.orderId,jdbcType=VARCHAR} then #{item.createdTime,jdbcType=TIMESTAMP}
              if>
              <if test="item.createdTime ==null ">
                when #{item.orderId,jdbcType=VARCHAR} then T_HUI_ORDER.CREATED_TIME
              if>
            foreach>
          trim>
        trim>
        where ORDER_ID in(
        <foreach collection="recordList" index="index" item="item" separator=",">
          #{item.orderId,jdbcType=VARCHAR}
        foreach>
        )
      update>
    
    plugin-code
    /**
     * MysqlBatchUpdatePlugin
     * 
     * Description:
     * 
     * Creation Time: 2018/12/10 0:48.
     *
     * @author HuWeihui
     */
    public class MysqlBatchUpdatePlugin 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 primaryKeyParameterClause = MyBatis3FormattingUtilities.getParameterClause(introspectedTable.getPrimaryKeyColumns().get(0), "item.");
    
            //primaryKey JAVA  
            String primaryKeyJavaName = introspectedTable.getPrimaryKeyColumns().get(0).getJavaProperty();
    
    
            XmlElement updateXmlElement = SqlMapperGeneratorTool.baseElementGenerator(SqlMapperGeneratorTool.UPDATE,
                    BATCH_UPDATE,
                    FullyQualifiedJavaType.getNewListInstance());
    
            updateXmlElement.addElement(new TextElement(String.format("update %s ", tableName)));
    
            XmlElement trimElement = SqlMapperGeneratorTool.baseTrimElement("set", null, ",");
    
            for (int i = 0; i < columnList.size(); i++) {
    
                IntrospectedColumn introspectedColumn = columnList.get(i);
    
                String columnName = introspectedColumn.getActualColumnName();
    
                String columnJavaTypeName = introspectedColumn.getJavaProperty("item.");
    
                String parameterClause = MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, "item.");
    
    
                if (introspectedColumn.isIdentity()) {
                    continue;
                }
    
                String ifSql = String.format("when %s then %s", primaryKeyParameterClause, parameterClause);
                XmlElement ifElement = SqlMapperGeneratorTool.baseIfJudgeElementGen(columnJavaTypeName, ifSql, false);
    
                String ifNullSql = String.format("when %s then %s", primaryKeyParameterClause, tableName + "." + columnName);
                XmlElement ifNullElement = SqlMapperGeneratorTool.baseIfJudgeElementGen(columnJavaTypeName, ifNullSql, true);
    
    
                XmlElement foreachElement = SqlMapperGeneratorTool.baseForeachElementGenerator(PARAMETER_NAME, "item", "index", null);
                foreachElement.addElement(ifElement);
                foreachElement.addElement(ifNullElement);
    
                XmlElement caseTrimElement = SqlMapperGeneratorTool.baseTrimElement(columnName + " =case " + primaryKeyName, "end,", null);
                caseTrimElement.addElement(foreachElement);
    
                trimElement.addElement(caseTrimElement);
            }
    
            updateXmlElement.addElement(trimElement);
    
            XmlElement foreachElement = SqlMapperGeneratorTool.baseForeachElementGenerator(PARAMETER_NAME,
                    "item",
                    "index",
                    ",");
            foreachElement.addElement(new TextElement(primaryKeyParameterClause));
    
            updateXmlElement.addElement(new TextElement(String.format("where %s in(", primaryKeyName)));
    
            updateXmlElement.addElement(foreachElement);
    
            updateXmlElement.addElement(new TextElement(")"));
    
            document.getRootElement().addElement(updateXmlElement);
        }
    }
    
    
    plugin-common-tool
    プラグインの一般的な方法/ツール、上記の生成方法はツールの種類に基づいて、下のブログはソースコードがあります.本当に必要なのはgithubを見ることができます.
    【Mybatis】Mybatis-Generator-Taol JavaとXmlのMethodツール類を生成する
    使い方
    generator Config.xml
    
    <plugin type="com.hui.mybatis.plugins.MysqlBatchUpdatePlugin"/>
    
    pom.xml
    <build>
            <plugins>
                <plugin>
                    <groupId>org.mybatis.generatorgroupId>
                    <artifactId>mybatis-generator-maven-pluginartifactId>
                    <version>${mybatis.generator}version>
                    <configuration>
                        
                        <configurationFile>
                            ${basedir}/src/main/resources/generator/generatorConfig.xml
                        configurationFile>
                        
                        <verbose>trueverbose>
                        
                        <overwrite>trueoverwrite>
                    configuration>
                    <dependencies>
                        
                        <dependency>
                            <groupId>org.mybatis.generatorgroupId>
                            <artifactId>mybatis-generator-coreartifactId>
                            <version>${mybatis.generator}version>
                        dependency>
    
                        
                        <dependency>
                            <groupId>com.hui.mybatis.pluginsgroupId>
                            <artifactId>hui-mybatis-pluginsartifactId>
                            <version>0.0.1-SNAPSHOTversion>
                        dependency>
    
                        
                        <dependency>
                            <groupId>mysqlgroupId>
                            <artifactId>mysql-connector-javaartifactId>
                            <version>5.1.44version>
                        dependency>
    
                        
                        <dependency>
                            <groupId>com.oraclegroupId>
                            <artifactId>ojdbc6artifactId>
                            <version>11.1.0.7.0version>
                        dependency>
    
                        
                        <dependency>
                            <groupId>org.postgresqlgroupId>
                            <artifactId>postgresqlartifactId>
                            <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      ,            ,