思路
采用 HandlerMethodArgumentResolver 预先对排序数据处理。
代码
MybatisPlusProperties
@ConfigurationProperties("mybatis-plus.page")
@Data
public class MybatisPlusProperties {
/** 当前页字段 */
private String keyCurrent="current";
/** 分页记录数字段 */
private String keySize = "size";
/** 排序字段 */
private String keyOrders = "orders";
/** 正序字段 */
private String keyAsc = "asc";
/** 逆序字段 */
private String keyDesc = "desc";
/** 分隔符 */
private String split = " ";
/** 默认当前页 */
private Long current = 1L;
/** 默认分页记录数 */
private Long size = 10L;
/** 默认分页排序 */
private boolean asc = false;
/** 默认分页字段 */
private String column = "sys_create_time";
}
application.yml
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
logic-delete-field: sys_enable # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2)
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
type-enums-package: com.example.mvc.enums
page:
current: 1
size: 10
column: sys_create_time
asc: false
CPageArgumentResolver
public class CPageArgumentResolver implements HandlerMethodArgumentResolver {
public void setMybatisPlusProperties(MybatisPlusProperties mybatisPlusProperties) {
this.mybatisPlusProperties = mybatisPlusProperties;
}
private MybatisPlusProperties mybatisPlusProperties;
@Override
public boolean supportsParameter(MethodParameter parameter) {
Class<?> type = parameter.getParameterType();
return Page.class.isAssignableFrom(type);
}
@SuppressWarnings("rawtypes")
@Override
public Object resolveArgument(MethodParameter parameter,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception {
long current = mybatisPlusProperties.getCurrent();
long size = mybatisPlusProperties.getSize();
final String currentS = webRequest.getParameter(mybatisPlusProperties.getKeyCurrent());
final String sizeS = webRequest.getParameter(mybatisPlusProperties.getKeySize());
final String[] ordersS = Optional.ofNullable(
webRequest.getParameterValues(mybatisPlusProperties.getKeyOrders()))
.orElse(new String[]{mybatisPlusProperties.getColumn()});
if (!StringUtils.isBlank(currentS)&& ValidateUtil.isNumber(currentS)){
current = Long.parseLong(currentS);
}
if (!StringUtils.isBlank(sizeS)&& ValidateUtil.isNumber(currentS)){
size = Long.parseLong(sizeS);
}
List<OrderItem> orders = new ArrayList<>();
for (String temp : ordersS) {
final String[] s = temp.split(mybatisPlusProperties.getSplit());
String column = mybatisPlusProperties.getColumn();
boolean asc = mybatisPlusProperties.isAsc();
if(s.length==2){
if (mybatisPlusProperties.getKeyDesc().equalsIgnoreCase(s[1])){
asc = false;
}else if (mybatisPlusProperties.getKeyAsc().equalsIgnoreCase(s[1])){
asc = true;
}else {
asc = mybatisPlusProperties.isAsc();
}
column = StringUtil.camelToUnderline(s[0]);
}else if (s.length == 1){
column = StringUtil.camelToUnderline(s[0]);
}
orders.add(new OrderItem(column, asc));
}
final Page page = new Page(current, size);
page.setOrders(orders);
return page;
}
}
WebAppConfigure
@Configuration
public class WebAppConfigure implements WebMvcConfigurer {
@Autowired
private MybatisPlusProperties mybatisPlusProperties;
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
final CPageArgumentResolver cPageArgumentResolver = new CPageArgumentResolver();
cPageArgumentResolver.setMybatisPlusProperties(mybatisPlusProperties);
resolvers.add(cPageArgumentResolver);
WebMvcConfigurer.super.addArgumentResolvers(resolvers);
}
}
SysOperateLog
@Builder
@EqualsAndHashCode(callSuper = true)
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@TableName("sys_operate_log")
public class SysOperateLog implements Serializable {
private static final long serialVersionUID = 3636265721933858877L;
/**创建时间*/
private String LocalDateTime sysCreateTime;
}
ISysOperateLogService
public interface ISysOperateLogService extends IService<SysOperateLog> {
}
SysOperateLogServiceImpl
@Service
public class SysOperateLogServiceImpl extends ServiceImpl<SysOperateLogMapper, SysOperateLog> implements ISysOperateLogService {
}
SysOperateLogController
@Controller
@RequestMapping("/sysOperateLog")
public class SysOperateLogController {
final ISysOperateLogService logService;
public SysOperateLogController(ISysOperateLogService logService) {
this.logService = logService;
}
@ResponseBody
@ResponseResult
@GetMapping("/page")
public Page<SysOperateLog> page(Page<SysOperateLog> page, SysOperateLog entity){
QueryWrapper<SysOperateLog> queryWrapper = new QueryWrapper<SysOperateLog>();
queryWrapper.setEntity(entity);
return logService.page(page, queryWrapper);
}
}
调用方式:
GET http://127.0.0.1:8080/sysOperateLog/page?current=2&size=10&orders=sysCreateTime asc
说明:
该案例默认在“MybatisPlusProperties”中进行了对“current”“page”“orders”字段和默认值进行了封装,并且可以在application.yml 中进行更改。默认使用sys_create_time(数据库字段)进行逆序排序,传递参数直接传 “orders=sysCreateTime asc”即可,支持传多个值,后端对进行驼峰转下滑线处理。
SQL日志:
SELECT sys_create_time FROM sys_operate_log ORDER BY sys_create_time ASC LIMIT 10,10