mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
feat: updateWrapper add setApply api
This commit is contained in:
parent
24212523d7
commit
18b2394e7e
@ -12,14 +12,11 @@ import com.github.yulichang.toolkit.ReflectionKit;
|
|||||||
import com.github.yulichang.toolkit.*;
|
import com.github.yulichang.toolkit.*;
|
||||||
import com.github.yulichang.wrapper.interfaces.Update;
|
import com.github.yulichang.wrapper.interfaces.Update;
|
||||||
import com.github.yulichang.wrapper.interfaces.UpdateChain;
|
import com.github.yulichang.wrapper.interfaces.UpdateChain;
|
||||||
import lombok.AllArgsConstructor;
|
import com.github.yulichang.wrapper.segments.FuncConsumer;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -111,42 +108,36 @@ public class UpdateJoinWrapper<T> extends JoinAbstractLambdaWrapper<T, UpdateJoi
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <R> UpdateJoinWrapper<T> set(boolean condition, SFunction<R, ?> column, Object val, String mapping) {
|
public <R> UpdateJoinWrapper<T> set(boolean condition, SFunction<R, ?> column, Object val, String mapping) {
|
||||||
return maybeDo(condition, () -> {
|
return maybeDo(condition, () -> getUpdateSet().add(new UpdateSet(column, val, mapping, false, null)));
|
||||||
if (Objects.isNull(updateSet)) {
|
|
||||||
updateSet = new ArrayList<>();
|
|
||||||
}
|
|
||||||
updateSet.add(new UpdateSet(column, val, mapping, false, null));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <R, V> UpdateJoinWrapper<T> set(boolean condition, SFunction<R, ?> column, SFunction<V, ?> val, String mapping) {
|
public <R, V> UpdateJoinWrapper<T> set(boolean condition, SFunction<R, ?> column, SFunction<V, ?> val, String mapping) {
|
||||||
return maybeDo(condition, () -> {
|
return maybeDo(condition, () -> getUpdateSet().add(new UpdateSet(column, val, mapping, false, null)));
|
||||||
if (Objects.isNull(updateSet)) {
|
|
||||||
updateSet = new ArrayList<>();
|
|
||||||
}
|
|
||||||
updateSet.add(new UpdateSet(column, val, mapping, false, null));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <R> UpdateJoinWrapper<T> setIncrBy(boolean condition, SFunction<R, ?> column, Number val) {
|
public <R> UpdateJoinWrapper<T> setIncrBy(boolean condition, SFunction<R, ?> column, Number val) {
|
||||||
return maybeDo(condition, () -> {
|
return maybeDo(condition, () -> getUpdateSet().add(new UpdateSet(column, val, null, true, Constant.PLUS)));
|
||||||
if (Objects.isNull(updateSet)) {
|
|
||||||
updateSet = new ArrayList<>();
|
|
||||||
}
|
|
||||||
updateSet.add(new UpdateSet(column, val, null, true, Constant.PLUS));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <R> UpdateJoinWrapper<T> setDecrBy(boolean condition, SFunction<R, ?> column, Number val) {
|
public <R> UpdateJoinWrapper<T> setDecrBy(boolean condition, SFunction<R, ?> column, Number val) {
|
||||||
return maybeDo(condition, () -> {
|
return maybeDo(condition, () -> getUpdateSet().add(new UpdateSet(column, val, null, true, Constant.DASH)));
|
||||||
if (Objects.isNull(updateSet)) {
|
|
||||||
updateSet = new ArrayList<>();
|
|
||||||
}
|
}
|
||||||
updateSet.add(new UpdateSet(column, val, null, true, Constant.DASH));
|
|
||||||
});
|
@Override
|
||||||
|
public UpdateJoinWrapper<T> setApply(boolean condition, String applySql, SFunction<FuncConsumer, SFunction<?, ?>[]> consumerFunction, Object... values) {
|
||||||
|
if (condition && StringUtils.isNotBlank(applySql)) {
|
||||||
|
SFunction<?, ?>[] arg = consumerFunction.apply(FuncConsumer.func);
|
||||||
|
UpdateSet set = new UpdateSet();
|
||||||
|
set.setApply(true);
|
||||||
|
set.setFormat(applySql);
|
||||||
|
set.setColumns(arg);
|
||||||
|
set.setArgs(values);
|
||||||
|
getUpdateSet().add(set);
|
||||||
|
}
|
||||||
|
return typedThis;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -168,7 +159,13 @@ public class UpdateJoinWrapper<T> extends JoinAbstractLambdaWrapper<T, UpdateJoi
|
|||||||
}
|
}
|
||||||
StringBuilder set = new StringBuilder(StringPool.EMPTY);
|
StringBuilder set = new StringBuilder(StringPool.EMPTY);
|
||||||
if (CollectionUtils.isNotEmpty(updateSet)) {
|
if (CollectionUtils.isNotEmpty(updateSet)) {
|
||||||
set = new StringBuilder(updateSet.stream().map(i -> {
|
String setSql = updateSet.stream().map(i -> {
|
||||||
|
if (i.isApply) {
|
||||||
|
String col = String.format(i.format, Arrays.stream(i.columns).map(f ->
|
||||||
|
tableList.getPrefixByClass(LambdaUtils.getEntityClass(f)) +
|
||||||
|
Constants.DOT + getCache(f).getColumn()).toArray());
|
||||||
|
return formatSqlMaybeWithParam(col, null, i.args);
|
||||||
|
} else {
|
||||||
String col = tableList.getPrefixByClass(LambdaUtils.getEntityClass(i.getColumn())) +
|
String col = tableList.getPrefixByClass(LambdaUtils.getEntityClass(i.getColumn())) +
|
||||||
Constants.DOT + getCache(i.getColumn()).getColumn();
|
Constants.DOT + getCache(i.getColumn()).getColumn();
|
||||||
if (i.incOrDnc) {
|
if (i.incOrDnc) {
|
||||||
@ -182,8 +179,9 @@ public class UpdateJoinWrapper<T> extends JoinAbstractLambdaWrapper<T, UpdateJoi
|
|||||||
return col + Constants.EQUALS + formatParam(i.mapping, i.value);
|
return col + Constants.EQUALS + formatParam(i.mapping, i.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
.collect(Collectors.joining(StringPool.COMMA)) + StringPool.COMMA);
|
}).collect(Collectors.joining(StringPool.COMMA)) + StringPool.COMMA;
|
||||||
|
set.append(setSql);
|
||||||
}
|
}
|
||||||
if (CollectionUtils.isNotEmpty(sqlSet)) {
|
if (CollectionUtils.isNotEmpty(sqlSet)) {
|
||||||
set.append(String.join(StringPool.COMMA, sqlSet)).append(StringPool.COMMA);
|
set.append(String.join(StringPool.COMMA, sqlSet)).append(StringPool.COMMA);
|
||||||
@ -219,6 +217,13 @@ public class UpdateJoinWrapper<T> extends JoinAbstractLambdaWrapper<T, UpdateJoi
|
|||||||
this.tableList, index, keyWord, joinClass, tableName);
|
this.tableList, index, keyWord, joinClass, tableName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<UpdateSet> getUpdateSet() {
|
||||||
|
if (updateSet == null) {
|
||||||
|
updateSet = new ArrayList<>();
|
||||||
|
}
|
||||||
|
return updateSet;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 不建议直接 new 该实例,使用 JoinWrappers.update(User.class)
|
* 不建议直接 new 该实例,使用 JoinWrappers.update(User.class)
|
||||||
*/
|
*/
|
||||||
@ -293,7 +298,6 @@ public class UpdateJoinWrapper<T> extends JoinAbstractLambdaWrapper<T, UpdateJoi
|
|||||||
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
|
||||||
public static class UpdateSet {
|
public static class UpdateSet {
|
||||||
|
|
||||||
private SFunction<?, ?> column;
|
private SFunction<?, ?> column;
|
||||||
@ -305,5 +309,24 @@ public class UpdateJoinWrapper<T> extends JoinAbstractLambdaWrapper<T, UpdateJoi
|
|||||||
private boolean incOrDnc;
|
private boolean incOrDnc;
|
||||||
|
|
||||||
private String cal;
|
private String cal;
|
||||||
|
|
||||||
|
private boolean isApply = false;
|
||||||
|
|
||||||
|
private String format;
|
||||||
|
|
||||||
|
private SFunction<?, ?>[] columns;
|
||||||
|
|
||||||
|
private Object[] args;
|
||||||
|
|
||||||
|
public UpdateSet() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public UpdateSet(SFunction<?, ?> column, Object value, String mapping, boolean incOrDnc, String cal) {
|
||||||
|
this.column = column;
|
||||||
|
this.value = value;
|
||||||
|
this.mapping = mapping;
|
||||||
|
this.incOrDnc = incOrDnc;
|
||||||
|
this.cal = cal;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.github.yulichang.wrapper.interfaces;
|
package com.github.yulichang.wrapper.interfaces;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
|
import com.github.yulichang.wrapper.segments.FuncConsumer;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
@ -79,6 +80,12 @@ public interface Update<Children> extends Serializable {
|
|||||||
|
|
||||||
<R> Children setDecrBy(boolean condition, SFunction<R, ?> column, Number val);
|
<R> Children setDecrBy(boolean condition, SFunction<R, ?> column, Number val);
|
||||||
|
|
||||||
|
default Children setApply(String applySql, SFunction<FuncConsumer, SFunction<?, ?>[]> consumerFunction, Object... values) {
|
||||||
|
return setApply(true, applySql, consumerFunction, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
Children setApply(boolean condition, String applySql, SFunction<FuncConsumer, SFunction<?, ?>[]> consumerFunction, Object... values);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ignore
|
* ignore
|
||||||
*/
|
*/
|
||||||
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.github.yulichang.test.join.unit;
|
||||||
|
|
||||||
|
import com.github.yulichang.test.join.entity.UserDO;
|
||||||
|
import com.github.yulichang.test.util.Reset;
|
||||||
|
import com.github.yulichang.test.util.ThreadLocalUtils;
|
||||||
|
import com.github.yulichang.toolkit.JoinWrappers;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class SetApplyTest {
|
||||||
|
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
Reset.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void applyFunc() {
|
||||||
|
ThreadLocalUtils.set("UPDATE `user` t SET t.id = t.id + ? + ? WHERE t.del = false");
|
||||||
|
|
||||||
|
int update = JoinWrappers.update(UserDO.class)
|
||||||
|
.setApply("%s = %s + {0} + {1}", arg -> arg.accept(UserDO::getId, UserDO::getId), "100", "1000")
|
||||||
|
.update();
|
||||||
|
|
||||||
|
assert update > 0;
|
||||||
|
|
||||||
|
List<UserDO> list = JoinWrappers.lambda(UserDO.class).list();
|
||||||
|
System.out.println(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user