Commit f6b040a4 authored by hewei's avatar hewei

BatchInsertPlugin 增加 allowMultiQueries配置,开启时在不传指定列时启用原生非空判断

parent c8869e95
......@@ -23,8 +23,11 @@ import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.*;
import org.mybatis.generator.codegen.mybatis3.ListUtilities;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
import org.mybatis.generator.internal.util.StringUtility;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* ---------------------------------------------------------------------------
......@@ -37,6 +40,8 @@ import java.util.List;
public class BatchInsertPlugin extends BasePlugin {
public static final String METHOD_BATCH_INSERT = "batchInsert"; // 方法名
public static final String METHOD_BATCH_INSERT_SELECTIVE = "batchInsertSelective"; // 方法名
public static final String PRO_ALLOW_MULTI_QUERIES = "allowMultiQueries"; // property allowMultiQueries
private boolean allowMultiQueries = false; // 是否允许多sql提交
/**
* {@inheritDoc}
......@@ -59,6 +64,15 @@ public class BatchInsertPlugin extends BasePlugin {
return false;
}
// 插件是否开启了多sql提交
Properties properties = this.getProperties();
String allowMultiQueries = properties.getProperty(PRO_ALLOW_MULTI_QUERIES);
this.allowMultiQueries = allowMultiQueries == null ? false : StringUtility.isTrue(allowMultiQueries);
if (this.allowMultiQueries) {
// 提示用户注意信息
warnings.add("itfsw:插件" + this.getClass().getTypeName() + "插件您开启了allowMultiQueries支持,注意在jdbc url 配置中增加“allowMultiQueries=true”支持(不怎么建议使用该功能,开启多sql提交会增加sql注入的风险,请确保你所有sql都使用MyBatis书写,请不要使用statement进行sql提交)!");
}
return super.validate(warnings);
}
......@@ -143,23 +157,75 @@ public class BatchInsertPlugin extends BasePlugin {
// values 构建
batchInsertEle.addElement(new TextElement("values"));
batchInsertEle.addElement(foreachElement);
if (context.getPlugins().sqlMapInsertElementGenerated(batchInsertEle, introspectedTable)) {
document.getRootElement().addElement(batchInsertEle);
logger.debug("itfsw(批量插入插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加batchInsert实现方法。");
}
// 2. batchInsertSelective
XmlElement element = new XmlElement("insert");
element.addAttribute(new Attribute("id", METHOD_BATCH_INSERT_SELECTIVE));
// 参数类型
element.addAttribute(new Attribute("parameterType", "map"));
XmlElement batchInsertSelectiveEle = new XmlElement("insert");
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
commentGenerator.addComment(element);
commentGenerator.addComment(batchInsertSelectiveEle);
batchInsertSelectiveEle.addAttribute(new Attribute("id", METHOD_BATCH_INSERT_SELECTIVE));
// 参数类型
batchInsertSelectiveEle.addAttribute(new Attribute("parameterType", "map"));
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools.useGeneratedKeys(element, introspectedTable);
XmlElementGeneratorTools.useGeneratedKeys(batchInsertSelectiveEle, introspectedTable);
// 支持原生字段非空判断
if (this.allowMultiQueries) {
XmlElement chooseEle = new XmlElement("choose");
// selective 增强
XmlElement selectiveEnhancedEle = new XmlElement("when");
selectiveEnhancedEle.addAttribute(new Attribute("test", "selective != null and selective.length > 0"));
chooseEle.addElement(selectiveEnhancedEle);
selectiveEnhancedEle.getElements().addAll(this.generateSelectiveEnhancedEles(introspectedTable));
element.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime() + " ("));
// 原生非空判断语句
XmlElement selectiveNormalEle = new XmlElement("otherwise");
chooseEle.addElement(selectiveNormalEle);
XmlElement foreachEle = new XmlElement("foreach");
selectiveNormalEle.addElement(foreachEle);
foreachEle.addAttribute(new Attribute("collection", "list"));
foreachEle.addAttribute(new Attribute("item", "item"));
foreachEle.addAttribute(new Attribute("separator", ";"));
foreachEle.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
XmlElement insertTrimElement = new XmlElement("trim");
foreachEle.addElement(insertTrimElement);
insertTrimElement.addElement(XmlElementGeneratorTools.generateKeysSelective(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()), "item."));
foreachEle.addElement(new TextElement("values"));
XmlElement valuesTrimElement = new XmlElement("trim");
foreachEle.addElement(valuesTrimElement);
valuesTrimElement.addElement(XmlElementGeneratorTools.generateValuesSelective(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()), "item."));
batchInsertSelectiveEle.addElement(chooseEle);
} else {
batchInsertSelectiveEle.getElements().addAll(this.generateSelectiveEnhancedEles(introspectedTable));
}
document.getRootElement().addElement(batchInsertSelectiveEle);
logger.debug("itfsw(批量插入插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加batchInsertSelective实现方法。");
return true;
}
/**
* 生成insert selective 增强的插入语句
* @param introspectedTable
* @return
*/
private List<Element> generateSelectiveEnhancedEles(IntrospectedTable introspectedTable) {
List<Element> eles = new ArrayList<>();
eles.add(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime() + " ("));
XmlElement foreachInsertColumns = new XmlElement("foreach");
foreachInsertColumns.addAttribute(new Attribute("collection", "selective"));
......@@ -167,12 +233,12 @@ public class BatchInsertPlugin extends BasePlugin {
foreachInsertColumns.addAttribute(new Attribute("separator", ","));
foreachInsertColumns.addElement(new TextElement("${column.value}"));
element.addElement(foreachInsertColumns);
eles.add(foreachInsertColumns);
element.addElement(new TextElement(")"));
eles.add(new TextElement(")"));
// values
element.addElement(new TextElement("values"));
eles.add(new TextElement("values"));
// foreach values
XmlElement foreachValues = new XmlElement("foreach");
......@@ -203,14 +269,8 @@ public class BatchInsertPlugin extends BasePlugin {
foreachValues.addElement(new TextElement(")"));
element.addElement(foreachValues);
if (context.getPlugins().sqlMapInsertElementGenerated(element, introspectedTable)) {
document.getRootElement().addElement(element);
logger.debug("itfsw(批量插入插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加batchInsertSelective实现方法。");
}
eles.add(foreachValues);
return true;
return eles;
}
}
\ No newline at end of file
......@@ -66,6 +66,11 @@ public class BatchInsertPluginTest {
Assert.assertTrue(tool.getWarnings().size() == 2);
Assert.assertEquals(tool.getWarnings().get(0), "itfsw:插件com.itfsw.mybatis.generator.plugins.BatchInsertPlugin插件需配合com.itfsw.mybatis.generator.plugins.ModelColumnPlugin插件使用!");
// 2. 普通提示
tool = MyBatisGeneratorTool.create("scripts/BatchInsertPlugin/mybatis-generator-with-allowMultiQueries.xml");
tool.generate();
Assert.assertEquals(tool.getWarnings().get(0), "itfsw:插件com.itfsw.mybatis.generator.plugins.BatchInsertPlugin插件您开启了allowMultiQueries支持,注意在jdbc url 配置中增加“allowMultiQueries=true”支持(不怎么建议使用该功能,开启多sql提交会增加sql注入的风险,请确保你所有sql都使用MyBatis书写,请不要使用statement进行sql提交)!");
}
/**
......@@ -164,6 +169,11 @@ public class BatchInsertPluginTest {
// 2. 执行sql
Object count = tbMapper.invoke("batchInsert", params);
Assert.assertEquals(count, 2);
for (int i = 0; i < params.size(); i++) {
ObjectUtil item = new ObjectUtil(params.get(i));
Assert.assertEquals(item.get("id"), 1L + i);
}
}
});
}
......@@ -204,4 +214,50 @@ public class BatchInsertPluginTest {
}
});
}
/**
* 测试开启 AllowMultiQueries 支持
*/
@Test
public void testAllowMultiQueries() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/BatchInsertPlugin/mybatis-generator-with-allowMultiQueries.xml");
// 1. 测试增强的selective
tool.generate(() -> DBHelper.resetDB("scripts/BatchInsertPlugin/init.sql"), new AbstractShellCallback() {
@Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
List<Object> params = new ArrayList<>();
params.add(new ObjectUtil(loader, packagz + ".Tb").set("field1", "test").getObject());
params.add(new ObjectUtil(loader, packagz + ".Tb").set("field1", "test").set("field2", 1).getObject());
ObjectUtil columnField2 = new ObjectUtil(loader, packagz + ".Tb$Column#field2");
Object columns = Array.newInstance(columnField2.getCls(), 1);
Array.set(columns, 0, columnField2.getObject());
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "batchInsertSelective", params, columns);
Assert.assertEquals(sql, "insert into tb ( field2 ) values ( null ) , ( 1 )");
// 2. 执行sql
Object count = tbMapper.invoke("batchInsertSelective", params, columns);
Assert.assertEquals(count, 2);
}
});
// 2. 测试原生非空判断
tool.generate(() -> DBHelper.resetDB("scripts/BatchInsertPlugin/init.sql"), new AbstractShellCallback() {
@Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
List<Object> params = new ArrayList<>();
params.add(new ObjectUtil(loader, packagz + ".Tb").set("field1", "test").getObject());
params.add(new ObjectUtil(loader, packagz + ".Tb").set("field1", "test").set("field2", 1).getObject());
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "batchInsertSelective", params, null);
Assert.assertEquals(sql, "insert into tb ( field1 ) values ( 'test' ) ; insert into tb ( field1, field2 ) values ( 'test', 1 )");
// 2. 执行sql
Object count = tbMapper.invoke("batchInsertSelective", params, null);
Assert.assertTrue((int) count > 0);
}
});
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2018.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<properties resource="db.properties"/>
<!--导入属性配置 -->
<context id="default" targetRuntime="MyBatis3">
<!-- 插件 -->
<!-- 批量插入插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.BatchInsertPlugin">
<property name="allowMultiQueries" value="true"/>
</plugin>
<!-- 数据Model属性对应Column获取插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"/>
<!--jdbc的数据库连接 -->
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}" />
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator targetPackage="" targetProject="">
<!-- 是否对model添加 构造函数 -->
<property name="constructorBased" value="true"/>
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator targetPackage="" targetProject="" />
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator targetPackage="" targetProject="" type="XMLMAPPER"/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table tableName="tb">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="tb_keys" />
<table tableName="tb_single_blob">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="tb_blobs">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
</context>
</generatorConfiguration>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment