地址:https://github.com/ithuhui/hui-mybatis-generator-plugins
分支:master
位置:com.hui.mybatis.plugins
批量删除的SQL比较简单和通用,所以,插件也比较简单。核心代码贴出来,其他需要的看GITHUB看看
github上针对mybatis-generator还有很多不同类型的插件,我实在懒的看,而且太多复杂的功能,我简单点搞自己需要的。
<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>
delete from T_HUI_ORDER where ORDER_ID in(?,?,?);
<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>
/**
* <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);
}
}
插件通用方法/工具类,上面生成方法基于了一个工具类,下面博客有源码,真正需要的可以看看github
【Mybatis】Mybatis-Generator-Tool 生成Java和Xml的Method工具类
<!--批量删除-->
<plugin type="com.hui.mybatis.plugins.BatchDeletePlugin"/>
<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和大数据问题,转载请注明作者和原文链接,感谢