mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
add UpdateWrapperTest.java
This commit is contained in:
parent
e7fc09922b
commit
f36a44785c
@ -718,7 +718,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
|
|
||||||
for (String modifyAttr : updateColumns) {
|
for (String modifyAttr : updateColumns) {
|
||||||
if (rawValueMap.containsKey(modifyAttr)) {
|
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 {
|
} else {
|
||||||
stringJoiner.add(wrap(modifyAttr) + EQUALS_PLACEHOLDER);
|
stringJoiner.add(wrap(modifyAttr) + EQUALS_PLACEHOLDER);
|
||||||
}
|
}
|
||||||
@ -789,7 +789,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
|
|
||||||
for (String modifyAttr : updateColumns) {
|
for (String modifyAttr : updateColumns) {
|
||||||
if (rawValueMap.containsKey(modifyAttr)) {
|
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 {
|
} else {
|
||||||
stringJoiner.add(wrap(modifyAttr) + EQUALS_PLACEHOLDER);
|
stringJoiner.add(wrap(modifyAttr) + EQUALS_PLACEHOLDER);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -105,7 +105,7 @@ public class Row extends LinkedHashMap<String, Object> implements UpdateWrapper
|
|||||||
|
|
||||||
SqlUtil.keepColumnSafely(column);
|
SqlUtil.keepColumnSafely(column);
|
||||||
|
|
||||||
if (value instanceof QueryWrapper || value instanceof QueryCondition) {
|
if (value instanceof QueryWrapper || value instanceof QueryCondition || value instanceof QueryColumn) {
|
||||||
setRaw(column, value);
|
setRaw(column, value);
|
||||||
} else {
|
} else {
|
||||||
super.put(column, value);
|
super.put(column, value);
|
||||||
@ -116,8 +116,8 @@ public class Row extends LinkedHashMap<String, Object> implements UpdateWrapper
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Row set(QueryColumn queryColumn, Object value) {
|
public Row set(QueryColumn queryColumn, Object value) {
|
||||||
if (value instanceof QueryWrapper || value instanceof QueryCondition) {
|
if (value instanceof QueryWrapper || value instanceof QueryCondition || value instanceof QueryColumn) {
|
||||||
seRaw(queryColumn, value);
|
setRaw(queryColumn, value);
|
||||||
} else {
|
} else {
|
||||||
super.put(queryColumn.getName(), value);
|
super.put(queryColumn.getName(), value);
|
||||||
}
|
}
|
||||||
@ -328,10 +328,10 @@ public class Row extends LinkedHashMap<String, Object> implements UpdateWrapper
|
|||||||
return attrs;
|
return attrs;
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String,RawValue> getRawValueMap(){
|
Map<String, RawValue> getRawValueMap() {
|
||||||
Map<String,RawValue> map = new HashMap<>();
|
Map<String, RawValue> map = new HashMap<>();
|
||||||
forEach((s, o) -> {
|
forEach((s, o) -> {
|
||||||
if (o instanceof RawValue){
|
if (o instanceof RawValue) {
|
||||||
map.put(s, (RawValue) o);
|
map.put(s, (RawValue) o);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -339,8 +339,6 @@ public class Row extends LinkedHashMap<String, Object> implements UpdateWrapper
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void resetByAttrs(Set<String> resetAttrs) {
|
void resetByAttrs(Set<String> resetAttrs) {
|
||||||
keySet().removeIf(s -> !resetAttrs.contains(s));
|
keySet().removeIf(s -> !resetAttrs.contains(s));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -513,7 +513,6 @@ public class TableInfo {
|
|||||||
MetaObject metaObject = EntityMetaObject.forObject(entity, reflectorFactory);
|
MetaObject metaObject = EntityMetaObject.forObject(entity, reflectorFactory);
|
||||||
Set<String> columns = new LinkedHashSet<>(); //需使用 LinkedHashSet 保证 columns 的顺序
|
Set<String> columns = new LinkedHashSet<>(); //需使用 LinkedHashSet 保证 columns 的顺序
|
||||||
if (entity instanceof UpdateWrapper) {
|
if (entity instanceof UpdateWrapper) {
|
||||||
// Set<String> properties = ((UpdateWrapper) entity).getModifyAttrs();
|
|
||||||
Map<String, Object> updates = ((UpdateWrapper) entity).getUpdates();
|
Map<String, Object> updates = ((UpdateWrapper) entity).getUpdates();
|
||||||
if (updates.isEmpty()) {
|
if (updates.isEmpty()) {
|
||||||
return Collections.emptySet();
|
return Collections.emptySet();
|
||||||
|
|||||||
@ -16,6 +16,8 @@
|
|||||||
package com.mybatisflex.core.update;
|
package com.mybatisflex.core.update;
|
||||||
|
|
||||||
import com.mybatisflex.core.dialect.IDialect;
|
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.QueryCondition;
|
||||||
import com.mybatisflex.core.query.QueryWrapper;
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
|
|
||||||
@ -43,6 +45,11 @@ public class RawValue implements Serializable {
|
|||||||
return ((QueryCondition) object).toSql(null, dialect);
|
return ((QueryCondition) object).toSql(null, dialect);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (object instanceof QueryColumn){
|
||||||
|
String s = CPI.toSelectSql((QueryColumn) object, null, dialect);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
return object.toString();
|
return object.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -34,7 +34,7 @@ public interface UpdateWrapper extends Serializable {
|
|||||||
|
|
||||||
|
|
||||||
default UpdateWrapper set(String property, Object value) {
|
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);
|
setRaw(property, value);
|
||||||
} else {
|
} else {
|
||||||
getUpdates().put(property, value);
|
getUpdates().put(property, value);
|
||||||
@ -44,8 +44,8 @@ public interface UpdateWrapper extends Serializable {
|
|||||||
|
|
||||||
|
|
||||||
default <T> UpdateWrapper set(LambdaGetter<T> getter, Object value) {
|
default <T> UpdateWrapper set(LambdaGetter<T> getter, Object value) {
|
||||||
if (value instanceof QueryWrapper || value instanceof QueryCondition) {
|
if (value instanceof QueryWrapper || value instanceof QueryCondition || value instanceof QueryColumn) {
|
||||||
seRaw(getter, value);
|
setRaw(getter, value);
|
||||||
} else {
|
} else {
|
||||||
getUpdates().put(LambdaUtil.getFieldName(getter), value);
|
getUpdates().put(LambdaUtil.getFieldName(getter), value);
|
||||||
}
|
}
|
||||||
@ -55,8 +55,8 @@ public interface UpdateWrapper extends Serializable {
|
|||||||
|
|
||||||
|
|
||||||
default <T> UpdateWrapper set(QueryColumn queryColumn, Object value) {
|
default <T> UpdateWrapper set(QueryColumn queryColumn, Object value) {
|
||||||
if (value instanceof QueryWrapper || value instanceof QueryCondition) {
|
if (value instanceof QueryWrapper || value instanceof QueryCondition || value instanceof QueryColumn) {
|
||||||
seRaw(queryColumn, value);
|
setRaw(queryColumn, value);
|
||||||
} else {
|
} else {
|
||||||
getUpdates().put(queryColumn.getName(), value);
|
getUpdates().put(queryColumn.getName(), value);
|
||||||
}
|
}
|
||||||
@ -69,12 +69,12 @@ public interface UpdateWrapper extends Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
default <T> UpdateWrapper seRaw(LambdaGetter<T> getter, Object value) {
|
default <T> UpdateWrapper setRaw(LambdaGetter<T> getter, Object value) {
|
||||||
getUpdates().put(LambdaUtil.getFieldName(getter), new RawValue(value));
|
getUpdates().put(LambdaUtil.getFieldName(getter), new RawValue(value));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
default <T> UpdateWrapper seRaw(QueryColumn queryColumn, Object value) {
|
default <T> UpdateWrapper setRaw(QueryColumn queryColumn, Object value) {
|
||||||
getUpdates().put(queryColumn.getName(), new RawValue(value));
|
getUpdates().put(queryColumn.getName(), new RawValue(value));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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<Account> 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<Account> 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<Account> 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<Account> accounts4 = accountMapper.selectAll();
|
||||||
|
System.out.println(accounts4);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user