Skip to content

Commit

Permalink
开启 v2.4.2 版本,修复使用 Pageable.unpaged() 时引起的错误
Browse files Browse the repository at this point in the history
  • Loading branch information
blinkfox committed Feb 3, 2021
1 parent 2fb5818 commit 6433f12
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
8 changes: 3 additions & 5 deletions fenix.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@
<xs:complexType name="like">
<xs:attribute name="field" type="xs:string" use="required"/>
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="value" type="xs:string" />
<xs:attribute name="pattern" type="xs:string" />
<xs:attribute name="value" type="xs:string"/>
<xs:attribute name="pattern" type="xs:string"/>
<xs:attribute name="match" type="xs:string"/>
</xs:complexType>

Expand All @@ -166,9 +166,7 @@
<xs:attribute name="match" type="xs:string"/>
</xs:complexType>



<xs:element name="where" >
<xs:element name="where">
<xs:complexType mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:group ref="mainGroup"/>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.blinkfox</groupId>
<artifactId>fenix</artifactId>
<version>2.4.1</version>
<version>2.4.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>fenix</name>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/blinkfox/fenix/jpa/FenixJpaQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -158,7 +158,7 @@ protected Query doCreateQuery(Object[] values) {
}

// 如果分页参数为空,说明不需要再做分页查询,须要从 ThreadLocal 中移除当前线程中的 fenixQueryInfo 信息.
if (pageable == null) {
if (pageable == null || pageable.isUnpaged()) {
fenixQueryInfo.remove();
}
return query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -323,18 +324,17 @@ public void queryWithConcurrent() throws InterruptedException {
*/
@Test
public void queryBlogsWithDistinct() {
Page<Blog> blogPage = blogRepository.queryBlogsWithDistinct(PageRequest.of(0,10));
Page<Blog> 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<Blog> blogPage = blogRepository.queryBlogsWithoutDistinct(PageRequest.of(0,10));
Page<Blog> blogPage = blogRepository.queryBlogsWithoutDistinct(PageRequest.of(0, 10));
Assert.assertFalse(blogPage.isEmpty());
Assert.assertTrue(blogPage.getTotalElements() == 10);
}
Expand All @@ -344,20 +344,29 @@ public void queryBlogsWithoutDistinct() {
*/
@Test
public void queryBlogsWithoutDistinctNative() {
Page<Blog> blogPage = blogRepository.queryBlogsWithoutDistinctNative(PageRequest.of(0,10));
Page<Blog> 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<Long> blogPage = blogRepository.queryBlogsWithDistinctNative(PageRequest.of(0,10));
Page<Long> 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<Long> blogPage = blogRepository.queryBlogsWithDistinctNative(Pageable.unpaged());
Assert.assertFalse(blogPage.isEmpty());
Assert.assertEquals(8, blogPage.getTotalElements());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, , "));

Expand Down Expand Up @@ -108,7 +110,18 @@ public void queryUserByIds() {
Map<String, Object> userMap = new HashMap<>(2);
userMap.put("ids", new String[] {"2", "4", "6", "8", "10"});
Page<User> 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<String, Object> userMap = new HashMap<>(2);
userMap.put("ids", new String[] {"2", "4", "6", "8", "10"});
Page<User> userPage = userRepository.queryUserByIds(userMap, Pageable.unpaged());
Assert.assertFalse(userPage.getContent().isEmpty());
}

Expand Down

0 comments on commit 6433f12

Please sign in to comment.