【Mybatis】Mybatis-Generator-batchDelete(批量删除)方法插件

文章目录

  • 系列
  • 前言
  • 装备
  • Core-Code
    • sql
    • mybatis-implement
    • plugin-code
    • plugin-common-tool
  • 使用方法
    • generatorConfig.xml
    • pom.xml
  • Github
  • 作者

系列

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

前言

批量删除的SQL比较简单和通用,所以,插件也比较简单。核心代码贴出来,其他需要的看GITHUB看看

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

装备

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

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

/**
 * BatchDeletePlugin
 * 

* Description: *

* Creation Time: 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.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和大数据问题,转载请注明作者和原文链接,感谢

你可能感兴趣的:(Developer,Manual)