spring data jpa定义接口,继承JpaSpecificationExecutor<T>实现动态查询

该接口可以实现jpa根据参数,实现动态查询,类似与mybatis中的根据参数是否传递进行动态sql查询

dao层

package com.choorn.serverboot.dao;

import com.choorn.serverboot.entity.Income;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;

public interface IncomeDao extends PagingAndSortingRepository<Income,Long>, JpaSpecificationExecutor<Income> {
}

service层实现

package com.choorn.serverboot.service.impl;

import com.choorn.serverboot.dao.IncomeDao;
import com.choorn.serverboot.dto.IncomeSearchDTO;
import com.choorn.serverboot.entity.Income;
import com.choorn.serverboot.service.IncomeService;
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.jpa.domain.Specification;
import org.springframework.stereotype.Service;

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

@Service
public class IncomeServiceImpl implements IncomeService {
    private final IncomeDao incomeDao;
    @Autowired
    public IncomeServiceImpl(IncomeDao incomeDao) {
        this.incomeDao = incomeDao;
    }

    @Override
    public Page<Income> findAll(Pageable pageable) {
        return incomeDao.findAll(pageable);
    }
//————————————————  这个方法是重点  ————————————————————————
    @Override
    public Page<Income> findAll(IncomeSearchDTO incomeSearchDTO) {
//        Root<IncomeSearchDTO> root, CriteriaQuery<?> query, CriteriaBuilder cb
        Specification<Income> sp = (root, query, criteriaBuilder) -> {
            List<Predicate> andList = new ArrayList<>();//用来封装and条件
//            List<Predicate> orList = new ArrayList<>();//用来封装or条件

            if(incomeSearchDTO  != null){
/*                    if(null != incomeSearchDTO.getBus().getId()){

StringUtils.isNotBlank(userDTO.getEmail())
                    //模糊查询
                    Predicate predicate = criteriaBuilder.like(root.get("bus"), "%" + userDTO.getName() + "%");
                    andList.add(predicate);
                }*/
                if(null != incomeSearchDTO.getBus()){
                    //精确查询
                    Predicate predicate = criteriaBuilder.equal(root.get("bus"), incomeSearchDTO.getBus());
                    andList.add(predicate);
                }
                //求生日在某个时间段范围内的用户
                if(null != incomeSearchDTO.getStartDate()){
                    //大于等于
                    Predicate predicate = criteriaBuilder
                            .greaterThanOrEqualTo(root.get("incomeDate"), incomeSearchDTO.getStartDate());
                    andList.add(predicate);
                }
                if(null != incomeSearchDTO.getEndDate()){
                    //小于等于
                    Predicate predicate = criteriaBuilder
                            .lessThanOrEqualTo(root.get("incomeDate"), incomeSearchDTO.getEndDate());
                    andList.add(predicate);
                }

                if(null != incomeSearchDTO.getRoute()){
                    Predicate predicate = criteriaBuilder.equal(root.get("route"), incomeSearchDTO.getRoute());
                    andList.add(predicate);
                }
                if(null != incomeSearchDTO.getDriver()){
                    Predicate predicate = criteriaBuilder.equal(root.get("driver"), incomeSearchDTO.getDriver());
                    andList.add(predicate);
                }

            }

            Predicate andPredicate = null;
//            Predicate orPredicate = null;

            //拼接and条件
            if(!andList.isEmpty()){
                //转换为数组
                Predicate[] predicates = andList.toArray(new Predicate[]{});
                andPredicate = criteriaBuilder.and(predicates);
            }

            /*拼接or条件
            if(!orList.isEmpty()){
                //转换为数组
                Predicate[] predicates = orList.toArray(new Predicate[]{});
                orPredicate = criteriaBuilder.or(predicates);
            }*/

            //拼接查询条件
            List<Predicate> predicates = new ArrayList<>();
            if(andPredicate != null) predicates.add(andPredicate);
//            if(orPredicate != null) predicates.add(orPredicate);

            Predicate predicate = null;
            if(!predicates.isEmpty()){
                //转换为数组
                Predicate[] predicateArray = predicates.toArray(new Predicate[]{});
                predicate = criteriaBuilder.and(predicateArray);
            }

            List<Order> orderList = new ArrayList<>();//封装排序条件
            //按照创建时间 倒序排序
            orderList.add(criteriaBuilder.desc(root.get("incomeDate")));
            // 按照生日顺序排序
            //orderList.add(criteriaBuilder.asc(root.get("birthday")));

            //设置排序条件
            query.orderBy(orderList);

            //返回查询条件
            return predicate;

        };
        Pageable pageable = PageRequest.of(incomeSearchDTO.getPage()>0? incomeSearchDTO.getPage() - 1:incomeSearchDTO.getPage()
                ,incomeSearchDTO.getSize(),  Sort.by(Sort.Direction.DESC, "id"));
        return incomeDao.findAll(sp,pageable);
    }

    @Override
    public Income save(Income income) {
        return incomeDao.save(income);
    }

    @Override
    public void deleteById(Long id) {
        incomeDao.deleteById(id);
    }
}

controller层

    @GetMapping("/page")
    public Page<Income> findAll(IncomeSearchDTO incomeSearchDTO){
        return incomeService.findAll(incomeSearchDTO);
    }

参数传递类 dto

package com.choorn.serverboot.dto;

import com.choorn.serverboot.entity.Bus;
import com.choorn.serverboot.entity.Driver;
import com.choorn.serverboot.entity.Route;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.sql.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class IncomeSearchDTO {
    private Bus bus;
    private Route route;
    private Driver driver;
    private Date startDate;
    private Date endDate;
    private Integer size = 2;
    private Integer page = 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值