mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
添加 字段自增 setIncrBy 自减 setDecrBy 方法
This commit is contained in:
parent
67aa1b106a
commit
cbc3709156
@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
|||||||
import com.baomidou.mybatisplus.core.toolkit.*;
|
import com.baomidou.mybatisplus.core.toolkit.*;
|
||||||
import com.github.yulichang.adapter.AdapterHelper;
|
import com.github.yulichang.adapter.AdapterHelper;
|
||||||
import com.github.yulichang.kt.interfaces.Update;
|
import com.github.yulichang.kt.interfaces.Update;
|
||||||
|
import com.github.yulichang.toolkit.Constant;
|
||||||
import com.github.yulichang.toolkit.KtUtils;
|
import com.github.yulichang.toolkit.KtUtils;
|
||||||
import com.github.yulichang.toolkit.TableHelper;
|
import com.github.yulichang.toolkit.TableHelper;
|
||||||
import com.github.yulichang.toolkit.TableList;
|
import com.github.yulichang.toolkit.TableList;
|
||||||
@ -114,7 +115,27 @@ public class KtUpdateJoinWrapper<T> extends KtAbstractLambdaWrapper<T, KtUpdateJ
|
|||||||
if (Objects.isNull(updateSet)) {
|
if (Objects.isNull(updateSet)) {
|
||||||
updateSet = new ArrayList<>();
|
updateSet = new ArrayList<>();
|
||||||
}
|
}
|
||||||
updateSet.add(new UpdateSet(column, val, mapping));
|
updateSet.add(new UpdateSet(column, val, mapping, false, null));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public KtUpdateJoinWrapper<T> setIncrBy(boolean condition, KProperty<?> column, Number val) {
|
||||||
|
return maybeDo(condition, () -> {
|
||||||
|
if (Objects.isNull(updateSet)) {
|
||||||
|
updateSet = new ArrayList<>();
|
||||||
|
}
|
||||||
|
updateSet.add(new UpdateSet(column, val, null, true, Constant.PLUS));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public KtUpdateJoinWrapper<T> setDecrBy(boolean condition, KProperty<?> column, Number val) {
|
||||||
|
return maybeDo(condition, () -> {
|
||||||
|
if (Objects.isNull(updateSet)) {
|
||||||
|
updateSet = new ArrayList<>();
|
||||||
|
}
|
||||||
|
updateSet.add(new UpdateSet(column, val, null, true, Constant.DASH));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,8 +157,15 @@ public class KtUpdateJoinWrapper<T> extends KtAbstractLambdaWrapper<T, KtUpdateJ
|
|||||||
}
|
}
|
||||||
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 -> tableList.getPrefixByClass(KtUtils.ref(i.getColumn())) +
|
set = new StringBuilder(updateSet.stream().map(i -> {
|
||||||
Constants.DOT + getCache(i.getColumn()).getColumn() + Constants.EQUALS + formatParam(i.mapping, i.value))
|
String col = tableList.getPrefixByClass(KtUtils.ref(i.getColumn())) +
|
||||||
|
Constants.DOT + getCache(i.getColumn()).getColumn();
|
||||||
|
if (i.incOrDnc) {
|
||||||
|
return col + Constants.EQUALS + col + i.cal + i.value;
|
||||||
|
} else {
|
||||||
|
return col + Constants.EQUALS + formatParam(i.mapping, i.value);
|
||||||
|
}
|
||||||
|
})
|
||||||
.collect(Collectors.joining(StringPool.COMMA)) + StringPool.COMMA);
|
.collect(Collectors.joining(StringPool.COMMA)) + StringPool.COMMA);
|
||||||
}
|
}
|
||||||
if (CollectionUtils.isNotEmpty(sqlSet)) {
|
if (CollectionUtils.isNotEmpty(sqlSet)) {
|
||||||
@ -253,5 +281,9 @@ public class KtUpdateJoinWrapper<T> extends KtAbstractLambdaWrapper<T, KtUpdateJ
|
|||||||
private Object value;
|
private Object value;
|
||||||
|
|
||||||
private String mapping;
|
private String mapping;
|
||||||
|
|
||||||
|
private boolean incOrDnc;
|
||||||
|
|
||||||
|
private String cal;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,6 +50,18 @@ public interface Update<Children> extends Serializable {
|
|||||||
*/
|
*/
|
||||||
Children set(boolean condition, KProperty<?> column, Object val, String mapping);
|
Children set(boolean condition, KProperty<?> column, Object val, String mapping);
|
||||||
|
|
||||||
|
default Children setIncrBy(KProperty<?> column, Number val) {
|
||||||
|
return setIncrBy(true, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
Children setIncrBy(boolean condition, KProperty<?> column, Number val);
|
||||||
|
|
||||||
|
default Children setDecrBy(KProperty<?> column, Number val) {
|
||||||
|
return setDecrBy(true, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
Children setDecrBy(boolean condition, KProperty<?> column, Number val);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ignore
|
* ignore
|
||||||
*/
|
*/
|
||||||
|
@ -7,6 +7,10 @@ import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
|||||||
*/
|
*/
|
||||||
public interface Constant {
|
public interface Constant {
|
||||||
|
|
||||||
|
String PLUS = " + ";
|
||||||
|
|
||||||
|
String DASH = " - ";
|
||||||
|
|
||||||
String AS = " AS ";
|
String AS = " AS ";
|
||||||
|
|
||||||
String ON = " ON ";
|
String ON = " ON ";
|
||||||
|
@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
|||||||
import com.baomidou.mybatisplus.core.toolkit.*;
|
import com.baomidou.mybatisplus.core.toolkit.*;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
import com.github.yulichang.adapter.AdapterHelper;
|
import com.github.yulichang.adapter.AdapterHelper;
|
||||||
|
import com.github.yulichang.toolkit.Constant;
|
||||||
import com.github.yulichang.toolkit.LambdaUtils;
|
import com.github.yulichang.toolkit.LambdaUtils;
|
||||||
import com.github.yulichang.toolkit.TableHelper;
|
import com.github.yulichang.toolkit.TableHelper;
|
||||||
import com.github.yulichang.toolkit.TableList;
|
import com.github.yulichang.toolkit.TableList;
|
||||||
@ -114,7 +115,27 @@ public class UpdateJoinWrapper<T> extends JoinAbstractLambdaWrapper<T, UpdateJoi
|
|||||||
if (Objects.isNull(updateSet)) {
|
if (Objects.isNull(updateSet)) {
|
||||||
updateSet = new ArrayList<>();
|
updateSet = new ArrayList<>();
|
||||||
}
|
}
|
||||||
updateSet.add(new UpdateSet(column, val, mapping));
|
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, () -> {
|
||||||
|
if (Objects.isNull(updateSet)) {
|
||||||
|
updateSet = new ArrayList<>();
|
||||||
|
}
|
||||||
|
updateSet.add(new UpdateSet(column, val, null, true, Constant.PLUS));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <R> UpdateJoinWrapper<T> setDecrBy(boolean condition, SFunction<R, ?> column, Number val) {
|
||||||
|
return maybeDo(condition, () -> {
|
||||||
|
if (Objects.isNull(updateSet)) {
|
||||||
|
updateSet = new ArrayList<>();
|
||||||
|
}
|
||||||
|
updateSet.add(new UpdateSet(column, val, null, true, Constant.DASH));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,8 +158,15 @@ 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 -> tableList.getPrefixByClass(LambdaUtils.getEntityClass(i.getColumn())) +
|
set = new StringBuilder(updateSet.stream().map(i -> {
|
||||||
Constants.DOT + getCache(i.getColumn()).getColumn() + Constants.EQUALS + formatParam(i.mapping, i.value))
|
String col = tableList.getPrefixByClass(LambdaUtils.getEntityClass(i.getColumn())) +
|
||||||
|
Constants.DOT + getCache(i.getColumn()).getColumn();
|
||||||
|
if (i.incOrDnc) {
|
||||||
|
return col + Constants.EQUALS + col + i.cal + i.value;
|
||||||
|
} else {
|
||||||
|
return col + Constants.EQUALS + formatParam(i.mapping, i.value);
|
||||||
|
}
|
||||||
|
})
|
||||||
.collect(Collectors.joining(StringPool.COMMA)) + StringPool.COMMA);
|
.collect(Collectors.joining(StringPool.COMMA)) + StringPool.COMMA);
|
||||||
}
|
}
|
||||||
if (CollectionUtils.isNotEmpty(sqlSet)) {
|
if (CollectionUtils.isNotEmpty(sqlSet)) {
|
||||||
@ -255,5 +283,9 @@ public class UpdateJoinWrapper<T> extends JoinAbstractLambdaWrapper<T, UpdateJoi
|
|||||||
private Object value;
|
private Object value;
|
||||||
|
|
||||||
private String mapping;
|
private String mapping;
|
||||||
|
|
||||||
|
private boolean incOrDnc;
|
||||||
|
|
||||||
|
private String cal;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,6 +50,18 @@ public interface Update<Children> extends Serializable {
|
|||||||
*/
|
*/
|
||||||
<R> Children set(boolean condition, SFunction<R, ?> column, Object val, String mapping);
|
<R> Children set(boolean condition, SFunction<R, ?> column, Object val, String mapping);
|
||||||
|
|
||||||
|
default <R> Children setIncrBy(SFunction<R, ?> column, Number val) {
|
||||||
|
return setIncrBy(true, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
<R> Children setIncrBy(boolean condition, SFunction<R, ?> column, Number val);
|
||||||
|
|
||||||
|
default <R> Children setDecrBy(SFunction<R, ?> column, Number val) {
|
||||||
|
return setDecrBy(true, column, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
<R> Children setDecrBy(boolean condition, SFunction<R, ?> column, Number val);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ignore
|
* ignore
|
||||||
*/
|
*/
|
||||||
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.github.yulichang.test.join.m;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class UpdateIncTest {
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
Reset.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void eqSql() {
|
||||||
|
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.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.lambda(UserDO.class).list().forEach(System.out::println);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user