diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java index 07149605..0157e2ce 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java @@ -718,7 +718,7 @@ public class CommonsDialectImpl implements IDialect { for (String modifyAttr : updateColumns) { if (rawValueMap.containsKey(modifyAttr)) { - stringJoiner.add(wrap(modifyAttr)).add(EQUALS).add(rawValueMap.get(modifyAttr).toSql(this)); + stringJoiner.add(wrap(modifyAttr) + EQUALS + rawValueMap.get(modifyAttr).toSql(this)); } else { stringJoiner.add(wrap(modifyAttr) + EQUALS_PLACEHOLDER); } @@ -789,7 +789,7 @@ public class CommonsDialectImpl implements IDialect { for (String modifyAttr : updateColumns) { if (rawValueMap.containsKey(modifyAttr)) { - stringJoiner.add(wrap(modifyAttr)).add(EQUALS).add(rawValueMap.get(modifyAttr).toSql(this)); + stringJoiner.add(wrap(modifyAttr) + EQUALS + rawValueMap.get(modifyAttr).toSql(this)); } else { stringJoiner.add(wrap(modifyAttr) + EQUALS_PLACEHOLDER); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Row.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Row.java index 0227bd6a..a266030c 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Row.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Row.java @@ -105,7 +105,7 @@ public class Row extends LinkedHashMap implements UpdateWrapper SqlUtil.keepColumnSafely(column); - if (value instanceof QueryWrapper || value instanceof QueryCondition) { + if (value instanceof QueryWrapper || value instanceof QueryCondition || value instanceof QueryColumn) { setRaw(column, value); } else { super.put(column, value); @@ -116,8 +116,8 @@ public class Row extends LinkedHashMap implements UpdateWrapper @Override public Row set(QueryColumn queryColumn, Object value) { - if (value instanceof QueryWrapper || value instanceof QueryCondition) { - seRaw(queryColumn, value); + if (value instanceof QueryWrapper || value instanceof QueryCondition || value instanceof QueryColumn) { + setRaw(queryColumn, value); } else { super.put(queryColumn.getName(), value); } @@ -317,7 +317,7 @@ public class Row extends LinkedHashMap implements UpdateWrapper this.primaryKeys = primaryKeys; } - Set getModifyAttrs() { + Set getModifyAttrs() { int pkCount = primaryKeys != null ? primaryKeys.length : 0; if (pkCount == 0) { return keySet(); @@ -328,10 +328,10 @@ public class Row extends LinkedHashMap implements UpdateWrapper return attrs; } - Map getRawValueMap(){ - Map map = new HashMap<>(); + Map getRawValueMap() { + Map map = new HashMap<>(); forEach((s, o) -> { - if (o instanceof RawValue){ + if (o instanceof RawValue) { map.put(s, (RawValue) o); } }); @@ -339,9 +339,7 @@ public class Row extends LinkedHashMap implements UpdateWrapper } - - - void resetByAttrs(Set resetAttrs) { + void resetByAttrs(Set resetAttrs) { keySet().removeIf(s -> !resetAttrs.contains(s)); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java index 2f0172d6..0bf15ef6 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java @@ -513,7 +513,6 @@ public class TableInfo { MetaObject metaObject = EntityMetaObject.forObject(entity, reflectorFactory); Set columns = new LinkedHashSet<>(); //需使用 LinkedHashSet 保证 columns 的顺序 if (entity instanceof UpdateWrapper) { -// Set properties = ((UpdateWrapper) entity).getModifyAttrs(); Map updates = ((UpdateWrapper) entity).getUpdates(); if (updates.isEmpty()) { return Collections.emptySet(); diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/RawValue.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/RawValue.java index 4570e26c..6b2e7c8c 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/RawValue.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/RawValue.java @@ -16,6 +16,8 @@ package com.mybatisflex.core.update; import com.mybatisflex.core.dialect.IDialect; +import com.mybatisflex.core.query.CPI; +import com.mybatisflex.core.query.QueryColumn; import com.mybatisflex.core.query.QueryCondition; import com.mybatisflex.core.query.QueryWrapper; @@ -43,6 +45,11 @@ public class RawValue implements Serializable { return ((QueryCondition) object).toSql(null, dialect); } + if (object instanceof QueryColumn){ + String s = CPI.toSelectSql((QueryColumn) object, null, dialect); + return s; + } + return object.toString(); } } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/UpdateWrapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/UpdateWrapper.java index 295a1d63..fd59ac52 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/UpdateWrapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/UpdateWrapper.java @@ -34,7 +34,7 @@ public interface UpdateWrapper extends Serializable { default UpdateWrapper set(String property, Object value) { - if (value instanceof QueryWrapper || value instanceof QueryCondition) { + if (value instanceof QueryWrapper || value instanceof QueryCondition || value instanceof QueryColumn) { setRaw(property, value); } else { getUpdates().put(property, value); @@ -44,8 +44,8 @@ public interface UpdateWrapper extends Serializable { default UpdateWrapper set(LambdaGetter getter, Object value) { - if (value instanceof QueryWrapper || value instanceof QueryCondition) { - seRaw(getter, value); + if (value instanceof QueryWrapper || value instanceof QueryCondition || value instanceof QueryColumn) { + setRaw(getter, value); } else { getUpdates().put(LambdaUtil.getFieldName(getter), value); } @@ -55,8 +55,8 @@ public interface UpdateWrapper extends Serializable { default UpdateWrapper set(QueryColumn queryColumn, Object value) { - if (value instanceof QueryWrapper || value instanceof QueryCondition) { - seRaw(queryColumn, value); + if (value instanceof QueryWrapper || value instanceof QueryCondition || value instanceof QueryColumn) { + setRaw(queryColumn, value); } else { getUpdates().put(queryColumn.getName(), value); } @@ -69,12 +69,12 @@ public interface UpdateWrapper extends Serializable { } - default UpdateWrapper seRaw(LambdaGetter getter, Object value) { + default UpdateWrapper setRaw(LambdaGetter getter, Object value) { getUpdates().put(LambdaUtil.getFieldName(getter), new RawValue(value)); return this; } - default UpdateWrapper seRaw(QueryColumn queryColumn, Object value) { + default UpdateWrapper setRaw(QueryColumn queryColumn, Object value) { getUpdates().put(queryColumn.getName(), new RawValue(value)); return this; } diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/UpdateWrapperTest.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/UpdateWrapperTest.java new file mode 100644 index 00000000..2f2faa9a --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/UpdateWrapperTest.java @@ -0,0 +1,77 @@ +package com.mybatisflex.test; + +import com.mybatisflex.core.MybatisFlexBootstrap; +import com.mybatisflex.core.audit.AuditManager; +import com.mybatisflex.core.audit.ConsoleMessageCollector; +import com.mybatisflex.core.audit.MessageCollector; +import com.mybatisflex.core.update.UpdateWrapper; +import com.mybatisflex.core.util.UpdateEntity; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; + +import javax.sql.DataSource; +import java.util.List; + +import static com.mybatisflex.test.table.AccountTableDef.ACCOUNT; + +public class UpdateWrapperTest { + + public static void main(String[] args) { + DataSource dataSource = new EmbeddedDatabaseBuilder() + .setType(EmbeddedDatabaseType.H2) + .addScript("schema.sql") + .addScript("data.sql") + .build(); + + MybatisFlexBootstrap bootstrap = MybatisFlexBootstrap.getInstance() + .setDataSource(dataSource) + .addMapper(AccountMapper.class) + .start(); + + //开启审计功能 + AuditManager.setAuditEnable(true); + + //设置 SQL 审计收集器 + MessageCollector collector = new ConsoleMessageCollector(); + AuditManager.setMessageCollector(collector); + + + AccountMapper accountMapper = bootstrap.getMapper(AccountMapper.class); + + List accounts1 = accountMapper.selectAll(); + System.out.println(accounts1); + + + System.out.println("//////////account2"); + + Account account = UpdateEntity.of(Account.class, 1); + UpdateWrapper wrapper = (UpdateWrapper) account; + wrapper.setRaw("age", "age + 1"); + accountMapper.update(account); + + List accounts2 = accountMapper.selectAll(); + System.out.println(accounts2); + + + System.out.println("//////////account3"); + + Account account3 = UpdateEntity.of(Account.class, 1); + UpdateWrapper wrapper3 = (UpdateWrapper) account3; + wrapper3.setRaw(Account::getAge, "age + 1"); + accountMapper.update(account3); + + List accounts3 = accountMapper.selectAll(); + System.out.println(accounts3); + + + System.out.println("//////////account4"); + + Account account4 = UpdateEntity.of(Account.class, 1); + UpdateWrapper wrapper4 = (UpdateWrapper) account4; + wrapper4.setRaw(ACCOUNT.AGE, ACCOUNT.AGE.add(1)); + accountMapper.update(account4); + + List accounts4 = accountMapper.selectAll(); + System.out.println(accounts4); + } +}