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 默认andif (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;}
}
使用
@Testpublic 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);}