JPA 以SQL语句实现分页加模糊查询(参数可能为空)

Posted 纯属谎言

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JPA 以SQL语句实现分页加模糊查询(参数可能为空)相关的知识,希望对你有一定的参考价值。

repository代码:

package com.fancy.miniflow.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import com.fancy.miniflow.entity.UploadRecord;

public interface UploadRecordRepository extends JpaRepository<UploadRecord, Integer>,JpaSpecificationExecutor<UploadRecord> {

  @Query(nativeQuery = true , value = "SELECT * from upload_record "
    + "WHERE if(?1 !=‘‘,upload_time < ?1,1=1) AND if(?2 !=‘‘,upload_time > ?2,1=1) AND if(?3 !=‘‘,id = ?3,1=1) LIMIT ?4,?5")
  List<UploadRecord> getList(String endTime , String startTime , Integer id , Integer p ,Integer pageSize );
}

service代码:

package com.fancy.miniflow.serviceimp;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fancy.miniflow.entity.UploadRecord;
import com.fancy.miniflow.repository.UploadRecordRepository;

@Service
public class UploadRecordService {

@Autowired
private UploadRecordRepository uploadRecordRepository;

public List<UploadRecord> getUploadRecord(int page , int pageSize , String beginTime , String endTime , Integer id){
Integer p = (page - 1)*pageSize;
List<UploadRecord> list = uploadRecordRepository.getList(endTime, beginTime, id, p, pageSize);
return list;
}
}

 

以上是关于JPA 以SQL语句实现分页加模糊查询(参数可能为空)的主要内容,如果未能解决你的问题,请参考以下文章

关于分页加模糊查询的存储过程

sqlserver2005模糊查询分页具体该怎么实现?

JPA自定义模糊查询并将查询结果分页

SpringBoot Mybatis-Plus 分页模糊查询 分页参数和响应封装

jsp+sqlserver 2005模糊查询分页时候点击那个首页或下一页的时候老是查出所有

mybatis-plus模糊查询