MybatisPlus版本
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.4.2</version>
</dependency>
定义MybatisPlus工具类
import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.xnt.product.common.core.core.domain.OrderBy;
import com.xnt.product.common.core.exception.CustomException;
import java.util.List;
import static com.baomidou.mybatisplus.core.enums.SqlKeyword.*;
/**
* @author wxm
*/
public class MybatisPlusUtil {
/**
* @param queryWrapper {@link LambdaQueryWrapper}
* @param column 排序字段
* @param asc 是否升序
* @param <T> 泛型
*/
public static <T> void setOrderOne(LambdaQueryWrapper<T> queryWrapper, String column, boolean asc) {
try {
ISqlSegment[] sqlSegments = {ORDER_BY, () -> column, asc ? ASC : DESC};
queryWrapper.getExpression().add(sqlSegments);
} catch (Exception e) {
e.printStackTrace();
throw new CustomException("设置排序出错:" + e.getMessage());
}
}
/**
* @param queryWrapper {@link LambdaQueryWrapper}
* @param order {@link OrderBy} 排序
* @param <T> 泛型
*/
public static <T> void setOrder(LambdaQueryWrapper<T> queryWrapper, OrderBy order) {
setOrderOne(queryWrapper, order.getColumn(), order.isAsc());
}
/**
* @param queryWrapper {@link LambdaQueryWrapper}
* @param list {@link OrderBy} 排序
* @param <T> 泛型
*/
public static <T> void setOrder(LambdaQueryWrapper<T> queryWrapper, List<OrderBy> list) {
for (OrderBy order : list) {
setOrder(queryWrapper, order);
}
}
/**
* @param queryWrapper {@link LambdaQueryWrapper}
* @param orderItem {@link OrderItem} 排序
* @param <T> 泛型
*/
public static <T> void setOrderItem(LambdaQueryWrapper<T> queryWrapper, OrderItem orderItem) {
setOrderOne(queryWrapper, orderItem.getColumn(), orderItem.isAsc());
}
/**
* @param queryWrapper {@link LambdaQueryWrapper}
* @param list {@link OrderItem} 排序
* @param <T> 泛型
*/
public static <T> void setOrderItem(LambdaQueryWrapper<T> queryWrapper, List<OrderItem> list) {
for (OrderItem order : list) {
setOrderItem(queryWrapper, order);
}
}
}
定义OrderBy实体类
@Data
public class OrderBy {
/**
* 字段名称
*/
@NotEmpty
private String column;
/**
* 排序类型 升序:asc 降序:desc
* 默认升序
*/
private boolean asc = true;
/**
* 是否进行驼峰转下划线
*/
private boolean toUnder = false;
public String getColumn() {
return isToUnder() ? StrUtil.toUnderlineCase(column) : column;
}
public String getColumn(boolean toUnder) {
return toUnder ? StrUtil.toUnderlineCase(column) : column;
}
/**
* 获取 OrderItem
*
* @return
*/
public OrderItem getOrderItem() {
return getOrderItem(isAsc(), isToUnder());
}
/**
* 获取 OrderItem
*
* @param isAsc 是否升序
* @return
*/
public OrderItem getOrderItem(boolean isAsc) {
return getOrderItem(isAsc, isToUnder());
}
/**
* 获取 OrderItem
*
* @param isToUnder 是否进行驼峰转下划线
* @return
*/
public OrderItem getOrderItemToUnder(boolean isToUnder) {
return getOrderItem(isAsc(), isToUnder);
}
/**
* 获取 OrderItem
*
* @param sort 排序方式 升序:asc 降序:desc
* @param isToUnder 是否进行驼峰转下划线
* @return
*/
public OrderItem getOrderItem(String sort, boolean isToUnder) {
return getOrderItem(isAsc(), isToUnder);
}
/**
* 获取 OrderItem
*
* @param isAsc 是否升序
* @param isToUnder 是否进行驼峰转下划线
* @return
*/
public OrderItem getOrderItem(boolean isAsc, boolean isToUnder) {
String col = isToUnder ? StrUtil.toUnderlineCase(column) : column;
return OrderItemUtils.getOrder(col, isAsc);
}
/**
* 获取sql
* @return
*/
public String getSql(){
return (isToUnder() ? StrUtil.toUnderlineCase(column) : column) + (this.isAsc()? " asc":" desc");
}
}
使用自定义排序
1、获取前段参数
public List<OrderBy> getOrderBys() {
String order = ServletUtils.getParameter(PageConstants.ORDER);
if (ObjectUtil.isEmpty(order)) {
return null;
}
List<OrderBy> orderByList = new ArrayList<>();
if (JsonUtil.isTypeJSONArray(order)) {
orderByList.addAll(JsonUtil.toList(order, OrderBy.class));
} else {
orderByList.add(JsonUtil.parse(order, OrderBy.class));
}
return orderByList;
}
2、调用工具类
public List<SysOperLog> getList(SysOperLog operLog) {
LambdaQueryWrapper<SysOperLog> lambda = getLambda();
List<OrderItem> list = getOrderItems();
if (ObjectUtils.isNotEmpty(list)) {
MybatisPlusUtil.setOrderItem(lambda, list);
} else {
lambda.orderByDesc(SysOperLog::getOperTime);
}
return super.list(lambda);
}
本文转载自: https://blog.csdn.net/qq_43040552/article/details/129276021
版权归原作者 程序猴老王 所有, 如有侵权,请联系我们删除。
版权归原作者 程序猴老王 所有, 如有侵权,请联系我们删除。