Commit db54e508 authored by kezhenxu94's avatar kezhenxu94 Committed by Jason Song

fixes #1880 pagination not work (#1891)

fixes #1880 pagination not work
parent f99d54a8
...@@ -35,13 +35,13 @@ public interface InstanceConfigRepository extends PagingAndSortingRepository<Ins ...@@ -35,13 +35,13 @@ public interface InstanceConfigRepository extends PagingAndSortingRepository<Ins
value = "select b.Id from `InstanceConfig` a inner join `Instance` b on b.Id =" + value = "select b.Id from `InstanceConfig` a inner join `Instance` b on b.Id =" +
" a.`InstanceId` where a.`ConfigAppId` = :configAppId and a.`ConfigClusterName` = " + " a.`InstanceId` where a.`ConfigAppId` = :configAppId and a.`ConfigClusterName` = " +
":clusterName and a.`ConfigNamespaceName` = :namespaceName and a.`DataChange_LastTime` " + ":clusterName and a.`ConfigNamespaceName` = :namespaceName and a.`DataChange_LastTime` " +
"> :validDate and b.`AppId` = :instanceAppId and ?#{#pageable.pageSize} > 0", "> :validDate and b.`AppId` = :instanceAppId",
countQuery = "select count(1) from `InstanceConfig` a inner join `Instance` b on b.id =" + countQuery = "select count(1) from `InstanceConfig` a inner join `Instance` b on b.id =" +
" a.`InstanceId` where a.`ConfigAppId` = :configAppId and a.`ConfigClusterName` = " + " a.`InstanceId` where a.`ConfigAppId` = :configAppId and a.`ConfigClusterName` = " +
":clusterName and a.`ConfigNamespaceName` = :namespaceName and a.`DataChange_LastTime` " + ":clusterName and a.`ConfigNamespaceName` = :namespaceName and a.`DataChange_LastTime` " +
"> :validDate and b.`AppId` = :instanceAppId", "> :validDate and b.`AppId` = :instanceAppId",
nativeQuery = true) nativeQuery = true)
Page<Object[]> findInstanceIdsByNamespaceAndInstanceAppId( Page<Object> findInstanceIdsByNamespaceAndInstanceAppId(
@Param("instanceAppId") String instanceAppId, @Param("configAppId") String configAppId, @Param("instanceAppId") String instanceAppId, @Param("configAppId") String configAppId,
@Param("clusterName") String clusterName, @Param("namespaceName") String namespaceName, @Param("clusterName") String clusterName, @Param("namespaceName") String namespaceName,
@Param("validDate") Date validDate, Pageable pageable); @Param("validDate") Date validDate, Pageable pageable);
......
...@@ -6,6 +6,7 @@ import com.ctrip.framework.apollo.biz.repository.InstanceConfigRepository; ...@@ -6,6 +6,7 @@ import com.ctrip.framework.apollo.biz.repository.InstanceConfigRepository;
import com.ctrip.framework.apollo.biz.repository.InstanceRepository; import com.ctrip.framework.apollo.biz.repository.InstanceRepository;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import java.util.Objects;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
...@@ -91,7 +92,7 @@ public class InstanceService { ...@@ -91,7 +92,7 @@ public class InstanceService {
appId, String clusterName, String appId, String clusterName, String
namespaceName, Pageable namespaceName, Pageable
pageable) { pageable) {
Page<Object[]> instanceIdResult = instanceConfigRepository Page<Object> instanceIdResult = instanceConfigRepository
.findInstanceIdsByNamespaceAndInstanceAppId(instanceAppId, appId, clusterName, .findInstanceIdsByNamespaceAndInstanceAppId(instanceAppId, appId, clusterName,
namespaceName, getValidInstanceConfigDate(), pageable); namespaceName, getValidInstanceConfigDate(), pageable);
...@@ -116,7 +117,7 @@ public class InstanceService { ...@@ -116,7 +117,7 @@ public class InstanceService {
} }
return null; return null;
}).filter((Long value) -> value != null).collect(Collectors.toSet()); }).filter(Objects::nonNull).collect(Collectors.toSet());
instances = findInstancesByIds(instanceIds); instances = findInstancesByIds(instanceIds);
} }
......
package com.ctrip.framework.apollo.biz.repository;
import com.ctrip.framework.apollo.biz.AbstractIntegrationTest;
import com.ctrip.framework.apollo.biz.entity.Instance;
import com.ctrip.framework.apollo.biz.entity.InstanceConfig;
import java.util.Date;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.annotation.Rollback;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
/**
* Created by kezhenxu94 at 2019/1/18 15:33.
*
* @author kezhenxu94 (kezhenxu94 at 163 dot com)
*/
public class InstanceConfigRepositoryTest extends AbstractIntegrationTest {
@Autowired
private InstanceConfigRepository instanceConfigRepository;
@Autowired
private InstanceRepository instanceRepository;
@Rollback
@Test
public void shouldPaginated() {
for (int i = 0; i < 25; i++) {
Instance instance = new Instance();
instance.setAppId("appId");
instanceRepository.save(instance);
final InstanceConfig instanceConfig = new InstanceConfig();
instanceConfig.setConfigAppId("appId");
instanceConfig.setInstanceId(instance.getId());
instanceConfig.setConfigClusterName("cluster");
instanceConfig.setConfigNamespaceName("namespace");
instanceConfigRepository.save(instanceConfig);
}
Page<Object> ids = instanceConfigRepository.findInstanceIdsByNamespaceAndInstanceAppId(
"appId", "appId", "cluster", "namespace", new Date(0), PageRequest.of(0, 10)
);
assertThat(ids.getContent(), hasSize(10));
ids = instanceConfigRepository.findInstanceIdsByNamespaceAndInstanceAppId(
"appId", "appId", "cluster", "namespace", new Date(0), PageRequest.of(1, 10)
);
assertThat(ids.getContent(), hasSize(10));
ids = instanceConfigRepository.findInstanceIdsByNamespaceAndInstanceAppId(
"appId", "appId", "cluster", "namespace", new Date(0), PageRequest.of(2, 10)
);
assertThat(ids.getContent(), hasSize(5));
}
}
...@@ -3,4 +3,3 @@ spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.Ph ...@@ -3,4 +3,3 @@ spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.Ph
spring.jpa.properties.hibernate.show_sql=false spring.jpa.properties.hibernate.show_sql=false
spring.h2.console.enabled = true spring.h2.console.enabled = true
spring.h2.console.settings.web-allow-others=true spring.h2.console.settings.web-allow-others=true
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment