Commit 2c70cdfb authored by hewei's avatar hewei

IncrementsPlugin 插件整合 UpsertPlugin 插件

parent a8a788f1
......@@ -176,7 +176,6 @@ public class IncrementsPluginTools {
IntrospectedColumn introspectedColumn = columnIterator.next();
if (this.supportColumn(introspectedColumn)) {
list.add(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + " = "));
for (Element ele : this.generatedIncrementsElement(introspectedColumn, prefix, columnIterator.hasNext())) {
list.add(ele);
}
......
......@@ -305,6 +305,61 @@ public class IncrementsPluginTest {
});
}
/**
* 测试整合 UpsertPlugin
*/
@Test
public void testWithUpsertPlugin() throws InterruptedException, SQLException, InvalidConfigurationException, IOException, XMLParserException {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator-with-upsert-plugin.xml");
tool.generate(new AbstractShellCallback() {
@Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
ObjectUtil tbBuilder = new ObjectUtil(loader, packagz + ".Tb$Builder");
ObjectUtil tbBuilderInc = new ObjectUtil(loader, packagz + ".Tb$Builder$Inc#INC");
tbBuilder.invoke("id", 10L);
tbBuilder.invoke("field1", "ts1");
tbBuilder.invoke("incF1", 10L, tbBuilderInc.getObject());
tbBuilder.invoke("incF2", 1L);
tbBuilder.invoke("incF3", 1L);
// --------------------------- upsert ---------------------------------
// sql
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "upsert", tbBuilder.invoke("build"));
Assert.assertEquals(sql, "insert into tb (id, field1, inc_f1, inc_f2, inc_f3) values (10, 'ts1', 10, 1, 1) on duplicate key update id = 10, field1 = 'ts1', inc_f1 = inc_f1 + 10 , inc_f2 = 1, inc_f3 = 1");
Object result = tbMapper.invoke("upsert", tbBuilder.invoke("build"));
Assert.assertEquals(result, 1);
// 再次执行触发update
tbBuilder.invoke("incF1", 10L, tbBuilderInc.getObject());
tbMapper.invoke("upsert", tbBuilder.invoke("build"));
ResultSet rs = DBHelper.execute(sqlSession, "select * from tb where id = 10");
rs.first();
Assert.assertEquals(rs.getInt("inc_f1"), 20);
// --------------------------- upsertByExample ---------------------------------
tbBuilder.invoke("field1", "ts2");
tbBuilder.invoke("id", 20L);
ObjectUtil tbExample = new ObjectUtil(loader, packagz + ".TbExample");
ObjectUtil criteria = new ObjectUtil(tbExample.invoke("createCriteria"));
criteria.invoke("andField1EqualTo", "ts2");
// sql
sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "upsertByExample", tbBuilder.invoke("build"), tbExample.getObject());
Assert.assertEquals(sql, "insert into tb (id, field1, inc_f1, inc_f2, inc_f3) select 20, 'ts2', 10, 1, 1 from dual where not exists ( select 1 from tb WHERE ( field1 = 'ts2' ) ) ; update tb set field1 = 'ts2', inc_f1 = inc_f1 + 10 , inc_f2 = 1, inc_f3 = 1 WHERE ( field1 = 'ts2' )");
result = tbMapper.invoke("upsertByExample", tbBuilder.invoke("build"), tbExample.getObject());
Assert.assertEquals(result, 1);
// 再次执行触发update
tbBuilder.invoke("incF1", 10L, tbBuilderInc.getObject());
tbMapper.invoke("upsertByExample", tbBuilder.invoke("build"), tbExample.getObject());
rs = DBHelper.execute(sqlSession, "select * from tb where id = 10");
rs.first();
Assert.assertEquals(rs.getInt("inc_f1"), 20);
}
});
}
/**
* 测试 autoDelimitKeywords
*/
......
<?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.IncrementsPlugin" />
<plugin type="com.itfsw.mybatis.generator.plugins.ModelBuilderPlugin" />
<plugin type="com.itfsw.mybatis.generator.plugins.UpsertPlugin">
<property name="allowMultiQueries" value="true"/>
</plugin>
<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">
<property name="incrementsColumns" value="inc_f1"/>
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="tb_keys">
<property name="incrementsColumns" value="inc_f1,inc_f2,inc_f3"/>
</table>
<table tableName="tb_blobs">
<property name="incrementsColumns" value="inc_f1,inc_f3"/>
</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