Skip to content

Commit

Permalink
支持一下自增自减使用BigDecimal的少数情况.
Browse files Browse the repository at this point in the history
  • Loading branch information
nieqiurong committed Apr 15, 2024
1 parent 40aac96 commit a8c4e91
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
2 changes: 2 additions & 0 deletions mybatis-plus-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ dependencies {
implementation "${lib.'imadcn'}"
implementation "${lib.'mybatis-ehcache'}"
implementation "${lib.'mybatis-redis'}"

testImplementation "${lib.'logback-classic'}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -110,7 +111,7 @@ public LambdaUpdateWrapper<T> setIncrBy(SFunction<T, ?> column, Number val) {
public LambdaUpdateWrapper<T> setIncrBy(boolean condition, SFunction<T, ?> column, Number val) {
return maybeDo(condition, () -> {
String realColumn = columnToString(column);
sqlSet.add(realColumn + Constants.EQUALS + realColumn + Constants.PLUS + val);
sqlSet.add(realColumn + Constants.EQUALS + realColumn + Constants.PLUS + (val instanceof BigDecimal ? ((BigDecimal) val).toPlainString() : val));
});
}

Expand All @@ -134,7 +135,7 @@ public LambdaUpdateWrapper<T> setDecrBy(SFunction<T, ?> column, Number val) {
public LambdaUpdateWrapper<T> setDecrBy(boolean condition, SFunction<T, ?> column, Number val) {
return maybeDo(condition, () -> {
String realColumn = columnToString(column);
sqlSet.add(realColumn + Constants.EQUALS + realColumn + Constants.DASH + val);
sqlSet.add(realColumn + Constants.EQUALS + realColumn + Constants.DASH + (val instanceof BigDecimal ? ((BigDecimal) val).toPlainString() : val));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;

/**
* @author miemie
* @since 2021-01-27
Expand Down Expand Up @@ -51,6 +53,68 @@ void testIncrByAndDecrBy() {
wrapper = new LambdaUpdateWrapper<>();
wrapper.setDecrBy(User::getRoleId, 1).setDecrBy(false, User::getName, 1).eq(User::getId, 1);
Assertions.assertEquals("role_id=role_id-1", wrapper.getSqlSet());


}

@Test
void testIncrByAndDecrByBigDecimal() {
var wrapper = new LambdaUpdateWrapper<User>();
wrapper.setDecrBy(User::getRoleId, new BigDecimal("1")).eq(User::getId, 1);
Assertions.assertEquals("role_id=role_id-1", wrapper.getSqlSet());

wrapper = new LambdaUpdateWrapper<>();
wrapper.setDecrBy(User::getRoleId, new BigDecimal(1)).eq(User::getId, 1);
Assertions.assertEquals("role_id=role_id-1", wrapper.getSqlSet());

wrapper = new LambdaUpdateWrapper<>();
wrapper.setDecrBy(User::getRoleId, new BigDecimal(1.0000)).eq(User::getId, 1);
Assertions.assertEquals("role_id=role_id-1", wrapper.getSqlSet());

wrapper = new LambdaUpdateWrapper<>();
wrapper.setDecrBy(User::getRoleId, new BigDecimal("1.0000")).eq(User::getId, 1);
Assertions.assertEquals("role_id=role_id-1.0000", wrapper.getSqlSet());

wrapper = new LambdaUpdateWrapper<>();
wrapper.setDecrBy(User::getRoleId, new BigDecimal(0.01)).eq(User::getId, 1);
Assertions.assertEquals("role_id=role_id-" + new BigDecimal(0.01), wrapper.getSqlSet());

wrapper = new LambdaUpdateWrapper<>();
wrapper.setDecrBy(User::getRoleId, new BigDecimal("0.01")).eq(User::getId, 1);
Assertions.assertEquals("role_id=role_id-0.01", wrapper.getSqlSet());

wrapper = new LambdaUpdateWrapper<>();
wrapper.setDecrBy(User::getRoleId, new BigDecimal("2340").setScale(-1)).eq(User::getId, 1);
Assertions.assertEquals("role_id=role_id-2340", wrapper.getSqlSet());


wrapper = new LambdaUpdateWrapper<>();
wrapper.setIncrBy(User::getRoleId, new BigDecimal("1")).eq(User::getId, 1);
Assertions.assertEquals("role_id=role_id+1", wrapper.getSqlSet());

wrapper = new LambdaUpdateWrapper<>();
wrapper.setIncrBy(User::getRoleId, new BigDecimal(1)).eq(User::getId, 1);
Assertions.assertEquals("role_id=role_id+1", wrapper.getSqlSet());

wrapper = new LambdaUpdateWrapper<>();
wrapper.setIncrBy(User::getRoleId, new BigDecimal(1.0000)).eq(User::getId, 1);
Assertions.assertEquals("role_id=role_id+1", wrapper.getSqlSet());

wrapper = new LambdaUpdateWrapper<>();
wrapper.setIncrBy(User::getRoleId, new BigDecimal("1.0000")).eq(User::getId, 1);
Assertions.assertEquals("role_id=role_id+1.0000", wrapper.getSqlSet());

wrapper = new LambdaUpdateWrapper<>();
wrapper.setIncrBy(User::getRoleId, new BigDecimal(0.01)).eq(User::getId, 1);
Assertions.assertEquals("role_id=role_id+" + new BigDecimal(0.01), wrapper.getSqlSet());

wrapper = new LambdaUpdateWrapper<>();
wrapper.setIncrBy(User::getRoleId, new BigDecimal("0.01")).eq(User::getId, 1);
Assertions.assertEquals("role_id=role_id+0.01", wrapper.getSqlSet());

wrapper = new LambdaUpdateWrapper<>();
wrapper.setIncrBy(User::getRoleId, new BigDecimal("2340").setScale(-1)).eq(User::getId, 1);
Assertions.assertEquals("role_id=role_id+2340", wrapper.getSqlSet());
}

}

0 comments on commit a8c4e91

Please sign in to comment.