criterion java_Criterion.java

package com.conedot.aland.training.dao.v2;

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;

public class Criterion {

public interface Column {

default String getColumn() {

return toString();

}

}

protected static final String EQUAL = " = ";

protected static final String NOT_EQUAL = " != ";

protected static final String LIKE = " like ";

protected static final String IN = " in ";

protected static final String NOT_NULL = " is not null ";

protected static final String NULL = " is null ";

protected static final String GREATER_THAN = " > ";

protected static final String GREATER_EQUAL = " >= ";

protected static final String LESS_THAN = " < ";

protected static final String LESS_EQUAL = " <= ";

protected static final String BETWEEN = " between ";

protected static final String AND = " and ";

protected static final String OR = " or ";

private List criterion;

private boolean isFirstCriteria = true;

public Criterion() {

criterion = new ArrayList<>();

}

public static Criterion create() {

Criterion criterion = new Criterion<>();

criterion.criterion = new ArrayList();

return criterion;

}

public List getCriterion() {

return criterion;

}

public Criterion andEqual(E e, Object value) {

addCriteria(CriteriaBindType.and, bindCondition(e , EQUAL), value);

return this;

}

public Criterion andNotEqual(E e, Object value) {

addCriteria(CriteriaBindType.and, bindCondition(e, NOT_EQUAL), value);

return this;

}

public Criterion andLike(E e, Object value) {

addCriteria(CriteriaBindType.and, bindCondition(e, LIKE), value);

return this;

}

public Criterion andIn(E e, Collection> value) {

addCriteria(CriteriaBindType.and, bindCondition(e, IN), value);

return this;

}

public Criterion andBetween(E e, Object value, Object secondValue) {

addCriteria(CriteriaBindType.and, bindCondition(e, BETWEEN), value, secondValue);

return this;

}

public Criterion andNotNull(E e) {

addCriteria(CriteriaBindType.and, bindCondition(e, NOT_NULL));

return this;

}

public Criterion andIsNull(E e) {

addCriteria(CriteriaBindType.and, bindCondition(e, NULL));

return this;

}

public Criterion andGreaterThan(E e, Object value) {

addCriteria(CriteriaBindType.and, bindCondition(e, GREATER_THAN), value);

return this;

}

public Criterion andGreaterEqual(E e, Object value) {

addCriteria(CriteriaBindType.and, bindCondition(e, GREATER_EQUAL), value);

return this;

}

public Criterion andLessThan(E e, Object value) {

addCriteria(CriteriaBindType.and, bindCondition(e, LESS_THAN), value);

return this;

}

public Criterion andLessEqual(E e, Object value) {

addCriteria(CriteriaBindType.and, bindCondition(e, LESS_EQUAL), value);

return this;

}

public Criterion andSubCriterion(Criterion subCriterion) {

addSubCriteria(CriteriaBindType.and, subCriterion);

return this;

}

public Criterion orEqual(E e, Object value) {

addCriteria(CriteriaBindType.or, bindCondition(e, EQUAL), value);

return this;

}

public Criterion orNotEqual(E e, Object value) {

addCriteria(CriteriaBindType.or, bindCondition(e, NOT_EQUAL), value);

return this;

}

public Criterion orLike(E e, Object value) {

addCriteria(CriteriaBindType.or, bindCondition(e, LIKE), value);

return this;

}

public Criterion orIn(E e, Collection> value) {

addCriteria(CriteriaBindType.or, bindCondition(e, IN), value);

return this;

}

public Criterion orBetween(E e, Object value, Object secondValue) {

addCriteria(CriteriaBindType.or, bindCondition(e, BETWEEN), value, secondValue);

return this;

}

public Criterion orNotNull(E e) {

addCriteria(CriteriaBindType.or, bindCondition(e, NOT_NULL));

return this;

}

public Criterion orIsNull(E e) {

addCriteria(CriteriaBindType.or, bindCondition(e, NULL));

return this;

}

public Criterion orGreaterThan(E e, Object value) {

addCriteria(CriteriaBindType.or, bindCondition(e, GREATER_THAN), value);

return this;

}

public Criterion orGreaterEqual(E e, Object value) {

addCriteria(CriteriaBindType.or, bindCondition(e, GREATER_EQUAL), value);

return this;

}

public Criterion orLessThan(E e, Object value) {

addCriteria(CriteriaBindType.or, bindCondition(e, LESS_THAN), value);

return this;

}

public Criterion orLessEqual(E e, Object value) {

addCriteria(CriteriaBindType.or, bindCondition(e, LESS_EQUAL), value);

return this;

}

public Criterion orSubCriterion(Criterion subCriterion) {

addSubCriteria(CriteriaBindType.or, subCriterion);

return this;

}

protected void addCriterion(Criteria criteria) {

criterion.add(criteria);

isFirstCriteria = false;

}

protected void addCriteria(CriteriaBindType bindType, String condition) {

addCriterion(new Criteria(isFirstCriteria ? CriteriaBindType.first : bindType, condition));

}

protected void addCriteria(CriteriaBindType bindType, String condition, Object value) {

if (value == null || value.toString().isEmpty()) {

return;

}

addCriterion(new Criteria(isFirstCriteria ? CriteriaBindType.first : bindType, condition, value));

}

protected void addCriteria(CriteriaBindType bindType, String condition, Object value, Object secondValue) {

if (value == null || value.toString().isEmpty()) {

return;

}

addCriterion(new Criteria(isFirstCriteria ? CriteriaBindType.first : bindType, condition, value, secondValue));

}

protected void addCriteria(CriteriaBindType bindType, String condition, Collection> value) {

if (value == null || value.isEmpty()) {

return;

}

addCriterion(new Criteria(isFirstCriteria ? CriteriaBindType.first : bindType, condition, value));

}

protected void addSubCriteria(CriteriaBindType bindType, Criterion subCriterion) {

if (subCriterion == null || subCriterion.getCriterion() == null || subCriterion.getCriterion().isEmpty()) {

return;

}

addCriterion(new Criteria(isFirstCriteria ? CriteriaBindType.first : bindType, subCriterion.getCriterion()));

}

private String bindCondition(E e, String operator) {

return new StringBuilder(e.getColumn()).append(operator).toString();

}

protected String getBaseWhereClause(List criterion) {

StringBuilder sb = new StringBuilder();

for (Criteria criteria : criterion) {

sb.append(criteria.criteriaBindType.getValue());

sb.append(criteria.condition);

switch (criteria.criteriaType) {

case noValue:

break;

case singleValue:

sb.append(criteria.value);

break;

case betweenValue:

sb.append(criteria.value);

sb.append(AND);

sb.append(criteria.secondValue);

break;

case setValue:

sb.append("(");

boolean isFirst = true;

for (Object value : ((Collection>) criteria.value)) {

if (!isFirst) {

sb.append(",");

} else {

isFirst = false;

}

sb.append("'");

sb.append(value);

sb.append("'");

}

sb.append(")");

break;

case subCriterion:

sb.append("(");

sb.append(getBaseWhereClause(criteria.subCriterion));

sb.append(")");

default:

break;

}

}

return sb.toString();

}

public String getBaseWhereClause() {

return getBaseWhereClause(criterion);

}

public enum CriteriaBindType {

and(AND), or(OR), first("");

private String value;

CriteriaBindType(String value) {

this.value = value;

}

public String getValue() {

return value;

}

}

public enum CriteriaType {

noValue, singleValue, betweenValue, setValue, subCriterion

}

public class Criteria {

private CriteriaBindType criteriaBindType;

private String condition;

private Object value;

private Object secondValue;

private CriteriaType criteriaType;

private List subCriterion;

protected Criteria(CriteriaBindType criteriaBindType, String conditon) {

this.criteriaBindType = criteriaBindType;

this.condition = conditon;

this.criteriaType = CriteriaType.noValue;

}

protected Criteria(CriteriaBindType criteriaBindType, String conditon, Object value) {

this.criteriaBindType = criteriaBindType;

this.condition = conditon;

this.value = value;

this.criteriaType = CriteriaType.singleValue;

}

protected Criteria(CriteriaBindType criteriaBindType, String conditon, Object value, Object secondValue) {

this.criteriaBindType = criteriaBindType;

this.condition = conditon;

this.value = value;

this.secondValue = secondValue;

this.criteriaType = CriteriaType.betweenValue;

}

protected Criteria(CriteriaBindType criteriaBindType, String conditon, Collection> value) {

this.criteriaBindType = criteriaBindType;

this.condition = conditon;

this.value = value;

this.criteriaType = CriteriaType.setValue;

}

protected Criteria(CriteriaBindType criteriaBindType, List subCriterion) {

this.condition = "";

this.criteriaBindType = criteriaBindType;

this.subCriterion = subCriterion;

this.criteriaType = CriteriaType.subCriterion;

}

public CriteriaBindType getCriteriaBindType() {

return criteriaBindType;

}

public void setCriteriaBindType(CriteriaBindType criteriaBindType) {

this.criteriaBindType = criteriaBindType;

}

public String getCondition() {

return condition;

}

public void setCondition(String condition) {

this.condition = condition;

}

public Object getValue() {

return value;

}

public void setValue(Object value) {

this.value = value;

}

public Object getSecondValue() {

return secondValue;

}

public void setSecondValue(Object secondValue) {

this.secondValue = secondValue;

}

public CriteriaType getCriteriaType() {

return criteriaType;

}

public void setCriteriaType(CriteriaType criteriaType) {

this.criteriaType = criteriaType;

}

public List getSubCriterion() {

return subCriterion;

}

public void setSubCriterion(List subCriterion) {

this.subCriterion = subCriterion;

}

}

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值