系列
- 【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
Mysql和PostgreSql的批量插入語法一樣。這個還是比較安慰的。有需要上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
)
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>
作者
作者:HuHui
轉載:歡迎一起討論web和大數據問題,轉載請註明作者和原文鏈接,感謝