【Mybatis】Mybatis-Generator-batchDelete-批量删除

系列

  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

批量删除的SQL比较简单和通用,所以,插件也比较简单。核心代码贴出来,其他需要的看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>

sql

  delete from T_HUI_ORDER where ORDER_ID in(?,?,?);

mybatis-implement

  <delete id="batchDelete" parameterType="java.lang.String">
    delete from T_HUI_ORDER where ORDER_ID in (
    <foreach collection="ids" index="index" item="item" separator=",">
      #{item}
    </foreach>
    )
  </delete>

plugin-code

/**
 * <b><code>BatchDeletePlugin</code></b>
 * <p/>
 * Description:
 * <p/>
 * <b>Creation Time:</b> 2018/12/10 0:48.
 *
 * @author HuWeihui
 */
public class BatchDeletePlugin extends PluginAdapter {

    private final static String BATCH_DELETE = "batchDelete";

    private final static String PARAMETER_NAME = "ids";

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


    @Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        if (BaseGenTool.isMybatisMode(introspectedTable)) {
            MethodGeneratorTool.defaultBatchDeleteMethodGen(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);
    }


    /**
     * 批量删除的xml方法生成
     * @param document
     * @param introspectedTable
     */
    private void addSqlMapper(Document document, IntrospectedTable introspectedTable){
        String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime();

        String key = introspectedTable.getPrimaryKeyColumns().get(0).getActualColumnName();

        String baseSql = String.format("delete from %s where %s in (",tableName,key);

        FullyQualifiedJavaType paramType = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType();

        XmlElement deleteElement = SqlMapperGeneratorTool.baseElementGenerator(SqlMapperGeneratorTool.DELETE, BATCH_DELETE,paramType);

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

        deleteElement.addElement(new TextElement(baseSql));

        foreachElement.addAttribute(new Attribute("separator", ","));

        foreachElement.addElement(new TextElement("#{item}"));

        deleteElement.addElement(foreachElement);

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

        document.getRootElement().addElement(deleteElement);
    }
}

plugin-common-tool

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

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

使用方法

generatorConfig.xml

<!--批量删除-->
<plugin type="com.hui.mybatis.plugins.BatchDeletePlugin"/>

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和大数据问题,转载请注明作者和原文链接,感谢
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值