All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.hutool.db.sql.ConditionBuilder Maven / Gradle / Ivy

Go to download

Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。

There is a newer version: 5.8.34
Show newest version
package cn.hutool.db.sql;

import cn.hutool.core.builder.Builder;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.StrUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 多条件构建封装
* 可以将多个条件构建为SQL语句的一部分,并将参数值转换为占位符,并提取对应位置的参数值。
* 例如:name = ? AND type IN (?, ?) AND other LIKE ? * * @author looly * @since 5.4.3 */ public class ConditionBuilder implements Builder { private static final long serialVersionUID = 1L; /** * 创建构建器 * * @param conditions 条件列表 * @return ConditionBuilder */ public static ConditionBuilder of(Condition... conditions) { return new ConditionBuilder(conditions); } /** * 条件数组 */ private final Condition[] conditions; /** * 占位符对应的值列表 */ private List paramValues; /** * 构造 * * @param conditions 条件列表 */ public ConditionBuilder(Condition... conditions) { this.conditions = conditions; } /** * 返回构建后的参数列表
* 此方法调用前必须调用{@link #build()} * * @return 参数列表 */ public List getParamValues() { return ListUtil.unmodifiable(this.paramValues); } /** * 构建组合条件
* 例如:name = ? AND type IN (?, ?) AND other LIKE ? * * @return 构建后的SQL语句条件部分 */ @Override public String build() { if(null == this.paramValues){ this.paramValues = new ArrayList<>(); } else { this.paramValues.clear(); } return build(this.paramValues); } /** * 构建组合条件
* 例如:name = ? AND type IN (?, ?) AND other LIKE ? * * @param paramValues 用于写出参数的List,构建时会将参数写入此List * @return 构建后的SQL语句条件部分 */ public String build(List paramValues) { if (ArrayUtil.isEmpty(conditions)) { return StrUtil.EMPTY; } final StringBuilder conditionStrBuilder = new StringBuilder(); boolean isFirst = true; for (Condition condition : conditions) { // 添加逻辑运算符 if (isFirst) { isFirst = false; } else { // " AND " 或者 " OR " conditionStrBuilder.append(CharUtil.SPACE).append(condition.getLinkOperator()).append(CharUtil.SPACE); } // 构建条件部分:"name = ?"、"name IN (?,?,?)"、"name BETWEEN ?AND ?"、"name LIKE ?" conditionStrBuilder.append(condition.toString(paramValues)); } return conditionStrBuilder.toString(); } /** * 构建组合条件
* 例如:name = ? AND type IN (?, ?) AND other LIKE ? * * @return 构建后的SQL语句条件部分 */ @Override public String toString() { return build(); } }