diff --git a/fenix.xsd b/fenix.xsd index e294e57d..2f2c03b8 100644 --- a/fenix.xsd +++ b/fenix.xsd @@ -142,8 +142,8 @@ - - + + @@ -166,9 +166,7 @@ - - - + diff --git a/pom.xml b/pom.xml index dce43c4c..bd99c43f 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.blinkfox fenix - 2.4.1 + 2.4.2-SNAPSHOT jar fenix diff --git a/src/main/java/com/blinkfox/fenix/jpa/FenixJpaQuery.java b/src/main/java/com/blinkfox/fenix/jpa/FenixJpaQuery.java index 822271c1..0dbf1637 100644 --- a/src/main/java/com/blinkfox/fenix/jpa/FenixJpaQuery.java +++ b/src/main/java/com/blinkfox/fenix/jpa/FenixJpaQuery.java @@ -146,7 +146,7 @@ protected Query doCreateQuery(Object[] values) { // 循环设置命名绑定参数,且如果分页对象不为空,就设置分页参数. sqlInfo.getParams().forEach(query::setParameter); - if (pageable != null) { + if (pageable != null && pageable.isPaged()) { query.setFirstResult((int) pageable.getOffset()); query.setMaxResults(pageable.getPageSize()); } @@ -158,7 +158,7 @@ protected Query doCreateQuery(Object[] values) { } // 如果分页参数为空,说明不需要再做分页查询,须要从 ThreadLocal 中移除当前线程中的 fenixQueryInfo 信息. - if (pageable == null) { + if (pageable == null || pageable.isUnpaged()) { fenixQueryInfo.remove(); } return query; diff --git a/src/test/java/com/blinkfox/fenix/repository/BlogRepositoryTest.java b/src/test/java/com/blinkfox/fenix/repository/BlogRepositoryTest.java index fac80982..d0ef7130 100644 --- a/src/test/java/com/blinkfox/fenix/repository/BlogRepositoryTest.java +++ b/src/test/java/com/blinkfox/fenix/repository/BlogRepositoryTest.java @@ -29,6 +29,7 @@ import org.springframework.core.io.Resource; 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.repository.Query; import org.springframework.test.context.ContextConfiguration; @@ -323,18 +324,17 @@ public void queryWithConcurrent() throws InterruptedException { */ @Test public void queryBlogsWithDistinct() { - Page blogPage = blogRepository.queryBlogsWithDistinct(PageRequest.of(0,10)); + Page blogPage = blogRepository.queryBlogsWithDistinct(PageRequest.of(0, 10)); Assert.assertFalse(blogPage.isEmpty()); Assert.assertTrue(blogPage.getTotalElements() == 8); } - /** * 测试使用 {@link QueryFenix} 注解使用开启 distinct 检测但是没有 distinct 关键字的分页查询. */ @Test public void queryBlogsWithoutDistinct() { - Page blogPage = blogRepository.queryBlogsWithoutDistinct(PageRequest.of(0,10)); + Page blogPage = blogRepository.queryBlogsWithoutDistinct(PageRequest.of(0, 10)); Assert.assertFalse(blogPage.isEmpty()); Assert.assertTrue(blogPage.getTotalElements() == 10); } @@ -344,20 +344,29 @@ public void queryBlogsWithoutDistinct() { */ @Test public void queryBlogsWithoutDistinctNative() { - Page blogPage = blogRepository.queryBlogsWithoutDistinctNative(PageRequest.of(0,10)); + Page blogPage = blogRepository.queryBlogsWithoutDistinctNative(PageRequest.of(0, 10)); Assert.assertFalse(blogPage.isEmpty()); Assert.assertTrue(blogPage.getTotalElements() == 10); } - /** * 测试使用 {@link QueryFenix} 注解使用开启 distinct 检测但是没有 distinct 关键字的原生 sql 分页查询. */ @Test public void queryBlogsWithDistinctNative() { - Page blogPage = blogRepository.queryBlogsWithDistinctNative(PageRequest.of(0,10)); + Page blogPage = blogRepository.queryBlogsWithDistinctNative(PageRequest.of(0, 10)); Assert.assertFalse(blogPage.isEmpty()); - Assert.assertTrue(blogPage.getTotalElements() == 8); + Assert.assertEquals(8, blogPage.getTotalElements()); + } + + /** + * 测试使用 {@link QueryFenix} 注解使用开启 distinct 检测但是没有 distinct 关键字的原生 sql 分页查询. + */ + @Test + public void queryBlogsWithDistinctNative2() { + Page blogPage = blogRepository.queryBlogsWithDistinctNative(Pageable.unpaged()); + Assert.assertFalse(blogPage.isEmpty()); + Assert.assertEquals(8, blogPage.getTotalElements()); } /** diff --git a/src/test/java/com/blinkfox/fenix/repository/UserRepositoryTest.java b/src/test/java/com/blinkfox/fenix/repository/UserRepositoryTest.java index 3b1b4a20..d94ac496 100644 --- a/src/test/java/com/blinkfox/fenix/repository/UserRepositoryTest.java +++ b/src/test/java/com/blinkfox/fenix/repository/UserRepositoryTest.java @@ -20,6 +20,7 @@ import org.springframework.core.io.Resource; 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.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -54,6 +55,7 @@ public void init() throws IOException { if (!isLoad) { FenixConfigManager.getInstance().initLoad(new FenixConfig() .setDebug(true) + .setPrintSqlInfo(true) .setXmlLocations("my, fenix , , others/fenix-xml.xml , abc, def/ghi") .setHandlerLocations("com.blinkfox.fenix.handler, , ")); @@ -108,7 +110,18 @@ public void queryUserByIds() { Map userMap = new HashMap<>(2); userMap.put("ids", new String[] {"2", "4", "6", "8", "10"}); Page userPage = userRepository.queryUserByIds(userMap, - PageRequest.of(0, 2, Sort.by(Sort.Order.desc("id")))); + PageRequest.of(0, 3, Sort.by(Sort.Order.desc("id")))); + Assert.assertFalse(userPage.getContent().isEmpty()); + } + + /** + * 测试使用 {@link QueryFenix} 注解没有任何元数据参数时,使用默认约定来查询用户信息的方法. + */ + @Test + public void queryUserByIdsWithUnPage() { + Map userMap = new HashMap<>(2); + userMap.put("ids", new String[] {"2", "4", "6", "8", "10"}); + Page userPage = userRepository.queryUserByIds(userMap, Pageable.unpaged()); Assert.assertFalse(userPage.getContent().isEmpty()); }