1 Star 0 Fork 0

sections/mybatis-plus-query

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

mybatis-plus-query

1 介绍

该组件的核心思想是通过@Query注解简化MybatisPlus中的QueryWrapper的条件组装,极大提升查询开发效率。

原来你需要这样组织查询条件,代码杂乱容易出错:

LambdaQueryWrapper<ItemInfo> queryWrapper = new LambdaQueryWrapper<ItemInfo>()
    .like(queryParam.getName() != null, queryParam::getName, queryParam.getName())
    .eq(!StringUtils.isEmpty(queryParam.getCode()), queryParam::getCode, queryParam.getCode())
    .ge(queryParam.getStartDateFrom() != null, queryParam::getStartDate, queryParam.getStartDateFrom())
    .le(queryParam.getStartDateTo() != null, queryParam::getStartDate, queryParam.getStartDateTo())
    .eq(queryParam.getState() != null, queryParam::getState, queryParam.getState())
    .orderByAsc(queryParam::getPhone);

现在使用mybatis-plus-query后可以简化成这样:

QueryWrapper<ItemInfo> queryWrapper = QueryConverter.convert(queryParam);

2 基本原理

  1. mybatis-plus-query基于注解的思想,把查询条件的组织逻辑,标注到查询参数对象上,上面例子中对应的查询对象如下定义:
public class QueryParam {
    @Query(Operator.LIKE)
    private String name;

    @Query(Operator.EQ)
    private String code;

    @Query(Operator.GE)
    private Date StartDateFrom;

    @Query(Operator.LE)
    private Date StartDateTo;

    @Query(Operator.EQ)
    private String state;

    private List<Sorter> sorters;
}
  1. 然后通过QueryConverter工具类,来解析注解,生成对应的QueryWrapper,就像这样一步完成查询转换:
QueryWrapper<ItemInfo> queryWrapper = QueryConverter.convert(queryParam);

3 具体用法

3.1 引入POM依赖

<dependency>
    <groupId>io.gitee.sections</groupId>
    <artifactId>mybatis-plus-query</artifactId>
    <version>1.2</version>
</dependency>

3.2 @Query注解介绍

Query注解支持所有mybatis-plus的查询操作:EQ, NE, GT, GE, LT, LE, LIKE, LIKE_LEFT, LIKE_RIGHT, NOT_LIKE, IS_NULL, IS_NOT_NULL, IN, NOT_IN, BETWEEN, NOT_BETWEEN, APPLY。

下面是一些高级用法案例:

public class QueryParam {
    //使用column指定数据库列名,若不指定则默认根据查询对象字段名生成
    @Query(operator = Operator.LIKE, column = "user_name")
    private String name;

    //默认情况下null值不参与条件组装,可以通过ignoreNull=false来指定null参与条件组装,它将被转换为isNull操作
    @Query(operator = Operator.EQ, ignoreNull = false)
    private String code;

    //支持between操作,需要传入一个集合或数组,且必须有两个元素
    @Query(operator = Operator.BETWEEN)
    private List<Integer> ageRange;

    //支持in操作,需要传入一个集合或数组,可以通过ignoreEmpty参数来设置空集合是否参与条件组织
    @Query(operator = Operator.IN, ignoreEmpty = false)
    private List<Integer> states;

    //支持使用mybatis-plus的apply方法实现动态的sql拼接
    @Query(operator = Operator.APPLY, applySql="name like {0} or code like {0}")
    private String keyword;
}

3.3 PageQuery和PageResult

PageQuery和PageResult用于实现分页查询。

  1. PageQuery可以作为分页查询对象的父类,提供了分页查询的公共参数:current(当前页码,默认1)、pageSize(每页大小,默认10)、sorters(排序字段)
  2. PageResult作为分页查询的返回对象,其字段设计与前端antd框架的ProTable要求的结构保持一致,方便前端使用
import io.gitee.sections.query.Operator;
import io.gitee.sections.query.PageQuery;
import io.gitee.sections.query.Query;

//使用PageQuery作为分页查询对象的父类
public class UserQueryDTO extends PageQuery{
    @Query(Operator.LIKE)
    private String name;

    @Query(Operator.GT)
    private Integer age;

    @Query(Operator = Operator.IN)
    private List<String> types;
}
import io.gitee.sections.query.PageResult;
import io.gitee.sections.query.QueryConverter;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.function.Function;

public class UserRepository extends ServiceImpl {
    public PageResult<User> query(UserQueryDTO userQueryDTO){
        final QueryWrapper<User> queryWrapper = QueryConverter.convert(userQueryDTO);
        final Page<User> page = new Page<>(userQueryDTO.getCurrent(), userQueryDTO.getPageSize());
        super.page(page, queryWrapper);
        //把mybatis-plus的page对象转换为本框架提供的PageResult对象
        return PageResult.convert(page, Function.identity());
    }
}

注意:分页查询需要配置mybatis-plus的PaginationInnerInterceptor启用

3.4 ListQuery

ListQuery用于列表查询,可以作为列表查询对象的父类,包含列表查询的基础字段:limit(限制查询数量,默认50)、sorters(排序字段)

import io.gitee.sections.query.Operator;
import io.gitee.sections.query.ListQuery;
import io.gitee.sections.query.Query;

//使用ListQuery作为列表查询对象的父类
public class UserQueryDTO extends ListQuery{
    @Query
    private Integer age;

    @Query(Operator.LIKE)
    private String name;
}

3.5 Sorter

Sorter用于查询排序,在PageQuery和ListQuery中默认包含一个List sorters字段,可以直接使用。 QueryConverter在生成QueryWrapper时会识别sorters字段并生成排序条件

MIT License Copyright (c) 2023 sections Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

通过@Query注解简化MybatisPlus中的QueryWrapper的复杂的条件组装过程,简化开发工作量,提升开发效率 展开 收起
README
MIT
取消

发行版 (2)

全部
8个月前

贡献者

全部

语言

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/sections/mybatis-plus-query.git
git@gitee.com:sections/mybatis-plus-query.git
sections
mybatis-plus-query
mybatis-plus-query
master

搜索帮助