mirror of
https://gitee.com/dromara/hutool.git
synced 2025-11-24 16:43:24 +08:00
Condition增加构造方法支持BETWEEN(issue#4041@Github)
This commit is contained in:
@@ -24,6 +24,7 @@ import cn.hutool.v7.core.text.CharUtil;
|
||||
import cn.hutool.v7.core.text.StrUtil;
|
||||
import cn.hutool.v7.core.text.split.SplitUtil;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
@@ -35,6 +36,7 @@ import java.util.List;
|
||||
* @author Looly
|
||||
*/
|
||||
public class Condition implements Cloneable, Serializable {
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
@@ -146,7 +148,12 @@ public class Condition implements Cloneable, Serializable {
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
* 构造LIKE value条件,支持:
|
||||
* <ul>
|
||||
* <li>{@link LikeType#StartWith}:LIKE value%</li>
|
||||
* <li>{@link LikeType#EndWith}:LIKE %value</li>
|
||||
* <li>{@link LikeType#Contains}:LIKE %value%</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param field 字段
|
||||
* @param value 值
|
||||
@@ -157,6 +164,21 @@ public class Condition implements Cloneable, Serializable {
|
||||
this.operator = OPERATOR_LIKE;
|
||||
this.value = SqlUtil.buildLikeValue(value, likeType, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造BETWEEN leftValue and rightValue条件
|
||||
*
|
||||
* @param field 字段
|
||||
* @param leftValue 左值
|
||||
* @param rightValue 右值
|
||||
* @since 5.8.41
|
||||
*/
|
||||
public Condition(final String field, final Object leftValue, final Object rightValue) {
|
||||
this.field = field;
|
||||
this.operator = OPERATOR_BETWEEN;
|
||||
this.value = leftValue;
|
||||
this.secondValue = rightValue;
|
||||
}
|
||||
// --------------------------------------------------------------- Constructor end
|
||||
|
||||
// --------------------------------------------------------------- Getters and Setters start
|
||||
|
||||
@@ -16,10 +16,14 @@
|
||||
|
||||
package cn.hutool.v7.db.sql;
|
||||
|
||||
import cn.hutool.v7.core.date.DateTime;
|
||||
import cn.hutool.v7.core.date.DateUtil;
|
||||
import cn.hutool.v7.core.lang.Console;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@@ -101,4 +105,11 @@ public class ConditionTest {
|
||||
final Condition age = Condition.parse("age", "not in 1,2,3");
|
||||
assertEquals("age NOT IN (?,?,?)", age.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void issue4041Test() {
|
||||
final DateTime date = DateUtil.parse("2025-08-29");
|
||||
final Condition createdDate = new Condition("createdDate", DateUtil.offsetDay(date, -3), DateUtil.offsetDay(date, -1));
|
||||
assertEquals("createdDate BETWEEN ? AND ?", createdDate.toString());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user