Mybatais多条件分页查询

Controller

@GetMapping("/list")
public Result<IPage<AssetsInventory>> inventoryList(@RequestParam(value = "pageNo", required = true) Integer pageNo,
                                                    @RequestParam(value = "pageSize", required = true) Integer pageSize,
                                                    @RequestParam(value = "condition", required = false) String condition) {
    Result<IPage<AssetsInventory>> result = new Result<>();
    // 动态组装queryWrapper
    QueryWrapper<AssetsInventory> queryWrapper = QueryWrapperGenerator.parseWhereSql(condition);
    // 分页查询
    IPage<AssetsInventory> pageList = assetsInventoryService.page(new Page<>(pageNo, pageSize), queryWrapper);
    result.success(pageList);
    return result;
}

组装queryWrapper工具类

public class QueryWrapperGenerator {
    public static QueryWrapper parseWhereSql(String param) {
        QueryWrapper queryWrapper = new QueryWrapper();
        // 转义前端传来的参数
        String condition = URLDecoder.decode(param, StandardCharsets.UTF_8);
        if(StrUtil.isNotEmpty(condition)) {
            List<ConditionVo> conditionList = JSON.parseArray(condition, ConditionVo.class);
            if(CollUtil.isNotEmpty(conditionList)){
                for(ConditionVo conditionVo : conditionList){
                    if(conditionVo.getValue() != null && !"".equals(conditionVo.getValue())) {
                    	// 驼峰转下划线
	                    String column = humpToLine2(conditionVo.getColumn());
                        switch (conditionVo.getType()) {
                            default:
                            case "eq":
                                queryWrapper.eq(column, conditionVo.getValue());
                                break;
                            case "ne":
                                queryWrapper.ne(column, conditionVo.getValue());
                                break;
                            case "like":
                                queryWrapper.like(column, conditionVo.getValue());
                                break;
                            case "leftlike":
                                queryWrapper.likeLeft(column, conditionVo.getValue());
                                break;
                            case "rightlike":
                                queryWrapper.likeRight(column, conditionVo.getValue());
                                break;
                            case "notlike":
                                queryWrapper.notLike(column, conditionVo.getValue());
                                break;
                            case "gt":
                                queryWrapper.gt(column, conditionVo.getValue());
                                break;
                            case "lt":
                                queryWrapper.lt(column, conditionVo.getValue());
                                break;
                            case "ge":
                                queryWrapper.ge(column, conditionVo.getValue());
                                break;
                            case "le":
                                queryWrapper.le(column, conditionVo.getValue());
                                break;
                        }
                    }
                }
            }
        }
        return queryWrapper;
    }

    /**
     * 驼峰转下划线
     */
    private static String humpToLine2(String str) {
        Pattern humpPattern = Pattern.compile("[A-Z]");
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}

ConditionVo

@Data
public class ConditionVo implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 数据库字段名
     */
    private String column;
    /**
     * 字段值
     */
    private String value;
    /**
     * 连接类型,如llike,equals,gt,ge,lt,le
     */
    private String type;
}

前端参数

let condition = [
	{
		column: "name",
		value: "test",
		type: "eq"
	}
]
let requestParams = {
	pageNo: 1,
	pageSize: 10,
	// get请求 需要转义
	condition: encodeURIComponent(JSON.stringify(condition ))
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值