番外-Jpa多条件查询

多条件分页+排序
1、Controller

package com.mf.controller.bas;

import com.mf.entity.bas.BasYeGroup;
import com.mf.service.bas_service.BasYeService;
import com.mf.util.ResponseResult;
import com.mf.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/bas/ye")
public class BasYeController {

    @Autowired
    private BasYeService basYeService;

    /**
     * 查询叶片
     * @param basYeGroup
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/all")
    public ResponseResult> YeAll(
            BasYeGroup basYeGroup,
            @RequestParam("page") Integer page,
            @RequestParam("rows") Integer rows
            ){
        //传参
        String yeGroupId=basYeGroup.getYeGroupId();
        Page  basYeGroups=basYeService.getYeAll(page,rows,yeGroupId);
        //返回数据
        List list =basYeGroups.getContent();
        //总条数
        Long count=basYeService.getCount(basYeGroup);
        return new ResponseResult<>(1,count,list);
    }
}


2、Service
package com.mf.service.bas_service;

import com.mf.entity.bas.BasProduct;
import com.mf.entity.bas.BasYeGroup;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

public interface BasYeService {

    /**
     * 叶片查询
     */
    Page  getYeAll(Integer page,Integer rows,String yeGroupId);

    /**
     * 叶片分页总数
     * @param basProduct
     * @return
     */
    Long getCount(BasYeGroup basProduct);

}


3、实现类

package com.mf.service.bas_impl;



import com.mf.entity.bas.BasYeGroup;
import com.mf.repository.bas.BasYeRepository;
import com.mf.service.bas_service.BasYeService;
import com.mf.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

@Service
public class BasYeServiceImpl implements BasYeService {

    @Autowired
    BasYeRepository basYeRepository;

    /**
     * 叶片分页
     * @param
     * @param
     * @param
     * @return
     */
    @Override
    public Page getYeAll(Integer page, Integer rows, String yeGroupId) {
        //1、多排序
        List list=new ArrayList<>();
        list.add( new Order(Direction.ASC, "yeGroupId"));
        list.add( new Order(Direction.ASC, "theOrder"));
        Sort sort = new Sort(list);
        //2、传参
        //Pageable 封装了分页的参数,当前页,每页显示的条数,注意:它的当前页是从0开始的
        Pageable pageable = new PageRequest(page-1, rows,sort);
        Specification spec = (root, criteriaQuery, criteriaBuilder) -> {
            List predicates = new ArrayList();
                //3、模糊查询
                if (StringUtil.isNotEmpty(yeGroupId)) {
                    predicates.add(criteriaBuilder.like(root.get("yeGroupId"), "%" + yeGroupId + "%"));
                }

            //4、将多条件连接在一起
            return criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
        };
        //5、返回数据
        return basYeRepository.findAll(spec,pageable);
    }

    /**
     * 叶片分页总数
     * @param basYeGroup
     * @return
     */
    @Override
    public Long getCount(BasYeGroup basYeGroup) {
        Long count = basYeRepository.count(new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                Predicate predicate = cb.conjunction();
                if(basYeGroup != null) {
                    if (StringUtil.isNotEmpty(basYeGroup.getYeGroupId())) {
                        predicate.getExpressions().add(cb.like(root.get("yeGroupId"), "%" + basYeGroup.getYeGroupId()+ "%"));

                    }
                }
                return predicate;
            }
        });
        return count;
    }
}

4、dao层

package com.mf.repository.bas;

import com.mf.entity.bas.BasYeGroup;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface BasYeRepository extends JpaRepository, JpaSpecificationExecutor {
}


5、实体类

package com.mf.entity.bas;

import lombok.Data;
import org.springframework.data.domain.Sort;

import javax.persistence.*;
import java.io.Serializable;

/**
 * 叶片
 */
@Data
@Entity//实体
@Table(name = "bas_ye_group")
@NamedQuery(name = "BasYeGroup.findAll",query = "select b from BasYeGroup b")
public class BasYeGroup implements Serializable {

    @Id
    @Column(name = "ye_group_guid")
    public String  yeGroupGuid;

    @Column(name = "ye_group_id")
    public String  yeGroupId;

    @Column(name = "product_id")
    public String  productId;

    @Column(name = "the_order")
    public Integer  theOrder;

    @Column(name = "quantity")
    public Integer  quantity;

    public BasYeGroup(){}

    @Override
    public String toString() {
        return "BasYeGroup{" +
                "yeGroupGuid='" + yeGroupGuid + '\'' +
                ", yeGroupId='" + yeGroupId + '\'' +
                ", productId='" + productId + '\'' +
                ", theOrder=" + theOrder +
                ", quantity=" + quantity +
                '}';
    }
}

你可能感兴趣的:(番外-Jpa多条件查询)