Commit d8438373 authored by hewei's avatar hewei

bug:upsertByExampleXXX insert和update写反了

parent 2c70cdfb
......@@ -171,9 +171,9 @@ public class OldSelectiveEnhancedPlugin extends BasePlugin {
// ====================================== 5. upsertByExampleSelective ======================================
if ("upsertByExampleSelective".equals(id)) {
List<XmlElement> eles = XmlElementGeneratorTools.findXmlElements(xmlElement, "trim");
this.replaceEle(eles.get(0), "record.", introspectedTable);
this.replaceEle(eles.get(1), "record.", introspectedTable);
// 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");
for (XmlElement ele : eles1) {
......
......@@ -334,44 +334,6 @@ public class UpsertPlugin extends BasePlugin {
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools.useGeneratedKeys(eleUpsertByExampleSelective, introspectedTable, "record.");
// insert
eleUpsertByExampleSelective.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
// selective
insertChooseEle = new XmlElement("choose");
eleUpsertByExampleSelective.addElement(insertChooseEle);
insertWhenEle = new XmlElement("when");
insertWhenEle.addAttribute(new Attribute("test", "selective != null and selective.length > 0"));
insertChooseEle.addElement(insertWhenEle);
insertForeachEle = new XmlElement("foreach");
insertForeachEle.addAttribute(new Attribute("collection", "selective"));
insertForeachEle.addAttribute(new Attribute("item", "column"));
insertForeachEle.addAttribute(new Attribute("open", "("));
insertForeachEle.addAttribute(new Attribute("separator", ","));
insertForeachEle.addAttribute(new Attribute("close", ")"));
insertForeachEle.addElement(new TextElement("${column.value}"));
insertWhenEle.addElement(insertForeachEle);
insertOtherwiseEle = new XmlElement("otherwise");
insertOtherwiseEle.addElement(XmlElementGeneratorTools.generateKeysSelective(
ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()),
"record."
));
insertChooseEle.addElement(insertOtherwiseEle);
insertTrimElement = new XmlElement("trim");
insertTrimElement.addAttribute(new Attribute("prefix", "("));
insertTrimElement.addAttribute(new Attribute("suffix", ")"));
insertTrimElement.addAttribute(new Attribute("suffixOverrides", ","));
insertOtherwiseEle.addElement(insertTrimElement);
this.generateExistsClause(introspectedTable, eleUpsertByExampleSelective, true, columns);
// multiQueries
eleUpsertByExampleSelective.addElement(new TextElement(";"));
// update
eleUpsertByExampleSelective.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
eleUpsertByExampleSelective.addElement(new TextElement("set"));
......@@ -412,6 +374,45 @@ public class UpsertPlugin extends BasePlugin {
// update where
eleUpsertByExampleSelective.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));
// multiQueries
eleUpsertByExampleSelective.addElement(new TextElement(";"));
// insert
eleUpsertByExampleSelective.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
// selective
insertChooseEle = new XmlElement("choose");
eleUpsertByExampleSelective.addElement(insertChooseEle);
insertWhenEle = new XmlElement("when");
insertWhenEle.addAttribute(new Attribute("test", "selective != null and selective.length > 0"));
insertChooseEle.addElement(insertWhenEle);
insertForeachEle = new XmlElement("foreach");
insertForeachEle.addAttribute(new Attribute("collection", "selective"));
insertForeachEle.addAttribute(new Attribute("item", "column"));
insertForeachEle.addAttribute(new Attribute("open", "("));
insertForeachEle.addAttribute(new Attribute("separator", ","));
insertForeachEle.addAttribute(new Attribute("close", ")"));
insertForeachEle.addElement(new TextElement("${column.value}"));
insertWhenEle.addElement(insertForeachEle);
insertOtherwiseEle = new XmlElement("otherwise");
insertOtherwiseEle.addElement(XmlElementGeneratorTools.generateKeysSelective(
ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()),
"record."
));
insertChooseEle.addElement(insertOtherwiseEle);
insertTrimElement = new XmlElement("trim");
insertTrimElement.addAttribute(new Attribute("prefix", "("));
insertTrimElement.addAttribute(new Attribute("suffix", ")"));
insertTrimElement.addAttribute(new Attribute("suffixOverrides", ","));
insertOtherwiseEle.addElement(insertTrimElement);
this.generateExistsClause(introspectedTable, eleUpsertByExampleSelective, true, columns);
document.getRootElement().addElement(eleUpsertByExampleSelective);
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。");
}
......@@ -463,14 +464,6 @@ public class UpsertPlugin extends BasePlugin {
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools.useGeneratedKeys(eleUpsertByExampleSelective, introspectedTable, "record.");
// insert
eleUpsertByExampleSelective.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
eleUpsertByExampleSelective.addElement(XmlElementGeneratorTools.generateKeysSelective(columns, "record."));
this.generateExistsClause(introspectedTable, eleUpsertByExampleSelective, true, columns);
// multiQueries
eleUpsertByExampleSelective.addElement(new TextElement(";"));
// update
eleUpsertByExampleSelective.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
eleUpsertByExampleSelective.addElement(new TextElement("set"));
......@@ -492,6 +485,14 @@ public class UpsertPlugin extends BasePlugin {
// update where
eleUpsertByExampleSelective.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));
// multiQueries
eleUpsertByExampleSelective.addElement(new TextElement(";"));
// insert
eleUpsertByExampleSelective.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
eleUpsertByExampleSelective.addElement(XmlElementGeneratorTools.generateKeysSelective(columns, "record."));
this.generateExistsClause(introspectedTable, eleUpsertByExampleSelective, true, columns);
document.getRootElement().addElement(eleUpsertByExampleSelective);
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。");
}
......@@ -556,16 +557,6 @@ public class UpsertPlugin extends BasePlugin {
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools.useGeneratedKeys(eleUpsertByExampleWithBLOBs, introspectedTable, "record.");
// insert
eleUpsertByExampleWithBLOBs.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
for (Element element : XmlElementGeneratorTools.generateKeys(columns)) {
eleUpsertByExampleWithBLOBs.addElement(element);
}
this.generateExistsClause(introspectedTable, eleUpsertByExampleWithBLOBs, false, columns);
// multiQueries
eleUpsertByExampleWithBLOBs.addElement(new TextElement(";"));
// update
eleUpsertByExampleWithBLOBs.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
eleUpsertByExampleWithBLOBs.addElement(new TextElement("set"));
......@@ -583,6 +574,16 @@ public class UpsertPlugin extends BasePlugin {
// update where
eleUpsertByExampleWithBLOBs.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));
// multiQueries
eleUpsertByExampleWithBLOBs.addElement(new TextElement(";"));
// insert
eleUpsertByExampleWithBLOBs.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
for (Element element : XmlElementGeneratorTools.generateKeys(columns)) {
eleUpsertByExampleWithBLOBs.addElement(element);
}
this.generateExistsClause(introspectedTable, eleUpsertByExampleWithBLOBs, false, columns);
document.getRootElement().addElement(eleUpsertByExampleWithBLOBs);
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。");
}
......@@ -646,16 +647,6 @@ public class UpsertPlugin extends BasePlugin {
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools.useGeneratedKeys(eleUpsertByExample, introspectedTable, "record.");
// insert
eleUpsertByExample.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
for (Element element : XmlElementGeneratorTools.generateKeys(columns)) {
eleUpsertByExample.addElement(element);
}
this.generateExistsClause(introspectedTable, eleUpsertByExample, false, columns);
// multiQueries
eleUpsertByExample.addElement(new TextElement(";"));
// update
eleUpsertByExample.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
eleUpsertByExample.addElement(new TextElement("set"));
......@@ -673,6 +664,16 @@ public class UpsertPlugin extends BasePlugin {
// update where
eleUpsertByExample.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));
// multiQueries
eleUpsertByExample.addElement(new TextElement(";"));
// insert
eleUpsertByExample.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
for (Element element : XmlElementGeneratorTools.generateKeys(columns)) {
eleUpsertByExample.addElement(element);
}
this.generateExistsClause(introspectedTable, eleUpsertByExample, false, columns);
document.getRootElement().addElement(eleUpsertByExample);
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。");
}
......
......@@ -347,13 +347,48 @@ public class IncrementsPluginTest {
// 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);
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' ) )");
tbMapper.invoke("upsertByExample", tbBuilder.invoke("build"), tbExample.getObject());
// 再次执行触发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 = 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();
Assert.assertEquals(rs.getInt("inc_f1"), 20);
}
......
......@@ -250,11 +250,11 @@ public class OldSelectiveEnhancedPluginTest {
// sql
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());
Assert.assertEquals(result, 1);
result = tbMapper.invoke("upsertByExampleSelective", tb.getObject(), TbExample.getObject());
Assert.assertEquals(result, 0);
result = tbMapper.invoke("upsertByExampleSelective", tb.getObject(), TbExample.getObject());
Assert.assertEquals(result, 1);
}
});
}
......
......@@ -219,11 +219,11 @@ public class SelectiveEnhancedPluginTest {
// sql
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);
Assert.assertEquals(result, 1);
result = tbMapper.invoke("upsertByExampleSelective", tb.getObject(), TbExample.getObject(), columns);
Assert.assertEquals(result, 0);
result = tbMapper.invoke("upsertByExampleSelective", tb.getObject(), TbExample.getObject(), columns);
Assert.assertEquals(result, 1);
}
});
}
......
......@@ -208,9 +208,9 @@ public class UpsertPluginTest {
// sql
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' )");
Object result = tbMapper.invoke("upsertByExample", tb.getObject(), TbExample.getObject());
Assert.assertEquals(result, 1);
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' ) )");
tbMapper.invoke("upsertByExample", tb.getObject(), TbExample.getObject());
tb.set("field2", 20);
tbMapper.invoke("upsertByExample", tb.getObject(), TbExample.getObject());
......@@ -245,9 +245,8 @@ public class UpsertPluginTest {
// sql
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' )");
Object result = TbBlobsMapper.invoke("upsertByExampleWithBLOBs", TbBlobsWithBLOBs.getObject(), TbBlobsExample.getObject());
Assert.assertEquals(result, 1);
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' ) )");
TbBlobsMapper.invoke("upsertByExampleWithBLOBs", TbBlobsWithBLOBs.getObject(), TbBlobsExample.getObject());
TbBlobsWithBLOBs.set("field2", "ts3");
TbBlobsMapper.invoke("upsertByExampleWithBLOBs", TbBlobsWithBLOBs.getObject(), TbBlobsExample.getObject());
......@@ -271,9 +270,8 @@ public class UpsertPluginTest {
// sql
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' )");
result = TbSingleBlobMapper.invoke("upsertByExampleWithBLOBs", TbSingleBlob.getObject(), TbSingleBlobExample.getObject());
Assert.assertEquals(result, 1);
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' ) )");
TbSingleBlobMapper.invoke("upsertByExampleWithBLOBs", TbSingleBlob.getObject(), TbSingleBlobExample.getObject());
TbSingleBlob.set("field1", "ts2");
TbSingleBlobMapper.invoke("upsertByExampleWithBLOBs", TbSingleBlob.getObject(), TbSingleBlobExample.getObject());
......@@ -307,9 +305,8 @@ public class UpsertPluginTest {
// sql
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' )");
Object result = tbMapper.invoke("upsertByExampleSelective", tb.getObject(), TbExample.getObject());
Assert.assertEquals(result, 1);
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' ) )");
tbMapper.invoke("upsertByExampleSelective", tb.getObject(), TbExample.getObject());
// 2. blobs
ObjectUtil TbBlobsMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbBlobsMapper")));
......@@ -325,9 +322,8 @@ public class UpsertPluginTest {
// sql
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' )");
result = TbBlobsMapper.invoke("upsertByExampleSelective", TbBlobsWithBLOBs.getObject(), TbBlobsExample.getObject());
Assert.assertEquals(result, 1);
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' ) )");
TbBlobsMapper.invoke("upsertByExampleSelective", TbBlobsWithBLOBs.getObject(), TbBlobsExample.getObject());
}
});
}
......
......@@ -16,6 +16,8 @@
package com.itfsw.mybatis.generator.plugins.tools;
import com.sun.deploy.util.StringUtils;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
......@@ -80,10 +82,17 @@ public class ObjectUtil {
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
public ObjectUtil set(String filedName, Object value) throws NoSuchFieldException, IllegalAccessException {
public ObjectUtil set(String filedName, Object value) throws NoSuchFieldException, IllegalAccessException, InstantiationException, ClassNotFoundException {
String[] strs = StringUtils.splitString(filedName, ".");
if (strs.length > 1) {
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;
}
......
......@@ -24,7 +24,7 @@ CREATE TABLE `tb` (
`field1` varchar(255) DEFAULT NULL COMMENT '注释2',
`inc_f1` 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`)
) 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