Java Spring JPA分页如果不止一个页面不起作用

时间:2018-04-03 08:50:43

标签: java mysql sql spring-data-jpa

我正在尝试将jpa Pagination与自定义查询一起使用。

如果匹配2个案例,

分页正在运作。

案例1:

  • 如果数据小于10且页码请求为0

如果请求不起作用

案例2:

  • 如果数据超过10且请求页码为1

如果没有分页不起作用。

我无法弄清楚问题所在。有帮助吗?

提前致谢。

@Repository
public interface SmsLogRepository extends JpaRepository<SmsLog, Long> {

@Query(value = "select * from sms_log s where s.phone_number = (:ph) \n#pageable\n", nativeQuery = true)
Page<SmsLog> findByPhoneNumber(@Param("ph") String phoneNumber, Pageable pageable);

@Query(value = "select * from sms_log s where s.phone_number = (:ph) AND s.message_timestamp between (:fromdate) and (:todate) \n#pageable\n", nativeQuery = true)
Page<SmsLog> findByPhoneNumberAndDate(@Param("ph") String phoneNumber, @Param("fromdate") String fromDate, @Param("todate") String todate, Pageable pageable);

@Query(value = "select * from sms_log s where s.message_timestamp between (:fromdate) and (:todate) \n#pageable\n", nativeQuery = true)
Page<SmsLog> findByDate(@Param("fromdate") String fromDate, @Param("todate") String todate, Pageable pageable);
}

请求:

PageRequest pageRequest = new PageRequest(page, 10, new Sort(new Sort.Order(Sort.Direction.DESC, "message_timestamp")));
            return logRepository.findByPhoneNumber(ph, pageRequest);

1 个答案:

答案 0 :(得分:1)

花了一些时间后我找到了答案

我错过了计数查询。添加计数查询后,分页工作就像魅力:)

//MAIN ROUTINE
var main = function() {
	
	var doc = app.properties.activeDocument,
	fgp = app.findGrepPreferences.properties,
	fcgo = app.findChangeGrepOptions.properties,
	found, n = 0, text, pTag, xe;
	
	//Exit if no documents open
	if ( !doc ) {
		alert("You need an open document" );
		return;
	}
	
	//Setting F/R Grep
	app.findGrepPreferences = app.findChangeGrepOptions = null;
	
	app.findGrepPreferences.properties = {
		findWhat : "^.+",
	}
	app.findChangeGrepOptions.properties = {
		includeFootnotes:false,
		includeHiddenLayers:true,
		includeLockedLayersForFind:true,
		includeLockedStoriesForFind:true,
		includeMasterPages:true,
	}
	
	//Adding "p" tag if needed
	pTag = doc.xmlTags.itemByName("p");
	!pTag.isValid && pTag = doc.xmlTags.add({name:"p"});
	
	//Getting paragraphs occurences
	found = doc.findGrep();
	n = found.length;
	while ( n-- ) {
		text = found[n];
		xe = text.associatedXMLElements;
		//Adding "p" tags with ids if needed
		if ( !xe.length || xe[0].markupTag.name!="p") { 
			doc.xmlElements[0].xmlElements.add( pTag, text ).xmlAttributes.add('id', guid () );
		}
	}
	
	//Reverting initial F/R settings
	app.findGrepPreferences.properties = fgp;
	app.findChangeGrepOptions.properties = fcgo;
}

//Returns unique ID
function guid() {
  function s4() {
    return Math.floor((1 + Math.random()) * 0x10000)
      .toString(16)
      .substring(1);
  }
  return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}

var u;

//Run
app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );