Skip to content

Commit

Permalink
解决optimizeJoinOfCountSql反序列化不支持问题.
Browse files Browse the repository at this point in the history
  • Loading branch information
nieqiurong committed Jun 23, 2024
1 parent a4d6259 commit d9c25df
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 17 deletions.
10 changes: 1 addition & 9 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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/**")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ default long getPages() {
/**
* 内部什么也不干
* <p>只是为了 json 反序列化时不报错</p>
* @deprecated 3.5.8
*/
@Deprecated
default IPage<T> setPages(long pages) {
// to do nothing
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,13 @@ public OrderItem setAsc(boolean asc) {
this.asc = asc;
return this;
}

@Override
public String toString() {
return "OrderItem{" +
"column='" + column + '\'' +
", asc=" + asc +
'}';
}

}
1 change: 0 additions & 1 deletion mybatis-plus-extension/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public boolean willDoQuery(Executor executor, MappedStatement ms, Object paramet
}

@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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,21 @@ public boolean searchCount() {
}
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 + '\'' +
'}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,31 @@ public static <T> Page<T> of(long current, long size, long total, boolean search
}

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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,15 +18,20 @@
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(){
void testCopy() {
Page page1 = new Page(2, 10, 100, false);
page1.setOptimizeCountSql(false);
page1.setOrders(Collections.singletonList(OrderItem.asc("test")));
Expand Down Expand Up @@ -52,4 +64,107 @@ void testCopy(){
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());
});
}

}

0 comments on commit d9c25df

Please sign in to comment.