解决optimizeJoinOfCountSql反序列化不支持问题.
This commit is contained in:
parent
a4d6259fcc
commit
d9c25dfc31
10
build.gradle
10
build.gradle
@ -48,7 +48,7 @@ ext {
|
||||
"spring-test" : "org.springframework:spring-test:${springVersion}",
|
||||
"assertj-core" : "org.assertj:assertj-core:3.25.3",
|
||||
"junit-jupiter" : "org.junit.jupiter:junit-jupiter:${junitVersion}",
|
||||
"fastjson" : "com.alibaba:fastjson:2.0.50",
|
||||
"fastjson" : "com.alibaba:fastjson:2.0.51",
|
||||
"jackson" : "com.fasterxml.jackson.core:jackson-databind:2.17.1",
|
||||
"gson" : "com.google.code.gson:gson:2.11.0",
|
||||
"lagarto" : "org.jodd:jodd-lagarto:6.0.6",
|
||||
@ -163,14 +163,6 @@ subprojects {
|
||||
test {
|
||||
dependsOn("cleanTest", "generatePomFileForMavenJavaPublication")
|
||||
useJUnitPlatform()
|
||||
// 增加jvm参数不是一个很好的处理方案,最好还是只用java8下跑,下面只是兼容运行一下,但在高版本jdk下还有很多第三方反射库存在问题(等待完全升级)
|
||||
// 部分测试用例如果需要在高版本java下运行用@EnabledOnJre
|
||||
// 例如: https://github.com/cglib/cglib/issues/191
|
||||
// if (JavaVersion.current().isJava9Compatible()) {
|
||||
// jvmArgs += ["--add-opens", "java.base/java.lang=ALL-UNNAMED",
|
||||
// "--add-opens", "java.base/java.util=ALL-UNNAMED",
|
||||
// "--add-opens", "java.base/java.lang.invoke=ALL-UNNAMED"]
|
||||
// }
|
||||
exclude("**/phoenix/**")
|
||||
exclude("**/postgresql/**")
|
||||
// exclude("**/generator/**")
|
||||
|
@ -102,7 +102,9 @@ public interface IPage<T> extends Serializable {
|
||||
/**
|
||||
* 内部什么也不干
|
||||
* <p>只是为了 json 反序列化时不报错</p>
|
||||
* @deprecated 3.5.8
|
||||
*/
|
||||
@Deprecated
|
||||
default IPage<T> setPages(long pages) {
|
||||
// to do nothing
|
||||
return this;
|
||||
|
@ -73,4 +73,13 @@ public class OrderItem implements Serializable {
|
||||
this.asc = asc;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "OrderItem{" +
|
||||
"column='" + column + '\'' +
|
||||
", asc=" + asc +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -21,7 +21,6 @@ dependencies {
|
||||
implementation "${lib.'mybatis-velocity'}"
|
||||
implementation "${lib.'mybatis-freemarker'}"
|
||||
implementation "de.ruedigermoeller:fst:3.0.4-jdk17"
|
||||
implementation "com.alibaba.fastjson2:fastjson2:2.0.50"
|
||||
implementation "com.github.ben-manes.caffeine:caffeine:2.9.3"
|
||||
testImplementation "io.github.classgraph:classgraph:4.8.172"
|
||||
testImplementation "${lib.h2}"
|
||||
|
@ -146,7 +146,7 @@ public class PaginationInnerInterceptor implements InnerInterceptor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
|
||||
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
|
||||
IPage<?> page = ParameterUtils.findPage(parameter).orElse(null);
|
||||
if (null == page) {
|
||||
return;
|
||||
|
@ -292,4 +292,21 @@ public class Page<T> implements IPage<T> {
|
||||
}
|
||||
return searchCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Page{" +
|
||||
"records=" + records +
|
||||
", total=" + total +
|
||||
", size=" + size +
|
||||
", current=" + current +
|
||||
", orders=" + orders +
|
||||
", optimizeCountSql=" + optimizeCountSql +
|
||||
", searchCount=" + searchCount +
|
||||
", optimizeJoinOfCountSql=" + optimizeJoinOfCountSql +
|
||||
", maxLimit=" + maxLimit +
|
||||
", countId='" + countId + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -66,23 +66,31 @@ public class PageDTO<T> extends Page<T> {
|
||||
}
|
||||
|
||||
public String getCountId() {
|
||||
return this.countId;
|
||||
return super.countId();
|
||||
}
|
||||
|
||||
public Long getMaxLimit() {
|
||||
return this.maxLimit;
|
||||
return super.maxLimit();
|
||||
}
|
||||
|
||||
public List<OrderItem> getOrders() {
|
||||
return this.orders;
|
||||
return super.orders();
|
||||
}
|
||||
|
||||
public boolean isOptimizeCountSql() {
|
||||
return this.optimizeCountSql;
|
||||
return super.optimizeCountSql();
|
||||
}
|
||||
|
||||
public boolean isSearchCount() {
|
||||
return this.searchCount;
|
||||
return super.searchCount();
|
||||
}
|
||||
|
||||
public boolean isOptimizeJoinOfCountSql() {
|
||||
return super.optimizeJoinOfCountSql();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PageDTO{} " + super.toString();
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,14 @@
|
||||
package com.baomidou.mybatisplus.test;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONB;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.core.metadata.OrderItem;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.Gson;
|
||||
import org.apache.ibatis.reflection.property.PropertyCopier;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -11,12 +18,17 @@ import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.cglib.beans.BeanCopier;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author nieqiurong 2020/3/20.
|
||||
*/
|
||||
class PageTest {
|
||||
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
private final Gson gson = new Gson();
|
||||
|
||||
@Test
|
||||
@EnabledOnJre(JRE.JAVA_8)
|
||||
void testCopy() {
|
||||
@ -52,4 +64,107 @@ class PageTest {
|
||||
Assertions.assertEquals(10, page4.getSize());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPageToJson() throws JsonProcessingException {
|
||||
var page = new Page<>(1, 10, 2000);
|
||||
page.setOrders(List.of(OrderItem.asc("a")));
|
||||
//page无法序列化排序等其他属性 {"records":[],"total":2000,"size":10,"current":1,"pages":200}
|
||||
assertPage(page);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testPageDtoToJson() throws JsonProcessingException {
|
||||
var page = new PageDTO<>(1, 10, 100);
|
||||
assertPageDto(page);
|
||||
|
||||
page = new PageDTO<>(1, 10, 100);
|
||||
page.setOptimizeCountSql(false);
|
||||
assertPageDto(page);
|
||||
|
||||
page = new PageDTO<>(1, 10, 100);
|
||||
page.setSearchCount(false);
|
||||
assertPageDto(page);
|
||||
|
||||
page = new PageDTO<>(1, 10, 100);
|
||||
page.setOptimizeJoinOfCountSql(false);
|
||||
assertPageDto(page);
|
||||
|
||||
page = new PageDTO<>(1, 10, 100);
|
||||
page.setRecords(List.of("1", "2", "3"));
|
||||
assertPageDto(page);
|
||||
|
||||
page = new PageDTO<>(1, 10, 100);
|
||||
page.setRecords(List.of("1", "2", "3"));
|
||||
assertPageDto(page);
|
||||
|
||||
page = new PageDTO<>(1, 10, 100);
|
||||
page.setMaxLimit(1000L);
|
||||
page.setRecords(List.of("1", "2", "3"));
|
||||
assertPageDto(page);
|
||||
|
||||
page = new PageDTO<>(1, 10, 100);
|
||||
page.setRecords(List.of("1", "2", "3"));
|
||||
page.setCountId("123");
|
||||
assertPageDto(page);
|
||||
|
||||
page = new PageDTO<>(1, 10, 100);
|
||||
page.setOrders(OrderItem.descs("a","b"));
|
||||
assertPageDto(page);
|
||||
|
||||
page = new PageDTO<>(1, 10, 100);
|
||||
page.setOrders(OrderItem.ascs("a","b"));
|
||||
assertPageDto(page);
|
||||
|
||||
page = new PageDTO<>(1, 10, 100);
|
||||
page.setRecords(List.of("1", "2", "3"));
|
||||
page.setOrders(OrderItem.ascs("a","b"));
|
||||
assertPageDto(page);
|
||||
|
||||
}
|
||||
|
||||
private void assertPage(Page<?> source) throws JsonProcessingException {
|
||||
toConvert(source, Page.class).forEach(target -> {
|
||||
Assertions.assertEquals(source.getCurrent(), target.getCurrent());
|
||||
Assertions.assertEquals(source.getTotal(), target.getTotal());
|
||||
Assertions.assertEquals(source.getSize(), target.getSize());
|
||||
Assertions.assertEquals(source.countId(), target.countId());
|
||||
Assertions.assertEquals(source.getRecords().size(), target.getRecords().size());
|
||||
Assertions.assertEquals(source.getPages(), target.getPages());
|
||||
});
|
||||
}
|
||||
|
||||
private <T extends IPage<?>> List<T> toConvert(T source, Class<T> tClass) throws JsonProcessingException {
|
||||
return List.of(
|
||||
objectMapper.readValue(objectMapper.writeValueAsString(source), tClass),
|
||||
gson.fromJson(gson.toJson(source), tClass),
|
||||
JSON.parseObject(JSON.toJSONString(source), tClass),
|
||||
JSONB.parseObject(JSONB.toBytes(source), tClass),
|
||||
com.alibaba.fastjson.JSON.parseObject(com.alibaba.fastjson.JSON.toJSONString(source), tClass)
|
||||
);
|
||||
}
|
||||
|
||||
private void assertPageDto(PageDTO<?> source) throws JsonProcessingException {
|
||||
toConvert(source, PageDTO.class).forEach(target -> {
|
||||
Assertions.assertEquals(source.toString(), target.toString());
|
||||
Assertions.assertEquals(source.getCurrent(), target.getCurrent());
|
||||
Assertions.assertEquals(source.getTotal(), target.getTotal());
|
||||
Assertions.assertEquals(source.getSize(), target.getSize());
|
||||
Assertions.assertEquals(source.countId(), target.getCountId());
|
||||
Assertions.assertEquals(source.countId(), target.countId());
|
||||
Assertions.assertEquals(source.searchCount(), target.isSearchCount());
|
||||
Assertions.assertEquals(source.searchCount(), target.searchCount());
|
||||
Assertions.assertEquals(source.optimizeCountSql(), target.isOptimizeCountSql());
|
||||
Assertions.assertEquals(source.optimizeCountSql(), target.optimizeCountSql());
|
||||
Assertions.assertEquals(source.optimizeJoinOfCountSql(), target.optimizeJoinOfCountSql());
|
||||
Assertions.assertEquals(source.optimizeJoinOfCountSql(), target.isOptimizeJoinOfCountSql());
|
||||
Assertions.assertEquals(source.getRecords().size(), target.getRecords().size());
|
||||
Assertions.assertEquals(source.maxLimit(), target.getMaxLimit());
|
||||
Assertions.assertEquals(source.maxLimit(), target.maxLimit());
|
||||
Assertions.assertEquals(source.getOrders().size(), target.getOrders().size());
|
||||
Assertions.assertEquals(source.getOrders().size(), target.orders().size());
|
||||
Assertions.assertEquals(source.getPages(), target.getPages());
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user