mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-07-31 03:32:43 +08:00
Add order by
This commit is contained in:
@ -70,5 +70,9 @@
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context-indexer</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -0,0 +1,27 @@
|
||||
package ai.chat2db.server.tools.common.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import ai.chat2db.server.tools.base.wrapper.param.OrderBy;
|
||||
import ai.chat2db.server.tools.common.util.EasySqlUtils;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
|
||||
import static com.baomidou.mybatisplus.core.enums.SqlKeyword.ORDER_BY;
|
||||
|
||||
/**
|
||||
* Custom query wrapper
|
||||
*
|
||||
* @author Jiaju Zhuang
|
||||
*/
|
||||
public class EasyLambdaQueryWrapper<T> extends LambdaQueryWrapper<T> {
|
||||
public void orderBy(List<OrderBy> orderByList) {
|
||||
if (CollectionUtils.isEmpty(orderByList)) {
|
||||
return;
|
||||
}
|
||||
for (OrderBy orderBy : orderByList) {
|
||||
appendSqlSegments(ORDER_BY, EasySqlUtils.columnToSqlSegment(orderBy.getOrderConditionName()),
|
||||
EasySqlUtils.parseOrderBy(orderBy.getDirection()));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package ai.chat2db.server.tools.common.util;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import ai.chat2db.server.tools.base.enums.OrderByDirectionEnum;
|
||||
import ai.chat2db.server.tools.base.wrapper.param.OrderBy;
|
||||
import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
|
||||
import com.baomidou.mybatisplus.core.conditions.segments.ColumnSegment;
|
||||
import com.baomidou.mybatisplus.core.conditions.segments.OrderBySegmentList;
|
||||
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
|
||||
import static com.baomidou.mybatisplus.core.enums.SqlKeyword.ASC;
|
||||
import static com.baomidou.mybatisplus.core.enums.SqlKeyword.DESC;
|
||||
|
||||
/**
|
||||
* sql utils
|
||||
*
|
||||
* @author Jiaju Zhuang
|
||||
*/
|
||||
public class EasySqlUtils {
|
||||
|
||||
public static String orderBy(List<OrderBy> orderByList) {
|
||||
if (CollectionUtils.isEmpty(orderByList)) {
|
||||
return null;
|
||||
}
|
||||
OrderBySegmentList orderBySegmentList = new OrderBySegmentList();
|
||||
for (OrderBy orderBy : orderByList) {
|
||||
orderBySegmentList.addAll(
|
||||
Arrays.asList(SqlKeyword.ORDER_BY, columnToSqlSegment(orderBy.getOrderConditionName()),
|
||||
parseOrderBy(orderBy.getDirection())));
|
||||
}
|
||||
return orderBySegmentList.getSqlSegment();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 columnName
|
||||
*/
|
||||
public static ColumnSegment columnToSqlSegment(String column) {
|
||||
return () -> column;
|
||||
}
|
||||
|
||||
public static ISqlSegment parseOrderBy(OrderByDirectionEnum direction) {
|
||||
if (direction == OrderByDirectionEnum.ASC) {
|
||||
return ASC;
|
||||
}
|
||||
return DESC;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user