【 jpa的模糊查询,条件查询,分页】

做项目需要进行该项操作 查询相关资料没有太过明白的讲解, 查询了接近的三份资料总结出较为完美的分页(具体的东西不懂只能实现功能)

先是实体类

@Data
@Entity
public class Goods implements Serializable {
          
   
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer gid;
    private String gname;
    private Integer price;
    private Integer status;
    private String des;
    private String photo;
    private Integer discount;
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="tid")
    private GoodsType goodsType;

}
------------------------------
@Data
@Entity
public class GoodsType implements Serializable {
          
   
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer tid;
    private String tname;

}

再是dao层

public interface GoodsDao extends JpaRepository<Goods,Integer> {
          
   

  
	//进行分页的方法
    Page<Goods> findAll(Specification<Goods> specification, Pageable pageable);
	//不进行分页的方法
    List<Goods> findAll(Specification<Goods> specification);

}

最后是service层

@Component
public class Xxxx {
          
   
    @Resource
    private GoodsDao goodsDao;


	//pageNo:页数 pageSize:条目数 gname:对名字进行模糊查询 goodsType:商品类型
    public List<Goods> queryFlows(int pageNo, int pageSize, String gname, GoodsType goodsType) {
          
   
        List<Goods> result = null;
        Specification<Goods> specification=new Specification<Goods>() {
          
   
            @Override
            public Predicate toPredicate(Root<Goods> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
          
   
                List<Predicate> predicates=new ArrayList<Predicate>(10);
                if(gname!=null){
          
   
                        predicates.add(criteriaBuilder.like(root.get("gname"),"%"+gname+"%"));
                }
                if(goodsType!=null){
          
   
                        predicates.add(criteriaBuilder.equal(root.get("goodsType").as(GoodsType.class),goodsType));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };

        try {
          
   
					//进行分页的判断
            if (pageNo == 0 && pageSize == 0) {
          
   
                result = goodsDao.findAll(specification);
            } else {
          
   
                result = goodsDao.findAll(specification, PageRequest.of(pageNo - 1, pageSize)).getContent();
            }
        } catch (Exception e) {
          
   
            e.printStackTrace();
        }

        return result;
    }

}

最后进行测试

@SpringBootTest
public class MyTest {
          
   
    @Resource
    private Xxxx xxxx;

    @Resource
    private GoodsTypeDao goodsTypeDao;
    @Resource
    private GoodsDao goodsDao;

    @Test
    public void xxxx(){
          
   
	
    GoodsType goodsType=goodsTypeDao.findById(1).get();
        System.out.println(this.xxxx.queryFlows(1,5,"米",goodsType));


    }
}
![测试结果](https://img-blog.img.cn/50060c45fe914207ada2b5d074a2c733.png#pic_center)
经验分享 程序员 微信小程序 职场和发展