码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2020-06-17 12:57:54      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:time   pos   查询   executor   spec   ace   record   family   value   

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语句实现分页加模糊查询(参数可能为空)

标签:time   pos   查询   executor   spec   ace   record   family   value   

原文地址:https://www.cnblogs.com/-z9-/p/13151687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!