废话
1、首先我们不得不说JPA的强大。他帮我们实现了很多重复的工作。而且他可以自己写HQL。或者写一些方法名,就能根据方法名转换成查询语句。比如
2、但是这些固定的字段查询不能满足我们的需求。因为我们的查询字段,常常是前端给我们几个参数就用几个参数去查,为NULL的参数肯定不参与SQL查询的。JPA是这么做的
他将对象用Example.of方法,根据参数是否有值生成相应的SQL查询。可他不支持模糊查询啊。气人不?!
大家都知道。我们做后台管理系统的时候,怎么可能没有模糊。
所以总结下上面两种方式:第一种方式,查询字段太固定 第二种方式没有模糊查询,美中不足
解决
于是我自己写了一段代码,支持字段动态查询下,还支持模糊查询。
(这里是Springboot + SpringBoot JPA 整合自行完成!)
ProductBrandInfoRepository.java
@Repository
public interface ProductBrandInfoRepository
extends CommonRepository<ProductBrandInfo> {
}
CommonRepository.java
package com.shby.product.common;
import com.shby.core.Pager;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
im