Specification封装一个类实现链式调用

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import java.util.Collection;


public class SpecBuilder<T> {
    /**
     * JPA 2 引入了一个条件 API,您可以使用该 API 以编程方式构建查询。通过编写 ,您可以为域类定义查询的 where 子句。
     * 再退一步,这些标准可以被视为 JPA 标准 API 约束所描述的实体的谓词。
     */
    private Specification<T> specification;
    /**
     * 逻辑与或 临时变量
     */
    private String logic;

    public SpecBuilder<T> not() {
        this.logic = "not";
        return this;
    }

    public SpecBuilder<T> where() {
        this.logic = "where";
        return this;
    }

    public SpecBuilder<T> and() {
        this.logic = "and";
        return this;
    }

    public SpecBuilder<T> or() {
        this.logic = "or";
        return this;
    }

    /**
     * 处理逻辑
     * 实现链式调用判断 and or调用
     */
    private void logic(Specification<T> specification) {
        // 直接调用 没有设置not where and or 默认是当前条件
        if (this.specification == null) {
            this.specification = specification;
        } else {
            // 不选 and or 默认and
            if (StringUtils.hasText(this.logic)) {
                switch (this.logic) {
                    case "not":
                        this.specification = Specification.not(specification);
                        break;
                    case "where":
                        this.specification = Specification.where(specification);
                        break;
                    case "and":
                        this.specification = this.specification.and(specification);
                        break;
                    case "or":
                        this.specification = this.specification.or(specification);
                        break;
                }
            } else {
                this.specification.and(specification);
            }
        }
    }

    /**
     * 模糊查询
     */
    public SpecBuilder<T> like(String name, String value) {
        // 处理模糊需要符号
        if (!value.startsWith("%")) {
            value = "%" + value;
        }
        if (!value.endsWith("%")) {
            value = value + "%";
        }
        String finalValue = value;
        Specification<T> specification = ((root, query, cb) -> cb.like(root.get(name), finalValue));
        this.logic(specification);
        return this;
    }

    public SpecBuilder<T> notLike(String name, String value) {
        // 处理模糊需要符号
        if (!value.startsWith("%")) {
            value = "%" + value;
        }
        if (!value.endsWith("%")) {
            value = value + "%";
        }
        String finalValue = value;
        Specification<T> specification = ((root, query, cb) -> cb.notLike(root.get(name), finalValue));
        this.logic(specification);
        return this;
    }

    /**
     * 判断是不是null
     */
    public SpecBuilder<T> isNull(String name) {
        Specification<T> specification = ((root, query, cb) -> cb.isNull(root.get(name)));
        this.logic(specification);
        return this;
    }

    /**
     * 判断是不是空的
     */
    public SpecBuilder<T> isNotNull(String name) {
        Specification<T> specification = ((root, query, cb) -> cb.isNotNull(root.get(name)));
        this.logic(specification);
        return this;
    }

    public SpecBuilder<T> isEmpty(String name) {
        Specification<T> specification = ((root, query, cb) -> cb.isEmpty(root.get(name)));
        this.logic(specification);
        return this;
    }

    public SpecBuilder<T> isNotEmpty(String name) {
        Specification<T> specification = ((root, query, cb) -> cb.isNotEmpty(root.get(name)));
        this.logic(specification);
        return this;
    }


    /**
     * 等于
     */
    public SpecBuilder<T> eq(String name, Object value) {
        Specification<T> specification = ((root, query, cb) -> cb.equal(root.get(name), value));
        this.logic(specification);
        return this;
    }

    public SpecBuilder<T> ne(String name, Object value) {
        Specification<T> specification = ((root, query, cb) -> cb.notEqual(root.get(name), value));
        this.logic(specification);
        return this;
    }

    /**
     * 大于
     */
    public SpecBuilder<T> gt(String name, Number value) {
        Specification<T> specification = ((root, query, cb) -> cb.gt(root.get(name), value));
        this.logic(specification);
        return this;
    }

    /**
     * 大于等于
     */
    public SpecBuilder<T> ge(String name, Number value) {
        Specification<T> specification = ((root, query, cb) -> cb.ge(root.get(name), value));
        this.logic(specification);
        return this;
    }

    /**
     * 小于
     */
    public SpecBuilder<T> lt(String name, Number value) {
        Specification<T> specification = ((root, query, cb) -> cb.lt(root.get(name), value));
        this.logic(specification);
        return this;
    }

    /**
     * 小于等于
     */
    public SpecBuilder<T> le(String name, Number value) {
        Specification<T> specification = ((root, query, cb) -> cb.le(root.get(name), value));
        this.logic(specification);
        return this;
    }

    /**
     * 两者之间
     */
    public <Y extends Comparable<? super Y>> SpecBuilder<T> between(String name, Y v1, Y v2) {
        Specification<T> specification = ((root, query, cb) -> cb.between(root.get(name), v1, v2));
        this.logic(specification);
        return this;
    }

    public SpecBuilder<T> in(String name, Collection<?> value) {
        Specification<T> specification = ((root, query, cb) -> root.get(name).in(value));
        this.logic(specification);
        return this;
    }

    public SpecBuilder<T> in(String name, Object... value) {
        Specification<T> specification = ((root, query, cb) -> root.get(name).in(value));
        this.logic(specification);
        return this;
    }

    public SpecBuilder<T> notIn(String name, Collection<?> value) {
        Specification<T> specification = ((root, query, cb) -> cb.not(root.get(name).in(value)));
        this.logic(specification);
        return this;
    }

    public SpecBuilder<T> notIn(String name, Object... value) {
        Specification<T> specification = ((root, query, cb) -> cb.not(root.get(name).in(value)));
        this.logic(specification);
        return this;
    }

    public Specification<T> build() {
        return this.specification;
    }
}

使用

 @Test
    public void testJpa() {
        Specification<Info> specification = new SpecBuilder<>()
                .where()
                .like("authMethod", "cookie")
                .and()
                .like("authMethod", "cookie")
                .notLike("authMethod", "apikey")
                .notIn("id", "1", "2", "3")
                .in("id", "12")
                .build();
        List<Info> list = apiAuthInfoRepository.findAll(specification);
        System.out.println(list);
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值