版权声明:小哥哥小姐姐们,本文为小博主原创文章,转载请附上博主博文网址,并标注作者谢谢~~。违者必究 https://blog.csdn.net/HuHui_/article/details/85150659
文章目录
系列
- 【Mybatis】Mybatis-Generator-Postgresql返回主键插件
- 【Mybatis】Mybatis-Generator-Oracle返回主键插件
- 【Mybatis】Mybatis-Generator-batchDelete(批量删除)方法插件
- 【Mybatis】Mybatis-Generator-Postgresql&Mysql(批量新增)方法插件
- 【Mybatis】Mybatis-Generator-Oracle(批量新增)方法插件
- 【Mybatis】Mybatis-Generator-Mysql(批量更新)方法插件
- 【Mybatis】Mybatis-Generator-Postgresql(批量更新)方法插件
- 【Mybatis】Mybatis-Generator-Oracle(批量更新)方法插件
- 【Mybatis】Mybatis-Generator-Tool 生成Java和Xml的Method工具类
前言
Mysql和PostgreSql的批量插入语法一样。这个还是比较安慰的。有需要上github看
github上针对mybatis-generator还有很多不同类型的插件,我实在懒的看,而且太多复杂的功能,我简单点搞自己需要的。
装备
-
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
<insert id="batchInsert" parameterType="java.util.List">
insert into T_HUI_ORDER (
ORDER_ID,
ORDER_NAME,
PRODUCT_ID,
BUY_QUANTITY,
CREATED_TIME
)
values
<foreach collection="recordList" index="index" item="item" separator=",">
(
#{item.orderId,jdbcType=VARCHAR},
#{item.orderName,jdbcType=VARCHAR},
#{item.productId,jdbcType=VARCHAR},
#{item.buyQuantity,jdbcType=DECIMAL},
#{item.createdTime,jdbcType=TIMESTAMP}
)
</foreach>
</insert>
plugin-code
/**
* <b><code>BatchInsertPlugin</code></b>
* <p/>
* Description: 批量insert 和 insertSelective插件开发(Mybatis模式的时候)
* <p/>
* <b>Creation Time:</b> 2018/12/6 22:59.
*
* @author HuWeihui
*/
public class BatchInsertPlugin extends PluginAdapter {
private final static String BATCH_INSERT = "batchInsert";
private final static String BATCH_DELETE = "batchDelete";
private final static String PARAMETER_NAME = "recordList";
@Override
public boolean validate(List<String> list) {
return true;
}
/**
* java代码Mapper生成
*
* @param interfaze
* @param topLevelClass
* @param introspectedTable
* @return
*/
@Override
public boolean clientGenerated(Interface interfaze,
TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
if (BaseGenTool.isMybatisMode(introspectedTable)) {
//生成batchInsert 和 batchInsertSelective的java方法
MethodGeneratorTool.defaultBatchInsertOrUpdateMethodGen(MethodGeneratorTool.INSERT, interfaze, introspectedTable, context);
}
return super.clientGenerated(interfaze, topLevelClass,
introspectedTable);
}
/**
* sqlMapper生成
*
* @param document
* @param introspectedTable
* @return
*/
@Override
public boolean sqlMapDocumentGenerated(Document document,
IntrospectedTable introspectedTable) {
if (BaseGenTool.isMybatisMode(introspectedTable)) {
//生成batchInsert 和 batchInsertSelective的java方法
addSqlMapper(document, introspectedTable);
}
return super.sqlMapDocumentGenerated(document, introspectedTable);
}
/**
* batchInsert和batchInsertSelective的SQL生成
*
* @param document
* @param introspectedTable
*/
private void addSqlMapper(Document document, IntrospectedTable introspectedTable) {
//table名名字
String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime();
//column信息
List<IntrospectedColumn> columnList = introspectedTable.getAllColumns();
XmlElement baseElement = SqlMapperGeneratorTool.baseElementGenerator(SqlMapperGeneratorTool.INSERT,
BATCH_INSERT,
FullyQualifiedJavaType.getNewListInstance());
XmlElement foreachElement = SqlMapperGeneratorTool.baseForeachElementGenerator(PARAMETER_NAME,
"item",
"index",
",");
baseElement.addElement(new TextElement(String.format("insert into %s (", tableName)));
foreachElement.addElement(new TextElement("("));
for (int i = 0; i < columnList.size(); i++) {
String columnInfo = "";
String valueInfo = "";
IntrospectedColumn introspectedColumn = columnList.get(i);
if (introspectedColumn.isIdentity()) {
continue;
}
columnInfo = introspectedColumn.getActualColumnName();
valueInfo = MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, "item.");
if (i != (columnList.size() - 1)) {
columnInfo += (",");
valueInfo += (",");
}
baseElement.addElement(new TextElement(columnInfo));
foreachElement.addElement(new TextElement(valueInfo));
}
foreachElement.addElement(new TextElement(")"));
baseElement.addElement(new TextElement(")"));
baseElement.addElement(new TextElement("values"));
baseElement.addElement(foreachElement);
//3.parent Add
document.getRootElement().addElement(baseElement);
}
}
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>
Github
https://github.com/ithuhui/hui-mybatis-plugins/tree/master/src/main/java/com/hui/mybatis/plugins
作者
作者:HuHui
转载:欢迎一起讨论web和大数据问题,转载请注明作者和原文链接,感谢