美文网首页
番外-Jpa多条件查询

番外-Jpa多条件查询

作者: 通灵路耳 | 来源:发表于2020-12-27 11:15 被阅读0次
多条件分页+排序
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<List<BasYeGroup>> YeAll(
            BasYeGroup basYeGroup,
            @RequestParam("page") Integer page,
            @RequestParam("rows") Integer rows
            ){
        //传参
        String yeGroupId=basYeGroup.getYeGroupId();
        Page<BasYeGroup>  basYeGroups=basYeService.getYeAll(page,rows,yeGroupId);
        //返回数据
        List<BasYeGroup> 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<BasYeGroup>  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<BasYeGroup> getYeAll(Integer page, Integer rows, String yeGroupId) {
        //1、多排序
        List<Order> 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<BasYeGroup> spec = (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<Predicate>();
                //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<BasYeGroup>() {
            @Override
            public Predicate toPredicate(Root<BasYeGroup> 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<BasYeGroup,String>, JpaSpecificationExecutor<BasYeGroup> {
}


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多条件查询

  • Spring Data JPA

    Spring Data JPA,一种动态条件查询的写法 我们在使用SpringData JPA框架时,进行条件查询...

  • JPA-复杂查询

    时间相关查询 Spring data jpa 条件查询-按时间段查询Jpa查询排序,时间范围查询,当天时间范围查询...

  • SpringBoot+Spring JPA基础使用

    Spring JPA集成基础查询实例复杂查询+分页一对多查询及多对多查询 1. Spring JPA集成 1.1 ...

  • Spring 随笔

    1.Spring JPA 条件查询 ``` @Override public ListgetJobsByAccou...

  • JPA多表分页动态多条件查询

    参考文章: 解决 JPA 多表动态查询 JPA EntityManager createNativeQuery 多...

  • JPA多条件查询

    (1)多条件查询实现 对And 和Or的同时处理办法 直接贴代码 使用上面代码块的方法,可以灵活实现复杂的条件查...

  • Spring Data JPA方法定义规范

    Spring Data Jpa方法定义的规则: (1)简单条件查询简单条件查询:查询某一个实体类或者集合。按照Sp...

  • Spring Data JPA方法名命名规则

    一、Spring Data Jpa方法定义的规则 简单条件查询 简单条件查询:查询某一个实体类或者集合。按照Spr...

  • Jpa的条件查询和分页

    简介 习惯了使用mybtais操作数据库。今天,尝试了下jpa的操作,但是遇到个问题,jpa的条件查询和分页查询该...

网友评论

      本文标题:番外-Jpa多条件查询

      本文链接:https://www.haomeiwen.com/subject/tphonktx.html