文章目录
系列
- 【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工具类
Github
地址:https://github.com/ithuhui/hui-mybatis-generator-plugins
分支:master
位置:com.hui.mybatis.plugins
Note
Oracle批量插入方法插件。有需要的上我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>
mybatis-implement
<insert id="batchInsert" parameterType="java.util.List">
insert into T_HUI_ORDER (
ORDER_ID,
ORDER_NAME,
PRODUCT_ID,
BUY_QUANTITY,
CREATED_TIME
)
(
<foreach collection="recordList" index="index" item="item" separator="union all">
(
select
#{item.orderId,jdbcType=VARCHAR},
#{item.orderName,jdbcType=VARCHAR},
#{item.productId,jdbcType=VARCHAR},
#{item.buyQuantity,jdbcType=DECIMAL},
#{item.createdTime,jdbcType=TIMESTAMP}
from dual
)
</foreach>
)
</insert>
plugin-code
/**
* <b><code>OracleBatchInsertPlugin</code></b>
* <p/>
* Description:
* <p/>
* <b>Creation Time:</b> 2018/12/15 1:12.
*
* @author HuWeihui
*/
public class OracleBatchInsertPlugin extends PluginAdapter {
private final static String BATCH_INSERT = "batchInsert";
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() == IntrospectedTable.TargetRuntime.MYBATIS3) {
MethodGeneratorTool.defaultBatchInsertOrUpdateMethodGen(MethodGeneratorTool.INSERT, 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);
}
private void addSqlMapper(Document document, IntrospectedTable introspectedTable) {
//1.Batchinsert
XmlElement baseElement = SqlMapperGeneratorTool.baseElementGenerator(SqlMapperGeneratorTool.INSERT,
BATCH_INSERT,
FullyQualifiedJavaType.getNewListInstance());
XmlElement foreachElement = SqlMapperGeneratorTool.baseForeachElementGenerator(PARAMETER_NAME,
"item",
"index",
"union all");
//tableName
baseElement.addElement(new TextElement(String.format("insert into %s (", introspectedTable.getFullyQualifiedTableNameAtRuntime())));
foreachElement.addElement(new TextElement("("));
foreachElement.addElement(new TextElement("select"));
for (int i = 0; i < introspectedTable.getAllColumns().size(); i++) {
//column信息
IntrospectedColumn introspectedColumn = introspectedTable.getAllColumns().get(i);
String columnInfo = "";
String valueInfo = "";
columnInfo = introspectedColumn.getActualColumnName();
valueInfo = MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, "item.");
if (introspectedColumn.isIdentity()) {
String nextval = introspectedTable.getFullyQualifiedTableNameAtRuntime()+"_SEQUENCE.nextval" ;
valueInfo = nextval;
}
if (i != (introspectedTable.getAllColumns().size() - 1)) {
columnInfo += (",");
valueInfo += ",";
}
baseElement.addElement(new TextElement(columnInfo));
foreachElement.addElement(new TextElement(valueInfo));
}
foreachElement.addElement(new TextElement("from dual"));
foreachElement.addElement(new TextElement(")"));
baseElement.addElement(new TextElement(")"));
baseElement.addElement(new TextElement("("));
baseElement.addElement(foreachElement);
baseElement.addElement(new TextElement(")"));
//3.parent Add
document.getRootElement().addElement(baseElement);
}
}
plugin-common-tool
插件通用方法/工具类,上面生成方法基于了一个工具类,下面博客有源码,真正需要的可以看看github
【Mybatis】Mybatis-Generator-Tool 生成Java和Xml的Method工具类
使用方法
generatorConfig.xml
<!--Oracle批量插入-->
<plugin type="com.hui.mybatis.plugins.OracleBatchInsertPlugin"/>
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和大数据问题,转载请注明作者和原文链接,感谢