Skip to content

Commit 7ae1485

Browse files
committed
feature: 使用 PageHelper 分页插件, 查询语句支持字段排序
1 parent 37971a7 commit 7ae1485

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/main/java/app/myoss/cloud/mybatis/repository/service/impl/BaseCrudServiceImpl.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Map;
3030
import java.util.Objects;
3131
import java.util.Set;
32+
import java.util.StringJoiner;
3233
import java.util.stream.Collectors;
3334

3435
import org.apache.commons.lang3.StringUtils;
@@ -966,9 +967,18 @@ protected void pageQuery(Page<T> result, T param, Map<String, Object> extraInfo,
966967

967968
@Override
968969
public <DTO> Page<DTO> findPageByHelper(Page<DTO> condition) {
970+
List<Order> orders = convertToOrders(condition.getSort());
971+
String orderBy = null;
972+
if (!CollectionUtils.isEmpty(orders)) {
973+
StringJoiner stringJoiner = new StringJoiner(", ");
974+
for (Order order : orders) {
975+
stringJoiner.add(order.getProperty() + " " + order.getDirection().name());
976+
}
977+
orderBy = stringJoiner.toString();
978+
}
969979
int pageNum = condition.getPageNum();
970980
int pageSize = condition.getPageSize();
971-
com.github.pagehelper.Page<DTO> page = com.github.pagehelper.PageHelper.startPage(pageNum, pageSize)
981+
com.github.pagehelper.Page<DTO> page = com.github.pagehelper.PageHelper.startPage(pageNum, pageSize, orderBy)
972982
.doSelectPage(() -> pageHelperQuery(condition.getParam(), condition));
973983
Page<DTO> pageResult = new Page<>();
974984
pageResult.setTotalCount(Math.toIntExact(page.getTotal()))

src/main/java/app/myoss/cloud/mybatis/repository/v2/service/impl/BaseCrudServiceImpl.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Map;
3030
import java.util.Objects;
3131
import java.util.Set;
32+
import java.util.StringJoiner;
3233
import java.util.stream.Collectors;
3334

3435
import org.apache.commons.lang3.StringUtils;
@@ -802,9 +803,18 @@ protected void pageQuery(Page<T> result, T param, Map<String, Object> extraInfo,
802803

803804
@Override
804805
public <DTO> Page<DTO> findPageByHelper(Page<DTO> condition) {
806+
List<Order> orders = convertToOrders(condition.getSort());
807+
String orderBy = null;
808+
if (!CollectionUtils.isEmpty(orders)) {
809+
StringJoiner stringJoiner = new StringJoiner(", ");
810+
for (Order order : orders) {
811+
stringJoiner.add(order.getProperty() + " " + order.getDirection().name());
812+
}
813+
orderBy = stringJoiner.toString();
814+
}
805815
int pageNum = condition.getPageNum();
806816
int pageSize = condition.getPageSize();
807-
com.github.pagehelper.Page<DTO> page = com.github.pagehelper.PageHelper.startPage(pageNum, pageSize)
817+
com.github.pagehelper.Page<DTO> page = com.github.pagehelper.PageHelper.startPage(pageNum, pageSize, orderBy)
808818
.doSelectPage(() -> pageHelperQuery(condition.getParam(), condition));
809819
Page<DTO> pageResult = new Page<>();
810820
pageResult.setTotalCount(Math.toIntExact(page.getTotal()))

0 commit comments

Comments
 (0)