mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
feat: updateJoin set值支持其他字段 https://gitee.com/best_handsome/mybatis-plus-join/issues/I9G29I
This commit is contained in:
parent
321abffa01
commit
cace18d3bb
@ -22,6 +22,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@ -119,6 +120,16 @@ public class UpdateJoinWrapper<T> extends JoinAbstractLambdaWrapper<T, UpdateJoi
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R, V> UpdateJoinWrapper<T> set(boolean condition, SFunction<R, ?> column, SFunction<V, ?> val, String mapping) {
|
||||
return maybeDo(condition, () -> {
|
||||
if (Objects.isNull(updateSet)) {
|
||||
updateSet = new ArrayList<>();
|
||||
}
|
||||
updateSet.add(new UpdateSet(column, val, mapping, false, null));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> UpdateJoinWrapper<T> setIncrBy(boolean condition, SFunction<R, ?> column, Number val) {
|
||||
return maybeDo(condition, () -> {
|
||||
@ -164,7 +175,13 @@ public class UpdateJoinWrapper<T> extends JoinAbstractLambdaWrapper<T, UpdateJoi
|
||||
if (i.incOrDnc) {
|
||||
return col + Constants.EQUALS + col + i.cal + i.value;
|
||||
} else {
|
||||
return col + Constants.EQUALS + formatParam(i.mapping, i.value);
|
||||
if (i.value instanceof Function) {
|
||||
SFunction<?, ?> value = (SFunction<?, ?>) i.getValue();
|
||||
return col + Constants.EQUALS + tableList.getPrefixByClass(LambdaUtils.getEntityClass(value)) +
|
||||
Constants.DOT + getCache(value).getColumn();
|
||||
} else {
|
||||
return col + Constants.EQUALS + formatParam(i.mapping, i.value);
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect(Collectors.joining(StringPool.COMMA)) + StringPool.COMMA);
|
||||
@ -256,6 +273,7 @@ public class UpdateJoinWrapper<T> extends JoinAbstractLambdaWrapper<T, UpdateJoi
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("DuplicatedCode")
|
||||
public void clear() {
|
||||
super.clear();
|
||||
sqlSetStr.toNull();
|
||||
|
@ -20,6 +20,13 @@ public interface Update<Children> extends Serializable {
|
||||
return set(true, column, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R, V> Children set(SFunction<R, ?> column, SFunction<V, ?> val) {
|
||||
return set(true, column, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 更新 SQL 的 SET 片段
|
||||
*
|
||||
@ -32,6 +39,10 @@ public interface Update<Children> extends Serializable {
|
||||
return set(condition, column, val, null);
|
||||
}
|
||||
|
||||
default <R, V> Children set(boolean condition, SFunction<R, ?> column, SFunction<V, ?> val) {
|
||||
return set(condition, column, val, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
@ -39,6 +50,10 @@ public interface Update<Children> extends Serializable {
|
||||
return set(true, column, val, mapping);
|
||||
}
|
||||
|
||||
default <R, V> Children set(SFunction<R, ?> column, SFunction<V, ?> val, String mapping) {
|
||||
return set(true, column, val, mapping);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 更新 SQL 的 SET 片段
|
||||
*
|
||||
@ -50,6 +65,8 @@ public interface Update<Children> extends Serializable {
|
||||
*/
|
||||
<R> Children set(boolean condition, SFunction<R, ?> column, Object val, String mapping);
|
||||
|
||||
<R, V> Children set(boolean condition, SFunction<R, ?> column, SFunction<V, ?> val, String mapping);
|
||||
|
||||
default <R> Children setIncrBy(SFunction<R, ?> column, Number val) {
|
||||
return setIncrBy(true, column, val);
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.github.yulichang.test.join.m;
|
||||
|
||||
import com.github.yulichang.test.join.entity.AddressDO;
|
||||
import com.github.yulichang.test.join.entity.UserDO;
|
||||
import com.github.yulichang.test.util.Reset;
|
||||
import com.github.yulichang.test.util.ThreadLocalUtils;
|
||||
@ -7,6 +8,7 @@ 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 org.springframework.jdbc.BadSqlGrammarException;
|
||||
|
||||
@SpringBootTest
|
||||
public class UpdateIncTest {
|
||||
@ -18,16 +20,28 @@ public class UpdateIncTest {
|
||||
|
||||
|
||||
@Test
|
||||
void eqSql() {
|
||||
void updateInc() {
|
||||
ThreadLocalUtils.set("UPDATE `user` t SET t.id = t.id + 100 WHERE t.del = false");
|
||||
JoinWrappers.update(UserDO.class).setIncrBy(UserDO::getId,100).update();
|
||||
JoinWrappers.update(UserDO.class).setIncrBy(UserDO::getId, 100).update();
|
||||
|
||||
JoinWrappers.lambda(UserDO.class).list().forEach(System.out::println);
|
||||
|
||||
ThreadLocalUtils.set("UPDATE `user` t SET t.id = t.id - 100 WHERE t.del = false");
|
||||
JoinWrappers.update(UserDO.class).setDecrBy(UserDO::getId,100).update();
|
||||
JoinWrappers.update(UserDO.class).setDecrBy(UserDO::getId, 100).update();
|
||||
|
||||
JoinWrappers.lambda(UserDO.class).list().forEach(System.out::println);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void updateInc1() {
|
||||
ThreadLocalUtils.set("UPDATE `user` t LEFT JOIN address t1 ON (t1.user_id = t.id) SET t1.address = t.head_img WHERE t.del = false AND t1.del = false");
|
||||
try {
|
||||
JoinWrappers.update(UserDO.class).set(AddressDO::getAddress, UserDO::getImg)
|
||||
.leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId).update();
|
||||
} catch (BadSqlGrammarException ignore) {
|
||||
}
|
||||
JoinWrappers.lambda(UserDO.class).list().forEach(System.out::println);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user