Commit d8438373 authored by hewei's avatar hewei

bug:upsertByExampleXXX insert和update写反了

parent 2c70cdfb
...@@ -171,9 +171,9 @@ public class OldSelectiveEnhancedPlugin extends BasePlugin { ...@@ -171,9 +171,9 @@ public class OldSelectiveEnhancedPlugin extends BasePlugin {
// ====================================== 5. upsertByExampleSelective ====================================== // ====================================== 5. upsertByExampleSelective ======================================
if ("upsertByExampleSelective".equals(id)) { if ("upsertByExampleSelective".equals(id)) {
List<XmlElement> eles = XmlElementGeneratorTools.findXmlElements(xmlElement, "trim"); List<XmlElement> eles = XmlElementGeneratorTools.findXmlElements(xmlElement, "trim");
this.replaceEle(eles.get(0), "record.", introspectedTable); this.replaceEle(eles.get(1), "record.", introspectedTable);
// upsertByExampleSelective的第二个trim比较特殊,需另行处理 // upsertByExampleSelective的第二个trim比较特殊,需另行处理
this.replaceEleForUpsertByExampleSelective(eles.get(1), "record.", introspectedTable, !introspectedTable.getRules().generateRecordWithBLOBsClass()); this.replaceEleForUpsertByExampleSelective(eles.get(2), "record.", introspectedTable, !introspectedTable.getRules().generateRecordWithBLOBsClass());
List<XmlElement> eles1 = XmlElementGeneratorTools.findXmlElements(xmlElement, "set"); List<XmlElement> eles1 = XmlElementGeneratorTools.findXmlElements(xmlElement, "set");
for (XmlElement ele : eles1) { for (XmlElement ele : eles1) {
......
...@@ -347,13 +347,48 @@ public class IncrementsPluginTest { ...@@ -347,13 +347,48 @@ public class IncrementsPluginTest {
// sql // sql
sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "upsertByExample", tbBuilder.invoke("build"), tbExample.getObject()); 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' )"); Assert.assertEquals(sql, "update tb set field1 = 'ts2', inc_f1 = inc_f1 + 10 , inc_f2 = 1, inc_f3 = 1 WHERE ( field1 = 'ts2' ) ; 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' ) )");
result = tbMapper.invoke("upsertByExample", tbBuilder.invoke("build"), tbExample.getObject()); tbMapper.invoke("upsertByExample", tbBuilder.invoke("build"), tbExample.getObject());
Assert.assertEquals(result, 1);
// 再次执行触发update // 再次执行触发update
tbBuilder.invoke("incF1", 10L, tbBuilderInc.getObject()); tbBuilder.invoke("incF1", 10L, tbBuilderInc.getObject());
tbMapper.invoke("upsertByExample", tbBuilder.invoke("build"), tbExample.getObject()); tbMapper.invoke("upsertByExample", tbBuilder.invoke("build"), tbExample.getObject());
rs = DBHelper.execute(sqlSession, "select * from tb where id = 10"); rs = DBHelper.execute(sqlSession, "select * from tb where id = 20");
rs.first();
Assert.assertEquals(rs.getInt("inc_f1"), 20);
// --------------------------- upsertSelective ---------------------------------
tbBuilder.set("obj.incF3", null);
tbBuilder.invoke("id", 30L);
// sql
sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "upsertSelective", tbBuilder.invoke("build"));
Assert.assertEquals(sql, "insert into tb ( id, field1, inc_f1, inc_f2 ) values ( 30, 'ts2', 10, 1 ) on duplicate key update field1 = 'ts2', inc_f1 = inc_f1 + 10 , inc_f2 = 1");
result = tbMapper.invoke("upsert", tbBuilder.invoke("build"));
Assert.assertEquals(result, 1);
// 再次执行触发update
tbBuilder.invoke("incF1", 10L, tbBuilderInc.getObject());
tbMapper.invoke("upsertSelective", tbBuilder.invoke("build"));
rs = DBHelper.execute(sqlSession, "select * from tb where id = 30");
rs.first();
Assert.assertEquals(rs.getInt("inc_f1"), 20);
// --------------------------- upsertByExampleSelective ---------------------------------
tbBuilder.invoke("field1", "ts3");
tbBuilder.invoke("id", 40L);
tbExample = new ObjectUtil(loader, packagz + ".TbExample");
criteria = new ObjectUtil(tbExample.invoke("createCriteria"));
criteria.invoke("andField1EqualTo", "ts3");
// sql
sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "upsertByExampleSelective", tbBuilder.invoke("build"), tbExample.getObject());
Assert.assertEquals(sql, "update tb set field1 = 'ts3', inc_f1 = inc_f1 + 10 , inc_f2 = 1 WHERE ( field1 = 'ts3' ) ; insert into tb ( id, field1, inc_f1, inc_f2 ) select 40, 'ts3', 10, 1 from dual where not exists ( select 1 from tb WHERE ( field1 = 'ts3' ) )");
result = tbMapper.invoke("upsert", tbBuilder.invoke("build"));
Assert.assertEquals(result, 1);
// 再次执行触发update
tbBuilder.invoke("incF1", 10L, tbBuilderInc.getObject());
tbMapper.invoke("upsertByExampleSelective", tbBuilder.invoke("build"), tbExample.getObject());
rs = DBHelper.execute(sqlSession, "select * from tb where id = 40");
rs.first(); rs.first();
Assert.assertEquals(rs.getInt("inc_f1"), 20); Assert.assertEquals(rs.getInt("inc_f1"), 20);
} }
......
...@@ -250,11 +250,11 @@ public class OldSelectiveEnhancedPluginTest { ...@@ -250,11 +250,11 @@ public class OldSelectiveEnhancedPluginTest {
// sql // sql
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "upsertByExampleSelective", tb.getObject(), TbExample.getObject()); String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "upsertByExampleSelective", tb.getObject(), TbExample.getObject());
Assert.assertEquals(sql, "insert into tb ( id, field_1, inc_f2 ) select 99, 'null', 5 from dual where not exists ( select 1 from tb WHERE ( id = '99' ) ) ; update tb set inc_f2 = 5, inc_f3 = 10 WHERE ( id = '99' )"); Assert.assertEquals(sql, "update tb set inc_f2 = 5, inc_f3 = 10 WHERE ( id = '99' ) ; insert into tb ( id, field_1, inc_f2 ) select 99, 'null', 5 from dual where not exists ( select 1 from tb WHERE ( id = '99' ) )");
Object result = tbMapper.invoke("upsertByExampleSelective", tb.getObject(), TbExample.getObject()); Object result = tbMapper.invoke("upsertByExampleSelective", tb.getObject(), TbExample.getObject());
Assert.assertEquals(result, 1);
result = tbMapper.invoke("upsertByExampleSelective", tb.getObject(), TbExample.getObject());
Assert.assertEquals(result, 0); Assert.assertEquals(result, 0);
result = tbMapper.invoke("upsertByExampleSelective", tb.getObject(), TbExample.getObject());
Assert.assertEquals(result, 1);
} }
}); });
} }
......
...@@ -219,11 +219,11 @@ public class SelectiveEnhancedPluginTest { ...@@ -219,11 +219,11 @@ public class SelectiveEnhancedPluginTest {
// sql // sql
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "upsertByExampleSelective", tb.getObject(), TbExample.getObject(), columns); String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "upsertByExampleSelective", tb.getObject(), TbExample.getObject(), columns);
Assert.assertEquals(sql, "insert into tb ( id , field_1 , inc_f2 ) select 99 , 'null' , 5 from dual where not exists ( select 1 from tb WHERE ( id = '99' ) ) ; update tb set id = 99 , field_1 = 'null' , inc_f2 = 5 WHERE ( id = '99' )"); Assert.assertEquals(sql, "update tb set id = 99 , field_1 = 'null' , inc_f2 = 5 WHERE ( id = '99' ) ; insert into tb ( id , field_1 , inc_f2 ) select 99 , 'null' , 5 from dual where not exists ( select 1 from tb WHERE ( id = '99' ) )");
Object result = tbMapper.invoke("upsertByExampleSelective", tb.getObject(), TbExample.getObject(), columns); Object result = tbMapper.invoke("upsertByExampleSelective", tb.getObject(), TbExample.getObject(), columns);
Assert.assertEquals(result, 1);
result = tbMapper.invoke("upsertByExampleSelective", tb.getObject(), TbExample.getObject(), columns);
Assert.assertEquals(result, 0); Assert.assertEquals(result, 0);
result = tbMapper.invoke("upsertByExampleSelective", tb.getObject(), TbExample.getObject(), columns);
Assert.assertEquals(result, 1);
} }
}); });
} }
......
...@@ -208,9 +208,9 @@ public class UpsertPluginTest { ...@@ -208,9 +208,9 @@ public class UpsertPluginTest {
// sql // sql
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "upsertByExample", tb.getObject(), TbExample.getObject()); String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "upsertByExample", tb.getObject(), TbExample.getObject());
Assert.assertEquals(sql, "insert into tb (id, field1, field2) select 50, 'ts1', 5 from dual where not exists ( select 1 from tb WHERE ( id = '50' ) ) ; update tb set field1 = 'ts1', field2 = 5 WHERE ( id = '50' )"); Assert.assertEquals(sql, "update tb set field1 = 'ts1', field2 = 5 WHERE ( id = '50' ) ; insert into tb (id, field1, field2) select 50, 'ts1', 5 from dual where not exists ( select 1 from tb WHERE ( id = '50' ) )");
Object result = tbMapper.invoke("upsertByExample", tb.getObject(), TbExample.getObject()); tbMapper.invoke("upsertByExample", tb.getObject(), TbExample.getObject());
Assert.assertEquals(result, 1);
tb.set("field2", 20); tb.set("field2", 20);
tbMapper.invoke("upsertByExample", tb.getObject(), TbExample.getObject()); tbMapper.invoke("upsertByExample", tb.getObject(), TbExample.getObject());
...@@ -245,9 +245,8 @@ public class UpsertPluginTest { ...@@ -245,9 +245,8 @@ public class UpsertPluginTest {
// sql // sql
String sql = SqlHelper.getFormatMapperSql(TbBlobsMapper.getObject(), "upsertByExampleWithBLOBs", TbBlobsWithBLOBs.getObject(), TbBlobsExample.getObject()); String sql = SqlHelper.getFormatMapperSql(TbBlobsMapper.getObject(), "upsertByExampleWithBLOBs", TbBlobsWithBLOBs.getObject(), TbBlobsExample.getObject());
Assert.assertEquals(sql, "insert into tb_blobs (id, field1, field2, field3) select 60, 'ts1', 'ts2', 'null' from dual where not exists ( select 1 from tb_blobs WHERE ( id = '60' ) ) ; update tb_blobs set id = 60, field1 = 'ts1', field2 = 'ts2', field3 = 'null' WHERE ( id = '60' )"); Assert.assertEquals(sql, "update tb_blobs set id = 60, field1 = 'ts1', field2 = 'ts2', field3 = 'null' WHERE ( id = '60' ) ; insert into tb_blobs (id, field1, field2, field3) select 60, 'ts1', 'ts2', 'null' from dual where not exists ( select 1 from tb_blobs WHERE ( id = '60' ) )");
Object result = TbBlobsMapper.invoke("upsertByExampleWithBLOBs", TbBlobsWithBLOBs.getObject(), TbBlobsExample.getObject()); TbBlobsMapper.invoke("upsertByExampleWithBLOBs", TbBlobsWithBLOBs.getObject(), TbBlobsExample.getObject());
Assert.assertEquals(result, 1);
TbBlobsWithBLOBs.set("field2", "ts3"); TbBlobsWithBLOBs.set("field2", "ts3");
TbBlobsMapper.invoke("upsertByExampleWithBLOBs", TbBlobsWithBLOBs.getObject(), TbBlobsExample.getObject()); TbBlobsMapper.invoke("upsertByExampleWithBLOBs", TbBlobsWithBLOBs.getObject(), TbBlobsExample.getObject());
...@@ -271,9 +270,8 @@ public class UpsertPluginTest { ...@@ -271,9 +270,8 @@ public class UpsertPluginTest {
// sql // sql
sql = SqlHelper.getFormatMapperSql(TbSingleBlobMapper.getObject(), "upsertByExampleWithBLOBs", TbSingleBlob.getObject(), TbSingleBlobExample.getObject()); sql = SqlHelper.getFormatMapperSql(TbSingleBlobMapper.getObject(), "upsertByExampleWithBLOBs", TbSingleBlob.getObject(), TbSingleBlobExample.getObject());
Assert.assertEquals(sql, "insert into tb_single_blob (id, field2, field1) select 70, 3, 'ts1' from dual where not exists ( select 1 from tb_single_blob WHERE ( id = '70' ) ) ; update tb_single_blob set id = 70, field2 = 3, field1 = 'ts1' WHERE ( id = '70' )"); Assert.assertEquals(sql, "update tb_single_blob set id = 70, field2 = 3, field1 = 'ts1' WHERE ( id = '70' ) ; insert into tb_single_blob (id, field2, field1) select 70, 3, 'ts1' from dual where not exists ( select 1 from tb_single_blob WHERE ( id = '70' ) )");
result = TbSingleBlobMapper.invoke("upsertByExampleWithBLOBs", TbSingleBlob.getObject(), TbSingleBlobExample.getObject()); TbSingleBlobMapper.invoke("upsertByExampleWithBLOBs", TbSingleBlob.getObject(), TbSingleBlobExample.getObject());
Assert.assertEquals(result, 1);
TbSingleBlob.set("field1", "ts2"); TbSingleBlob.set("field1", "ts2");
TbSingleBlobMapper.invoke("upsertByExampleWithBLOBs", TbSingleBlob.getObject(), TbSingleBlobExample.getObject()); TbSingleBlobMapper.invoke("upsertByExampleWithBLOBs", TbSingleBlob.getObject(), TbSingleBlobExample.getObject());
...@@ -307,9 +305,8 @@ public class UpsertPluginTest { ...@@ -307,9 +305,8 @@ public class UpsertPluginTest {
// sql // sql
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "upsertByExampleSelective", tb.getObject(), TbExample.getObject()); String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "upsertByExampleSelective", tb.getObject(), TbExample.getObject());
Assert.assertEquals(sql, "insert into tb ( id, field1 ) select 100, 'ts1' from dual where not exists ( select 1 from tb WHERE ( id = '100' ) ) ; update tb set field1 = 'ts1' WHERE ( id = '100' )"); Assert.assertEquals(sql, "update tb set field1 = 'ts1' WHERE ( id = '100' ) ; insert into tb ( id, field1 ) select 100, 'ts1' from dual where not exists ( select 1 from tb WHERE ( id = '100' ) )");
Object result = tbMapper.invoke("upsertByExampleSelective", tb.getObject(), TbExample.getObject()); tbMapper.invoke("upsertByExampleSelective", tb.getObject(), TbExample.getObject());
Assert.assertEquals(result, 1);
// 2. blobs // 2. blobs
ObjectUtil TbBlobsMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbBlobsMapper"))); ObjectUtil TbBlobsMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbBlobsMapper")));
...@@ -325,9 +322,8 @@ public class UpsertPluginTest { ...@@ -325,9 +322,8 @@ public class UpsertPluginTest {
// sql // sql
sql = SqlHelper.getFormatMapperSql(TbBlobsMapper.getObject(), "upsertByExampleSelective", TbBlobsWithBLOBs.getObject(), TbBlobsExample.getObject()); sql = SqlHelper.getFormatMapperSql(TbBlobsMapper.getObject(), "upsertByExampleSelective", TbBlobsWithBLOBs.getObject(), TbBlobsExample.getObject());
Assert.assertEquals(sql, "insert into tb_blobs ( id, field1, field2 ) select 200, 'ts1', 'ts2' from dual where not exists ( select 1 from tb_blobs WHERE ( id = '60' ) ) ; update tb_blobs set field1 = 'ts1', field2 = 'ts2' WHERE ( id = '60' )"); Assert.assertEquals(sql, "update tb_blobs set field1 = 'ts1', field2 = 'ts2' WHERE ( id = '60' ) ; insert into tb_blobs ( id, field1, field2 ) select 200, 'ts1', 'ts2' from dual where not exists ( select 1 from tb_blobs WHERE ( id = '60' ) )");
result = TbBlobsMapper.invoke("upsertByExampleSelective", TbBlobsWithBLOBs.getObject(), TbBlobsExample.getObject()); TbBlobsMapper.invoke("upsertByExampleSelective", TbBlobsWithBLOBs.getObject(), TbBlobsExample.getObject());
Assert.assertEquals(result, 1);
} }
}); });
} }
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
package com.itfsw.mybatis.generator.plugins.tools; package com.itfsw.mybatis.generator.plugins.tools;
import com.sun.deploy.util.StringUtils;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
...@@ -80,10 +82,17 @@ public class ObjectUtil { ...@@ -80,10 +82,17 @@ public class ObjectUtil {
* @throws NoSuchFieldException * @throws NoSuchFieldException
* @throws IllegalAccessException * @throws IllegalAccessException
*/ */
public ObjectUtil set(String filedName, Object value) throws NoSuchFieldException, IllegalAccessException { public ObjectUtil set(String filedName, Object value) throws NoSuchFieldException, IllegalAccessException, InstantiationException, ClassNotFoundException {
Field field = this.getDeclaredField(filedName); String[] strs = StringUtils.splitString(filedName, ".");
field.setAccessible(true); if (strs.length > 1) {
field.set(this.object, value); Object obj = this.get(strs[0]);
new ObjectUtil(obj).set(filedName.replaceFirst("\\w+\\.", ""), value);
} else {
Field field = this.getDeclaredField(filedName);
field.setAccessible(true);
field.set(this.object, value);
}
return this; return this;
} }
......
...@@ -24,7 +24,7 @@ CREATE TABLE `tb` ( ...@@ -24,7 +24,7 @@ CREATE TABLE `tb` (
`field1` varchar(255) DEFAULT NULL COMMENT '注释2', `field1` varchar(255) DEFAULT NULL COMMENT '注释2',
`inc_f1` bigint(20) NOT NULL DEFAULT '0', `inc_f1` bigint(20) NOT NULL DEFAULT '0',
`inc_f2` bigint(20) NOT NULL DEFAULT '0', `inc_f2` bigint(20) NOT NULL DEFAULT '0',
`inc_f3` bigint(20) NOT NULL DEFAULT '0', `inc_f3` bigint(20) DEFAULT '0',
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
......
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