Commit d0233d96 authored by hewei's avatar hewei

LombokPlugin 配合 IncrementsPlugin 测试用例

parent 5cbb85bd
/*
* Copyright (c) 2017.
*
* 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.
*/
package com.itfsw.mybatis.generator.plugins.utils;
import com.itfsw.mybatis.generator.plugins.IncrementsPlugin;
import com.itfsw.mybatis.generator.plugins.ModelBuilderPlugin;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.internal.util.StringUtility;
import java.util.ArrayList;
import java.util.List;
/**
* ---------------------------------------------------------------------------
* 增量插件工具
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2017/6/21 16:12
* ---------------------------------------------------------------------------
*/
public class IncrementsPluginTools {
private IntrospectedTable introspectedTable; // 表
private List<IntrospectedColumn> columns = new ArrayList<>(); // 表启用增量操作的字段
/**
* 构造函数
* @param introspectedTable
*/
private IncrementsPluginTools(IntrospectedTable introspectedTable) {
this.introspectedTable = introspectedTable;
}
/**
* 获取工具
* @param context
* @param introspectedTable
* @param warnings
* @return
*/
public static IncrementsPluginTools getTools(Context context, IntrospectedTable introspectedTable, List<String> warnings) {
IncrementsPluginTools tools = new IncrementsPluginTools(introspectedTable);
// 判断是否启用了插件
if (PluginTools.getPluginConfiguration(context, IncrementsPlugin.class) != null) {
String incrementsColumns = introspectedTable.getTableConfigurationProperty(IncrementsPlugin.PRO_INCREMENTS_COLUMNS);
if (StringUtility.stringHasValue(incrementsColumns)) {
// 切分
String[] incrementsColumnsStrs = incrementsColumns.split(",");
for (String incrementsColumnsStr : incrementsColumnsStrs) {
IntrospectedColumn column = IntrospectedTableTools.safeGetColumn(introspectedTable, incrementsColumnsStr);
if (column == null) {
warnings.add("itfsw:插件" + IncrementsPlugin.class.getTypeName() + "插件没有找到column为" + incrementsColumnsStr.trim() + "的字段!");
} else {
tools.columns.add(column);
}
}
}
}
return tools;
}
/**
* 获取INC Enum
* @return
*/
public FullyQualifiedJavaType getIncEnum() {
return new FullyQualifiedJavaType(this.introspectedTable.getFullyQualifiedTable().getDomainObjectName() + "." + ModelBuilderPlugin.BUILDER_CLASS_NAME + ".Inc");
}
/**
* 是否启用了
* @return
*/
public boolean support() {
return this.columns.size() > 0;
}
/**
* Getter method for property <tt>columns</tt>.
* @return property value of columns
* @author hewei
*/
public List<IntrospectedColumn> getColumns() {
return columns;
}
/**
* 判断是否为需要进行增量操作的column
* @param searchColumn
* @return
*/
public boolean supportColumn(IntrospectedColumn searchColumn) {
for (IntrospectedColumn column : this.columns) {
if (column.getActualColumnName().equals(searchColumn.getActualColumnName())) {
return true;
}
}
return false;
}
}
......@@ -602,15 +602,69 @@ public class IncrementsPluginTest {
tbBuilder = new ObjectUtil(tbBuilder.invoke("id", 1L));
tbBuilder.invoke("field1", "ts1");
ObjectUtil tb = new ObjectUtil(tbBuilder.invoke("build"));
Assert.assertEquals(tb.invoke("toString"), "Tb(id=1, field1=ts1, field2=null)");
Assert.assertEquals(tb.invoke("toString"), "Tb(id=1, field1=ts1, field2=null, incrementsColumnsInfoMap={})");
// super
ObjectUtil tbLombokWithBLOBsBuilder = new ObjectUtil(loader.loadClass(packagz + ".TbLombokWithBLOBs").getMethod("builder").invoke(null));
tbLombokWithBLOBsBuilder.invoke("field3", "ts3");
Assert.assertEquals(tbLombokWithBLOBsBuilder.invoke("toString"), "TbLombokWithBLOBs.TbLombokWithBLOBsBuilder(super=TbLombok.TbLombokBuilder(super=TbLombokKey.TbLombokKeyBuilder(id=null, key1=null), field1=null, incF1=null), field3=ts3, field4=null)");
Assert.assertEquals(tbLombokWithBLOBsBuilder.invoke("toString"), "TbLombokWithBLOBs.TbLombokWithBLOBsBuilder(super=TbLombok.TbLombokBuilder(super=TbLombokKey.TbLombokKeyBuilder(id=null, key1=null, incrementsColumnsInfoMap={}), field1=null, incF1=null), field3=ts3, field4=null)");
tbLombokWithBLOBsBuilder.invoke("field1", "ts1");
Assert.assertEquals(tbLombokWithBLOBsBuilder.invoke("toString"), "TbLombokWithBLOBs.TbLombokWithBLOBsBuilder(super=TbLombok.TbLombokBuilder(super=TbLombokKey.TbLombokKeyBuilder(id=null, key1=null), field1=ts1, incF1=null), field3=ts3, field4=null)");
Assert.assertEquals(tbLombokWithBLOBsBuilder.invoke("toString"), "TbLombokWithBLOBs.TbLombokWithBLOBsBuilder(super=TbLombok.TbLombokBuilder(super=TbLombokKey.TbLombokKeyBuilder(id=null, key1=null, incrementsColumnsInfoMap={}), field1=ts1, incF1=null), field3=ts3, field4=null)");
tbLombokWithBLOBsBuilder.invoke("id", 100L);
Assert.assertEquals(tbLombokWithBLOBsBuilder.invoke("toString"), "TbLombokWithBLOBs.TbLombokWithBLOBsBuilder(super=TbLombok.TbLombokBuilder(super=TbLombokKey.TbLombokKeyBuilder(id=100, key1=null), field1=ts1, incF1=null), field3=ts3, field4=null)");
Assert.assertEquals(tbLombokWithBLOBsBuilder.invoke("toString"), "TbLombokWithBLOBs.TbLombokWithBLOBsBuilder(super=TbLombok.TbLombokBuilder(super=TbLombokKey.TbLombokKeyBuilder(id=100, key1=null, incrementsColumnsInfoMap={}), field1=ts1, incF1=null), field3=ts3, field4=null)");
// ------------------------------------- 测试 sql 执行 ----------------------------------------
// 1. 测试updateByExample、updateByExampleSelective
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
ObjectUtil tbExample = new ObjectUtil(loader, packagz + ".TbExample");
ObjectUtil criteria = new ObjectUtil(tbExample.invoke("createCriteria"));
criteria.invoke("andIdEqualTo", 3l);
tbBuilder = new ObjectUtil(loader.loadClass(packagz + ".Tb").getMethod("builder").invoke(null));
ObjectUtil tbBuilderInc = new ObjectUtil(loader, packagz + ".Tb$TbBuilder$Inc#INC");
tbBuilder.invoke("field2", 100, tbBuilderInc.getObject());
// sql
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "updateByExample", tbBuilder.invoke("build"), tbExample.getObject());
Assert.assertEquals(sql, "update tb set id = null, field1 = 'null', field2 = field2 + 100 WHERE ( id = '3' )");
sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "updateByExampleSelective", tbBuilder.invoke("build"), tbExample.getObject());
Assert.assertEquals(sql, "update tb SET field2 = field2 + 100 WHERE ( id = '3' )");
// 执行
// inc_f1 增加100
Object result = tbMapper.invoke("updateByExampleSelective", tbBuilder.invoke("build"), tbExample.getObject());
Assert.assertEquals(result, 1);
ResultSet rs = DBHelper.execute(sqlSession.getConnection(), "select field2 from tb where id = 3");
rs.first();
Assert.assertEquals(rs.getInt("field2"), 103);
// 2. 测试有SuperBuilder的情况
ObjectUtil tbLombokMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbLombokMapper")));
ObjectUtil tbLombokExample = new ObjectUtil(loader, packagz + ".TbLombokExample");
criteria = new ObjectUtil(tbLombokExample.invoke("createCriteria"));
criteria.invoke("andKey1EqualTo", "key1");
tbLombokWithBLOBsBuilder = new ObjectUtil(loader.loadClass(packagz + ".TbLombokWithBLOBs").getMethod("builder").invoke(null));
tbLombokWithBLOBsBuilder.invoke("field3", "f3");
ObjectUtil tbLombokKeyBuilderIncINC = new ObjectUtil(loader, packagz + ".TbLombokKey$TbLombokKeyBuilder$Inc#INC");
tbLombokWithBLOBsBuilder.invoke("incF1", (short)1, tbLombokKeyBuilderIncINC.getObject());
tbLombokWithBLOBsBuilder.invoke("field1", "ts33");
ObjectUtil tbLombokKeyBuilderIncDEC = new ObjectUtil(loader, packagz + ".TbLombokKey$TbLombokKeyBuilder$Inc#DEC");
tbLombokWithBLOBsBuilder.invoke("id", 100L, tbLombokKeyBuilderIncDEC.getObject());
tbLombokWithBLOBsBuilder.invoke("key1", "key100");
// sql
sql = SqlHelper.getFormatMapperSql(tbLombokMapper.getObject(), "updateByExampleSelective", tbLombokWithBLOBsBuilder.invoke("build"), tbLombokExample.getObject());
Assert.assertEquals(sql, "update tb_lombok SET id = id - 100 , key1 = 'key100', field1 = 'ts33', inc_f1 = inc_f1 + 1 , field3 = 'f3' WHERE ( key1 = 'key1' )");
// 执行
result = tbLombokMapper.invoke("updateByExampleSelective", tbLombokWithBLOBsBuilder.invoke("build"), tbLombokExample.getObject());
Assert.assertEquals(result, 1);
rs = DBHelper.execute(sqlSession.getConnection(), "select * from tb_lombok where key1 = 'key100'");
rs.first();
Assert.assertEquals(rs.getInt("inc_f1"), 1);
Assert.assertEquals(rs.getInt("id"), -99);
}
});
}
......
......@@ -61,7 +61,7 @@ CREATE TABLE `tb_lombok` (
`id` bigint(20) NOT NULL COMMENT '注释1',
`key1` varchar(20) NOT NULL,
`field1` varchar(10) COMMENT '注释2',
`inc_f1` tinyint(1),
`inc_f1` smallint(3),
`field3` longtext,
`field4` longtext,
PRIMARY KEY (`id`,`key1`)
......
......@@ -57,7 +57,7 @@
<property name="incrementsColumns" value="inc_f1, field2"/>
</table>
<table tableName="tb_lombok">
<property name="incrementsColumns" value="inc_f1"/>
<property name="incrementsColumns" value="inc_f1,id"/>
</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