Search in sources :

Example 16 with OrderItem

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;
}
Also used : OrderItem(com.baomidou.mybatisplus.core.metadata.OrderItem) PropInfo(com.diboot.core.binding.parser.PropInfo)

Example 17 with OrderItem

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;
}
Also used : Executor(org.apache.ibatis.executor.Executor) Connection(java.sql.Connection) Properties(java.util.Properties) OrderItem(com.baomidou.mybatisplus.core.metadata.OrderItem) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement) ResultHandler(org.apache.ibatis.session.ResultHandler) IDialect(com.baomidou.mybatisplus.extension.plugins.pagination.dialects.IDialect) RowBounds(org.apache.ibatis.session.RowBounds) DbType(com.baomidou.mybatisplus.annotation.DbType) Collectors(java.util.stream.Collectors) StringUtils(com.baomidou.mybatisplus.core.toolkit.StringUtils) StatementHandler(org.apache.ibatis.executor.statement.StatementHandler) BoundSql(org.apache.ibatis.mapping.BoundSql) SQLException(java.sql.SQLException) List(java.util.List) Log(org.apache.ibatis.logging.Log) MappedStatement(org.apache.ibatis.mapping.MappedStatement) CollectionUtils(com.baomidou.mybatisplus.core.toolkit.CollectionUtils) Column(net.sf.jsqlparser.schema.Column) IPage(com.baomidou.mybatisplus.core.metadata.IPage) PaginationInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor) Column(net.sf.jsqlparser.schema.Column) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement)

Example 18 with OrderItem

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;
}
Also used : OrderItem(com.baomidou.mybatisplus.core.metadata.OrderItem) Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) IPage(com.baomidou.mybatisplus.core.metadata.IPage)

Example 19 with OrderItem

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);
}
Also used : OrderItem(com.baomidou.mybatisplus.core.metadata.OrderItem) ArrayList(java.util.ArrayList) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 20 with OrderItem

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");
}
Also used : OrderItem(com.baomidou.mybatisplus.core.metadata.OrderItem) ArrayList(java.util.ArrayList) PaginationInnerInterceptor(com.baomidou.mybatisplus.solon.plugins.inner.PaginationInnerInterceptor) Test(org.junit.jupiter.api.Test)

Aggregations

OrderItem (com.baomidou.mybatisplus.core.metadata.OrderItem)23 Page (com.baomidou.mybatisplus.extension.plugins.pagination.Page)12 ArrayList (java.util.ArrayList)7 IPage (com.baomidou.mybatisplus.core.metadata.IPage)6 List (java.util.List)4 Collectors (java.util.stream.Collectors)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 Map (java.util.Map)2 Sort (org.springframework.data.domain.Sort)2 OrderQueryParam (co.yixiang.common.web.param.OrderQueryParam)1 DbType (com.baomidou.mybatisplus.annotation.DbType)1 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)1 DefaultIdentifierGenerator (com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator)1 IdentifierGenerator (com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator)1 TableInfo (com.baomidou.mybatisplus.core.metadata.TableInfo)1 TableInfoHelper (com.baomidou.mybatisplus.core.metadata.TableInfoHelper)1 CollectionUtils (com.baomidou.mybatisplus.core.toolkit.CollectionUtils)1 StringUtils (com.baomidou.mybatisplus.core.toolkit.StringUtils)1 PaginationInnerInterceptor (com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor)1 IDialect (com.baomidou.mybatisplus.extension.plugins.pagination.dialects.IDialect)1