use of com.baomidou.mybatisplus.core.metadata.OrderItem in project diboot by dibo-software.
the class Pagination method toPage.
/**
* 转换为IPage
*
* @param <T>
* @return
*/
public <T> Page<T> toPage() {
List<OrderItem> orderItemList = null;
// 解析排序
if (V.notEmpty(this.orderBy)) {
orderItemList = new ArrayList<>();
// orderBy=shortName:DESC,age:ASC,birthdate
String[] orderByFields = S.split(this.orderBy);
for (String field : orderByFields) {
V.securityCheck(field);
if (field.contains(":")) {
String[] fieldAndOrder = S.split(field, ":");
String fieldName = fieldAndOrder[0];
String columnName = S.toSnakeCase(fieldName);
PropInfo propInfo = getEntityPropInfo();
if (propInfo != null) {
// 前参数为字段名
if (propInfo.getFieldToColumnMap().containsKey(fieldName)) {
columnName = propInfo.getFieldToColumnMap().get(fieldName);
} else // 前参数为列名
if (propInfo.getColumnToFieldMap().containsKey(fieldName)) {
columnName = fieldName;
}
}
if (Cons.ORDER_DESC.equalsIgnoreCase(fieldAndOrder[1])) {
orderItemList.add(OrderItem.desc(columnName));
} else {
orderItemList.add(OrderItem.asc(columnName));
}
} else {
orderItemList.add(OrderItem.asc(S.toSnakeCase(field)));
}
}
}
Page<T> page = new Page<T>().setCurrent(getPageIndex()).setSize(getPageSize()).setTotal(getTotalCount() > 0 ? -1 : getTotalCount());
if (orderItemList != null) {
page.addOrder(orderItemList);
}
return page;
}
use of com.baomidou.mybatisplus.core.metadata.OrderItem in project best-cloud by shanzhaozhen.
the class CustomPaginationInnerInterceptor method addOrderByElements.
@Override
protected List<OrderByElement> addOrderByElements(List<OrderItem> orderList, List<OrderByElement> orderByElements) {
// return super.addOrderByElements(orderList, orderByElements);
List<OrderByElement> additionalOrderBy = orderList.stream().filter(item -> StringUtils.isNotBlank(item.getColumn())).map(item -> {
OrderByElement element = new OrderByElement();
// element.setExpression(new Column(item.getColumn()));
element.setExpression(new Column(StringUtils.camelToUnderline(item.getColumn())));
element.setAsc(item.isAsc());
element.setAscDescPresent(true);
return element;
}).collect(Collectors.toList());
if (CollectionUtils.isEmpty(orderByElements)) {
return additionalOrderBy;
}
// github pull/3550 优化排序,比如:默认 order by id 前端传了name排序,设置为 order by name,id
additionalOrderBy.addAll(orderByElements);
return additionalOrderBy;
}
use of com.baomidou.mybatisplus.core.metadata.OrderItem in project onex-boot by zhangchaoxu.
the class EntityService method getPage.
/**
* 获取分页对象
*
* @param params 分页查询参数
* @param defaultOrderField 默认排序字段
* @param isAsc 排序方式
*/
protected IPage<T> getPage(Map<String, Object> params, String defaultOrderField, boolean isAsc) {
// 分页对象 参数,当前页和每页数
Page<T> page = new Page<>(MapUtil.getLong(params, Const.PAGE, 1L), MapUtil.getLong(params, Const.LIMIT, 10L));
// 分页参数?
// params.put(Constant.PAGE, page);
// 排序字段
String orderField = (String) params.get(Const.ORDER_FIELD);
String order = (String) params.get(Const.ORDER);
// 前端字段排序
if (StringUtils.isNotBlank(orderField) && StringUtils.isNotBlank(order)) {
return page.addOrder(new OrderItem(orderField, Const.ASC.equalsIgnoreCase(order)));
}
// 没有排序字段,则不排序
if (StringUtils.isBlank(defaultOrderField)) {
return page;
}
// 默认排序
page.addOrder(new OrderItem(orderField, isAsc));
return page;
}
use of com.baomidou.mybatisplus.core.metadata.OrderItem in project solon by noear.
the class SelectBodyToPlainSelectTest method setup.
@BeforeEach
void setup() {
List<OrderItem> orderItems = new ArrayList<>();
OrderItem order = new OrderItem();
order.setAsc(true);
order.setColumn("column");
orderItems.add(order);
OrderItem orderEmptyColumn = new OrderItem();
orderEmptyColumn.setAsc(false);
orderEmptyColumn.setColumn("");
orderItems.add(orderEmptyColumn);
}
use of com.baomidou.mybatisplus.core.metadata.OrderItem in project solon by noear.
the class SelectBodyToPlainSelectTest method testPaginationInterceptorConcatOrderByFix.
@Test
void testPaginationInterceptorConcatOrderByFix() {
List<OrderItem> orderList = new ArrayList<>();
orderList.add(OrderItem.asc("column"));
String actualSql = new PaginationInnerInterceptor().concatOrderBy("select * from test union select * from test2", orderList);
assertThat(actualSql).isEqualTo("SELECT * FROM test UNION SELECT * FROM test2 ORDER BY column ASC");
String actualSqlUnionAll = new PaginationInnerInterceptor().concatOrderBy("select * from test union all select * from test2", orderList);
assertThat(actualSqlUnionAll).isEqualTo("SELECT * FROM test UNION ALL SELECT * FROM test2 ORDER BY column ASC");
}
Aggregations