Skip to content

axolo/qsql.mybatis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HttpServletRequest to MybatisPlus QueryWrapper

Parse HttpServletRequest request params as MybatisPlus QueryWrapper.

Features

  • Easy Integration: Seamlessly integrates with Spring Boot and MybatisPlus
  • Zero Configuration: Works out of the box with sensible defaults
  • Flexible Query: Supports various query conditions including =, >, <, !=, LIKE, IN, BETWEEN, IS NULL
  • Automatic Joins: Supports _include parameter for LEFT OUTER JOIN operations
  • Sorting & Pagination: Built-in support for _order and _offset/_limit parameters
  • CamelCase Support: Automatically converts underscore to camelCase keys

install

Maven

<dependency>
  <groupId>io.github.axolo</groupId>
  <artifactId>qsql</artifactId>
  <version>0.1.0</version>
</dependency>

Gradle

implementation 'io.github.axolo:qsql:0.1.0'

usage

import java.util.List;
import jakarta.servlet.http.HttpServletRequest;
import io.github.axolo.qsql.QueryWrapper;
import your.mybatisplus.entity.News;
import your.mybatisplus.mapper.NewsMapper;

class Example {
    public List<News> example(HttpServletRequest request) {
        QueryWrapper wrapper = new QueryWrapper(request);
        // wrapper.orderByAsc("updated_at");
        return newsMapper.selectList(wrapper);
    }
}

API

Constructors

QueryWrapper(HttpServletRequest request)

Creates a QueryWrapper from HttpServletRequest with default configuration.

Parameters:

  • request - the HttpServletRequest instance containing query parameters

Example:

QueryWrapper<News> wrapper = new QueryWrapper<>(request);

QueryWrapper(HttpServletRequest request, QueryWrapperConfig config)

Creates a QueryWrapper from HttpServletRequest with custom configuration.

Parameters:

  • request - the HttpServletRequest instance containing query parameters
  • config - the configuration for customizing query behavior

Example:

QueryWrapperConfig config = new QueryWrapperConfig()
    .setOffset(0)
    .setLimit(50)
    .setReservedKeys(List.of("access_token", "api_key"))
    .camelCaseToUnderscore(true); // request: userId -> sql: user_id

QueryWrapper<News> wrapper = new QueryWrapper<>(request, config);

QueryWrapperConfig

Configuration class for customizing QueryWrapper behavior.

Parameter Type Default Description
offset Integer 0 Default offset for pagination
limit Integer 1000 Default limit for pagination
reservedKeys List<String> ["access_token"] Keys excluded from where clause
keepEmpty Boolean false Keep empty string query parameters
disableInclude Boolean false Disable the _include join feature
camelCaseToUnderscore Boolean true Convert camelCase keys to underscore

request

where

params sql example description
= price=99 price = 99
> > price=>99 price > 99
< < price=<100 price < 100
! != price=!99 price != 99
!< >= price=!<100 price >= 100
!> <= price=!>99 price <= 100
% LIKE title=Shanghai% starts with 'Shanghai'
, IN name=zhangsan,lisi name in ['zhangsan', 'lisi']
~ BETWEEN AND date=2023-07-01~2023-08-01 date in july of 2023
!null IS NOT NULL gender=!null gender is not null
null IS NULL gender=null gender is null
true true disabled=true disabled = true
false false disabled=false disabled = false

Query news created in October 2024

-- curl '/news?created_at=2024-10-01~2024-11-01'
SELECT * FROM news
WHERE `created_at` BETWEEN '2024-10-01' AND '2024-11-01';

order

params sql example description
_order ORDER BY _order=updated_at order by updated_at asc
! DESC _order=!updated_at order by updated_at desc
, , _order=sort,!updated_at sort asc, updated_at desc

Get news ordered by top ascending and updatedAt descending

-- curl '/news?_order=top,!updated_at'
SELECT * FROM news
ORDER BY `top` ASC, updated_at DESC;

limit

params sql example description
_offset LIMIT _offset=10 record start from 10
_limit LIMIT _limit=20 limit 20 records

Get news on page 2

-- curl '/news?_offset=10&_limit=20'
SELECT * FROM news
LIMIT 10, 20;

include

params sql example description
_include LEFT OUTER JOIN _include=user join user

Assuming news has one user

-- curl '/news?_include=user'
SELECT * FROM news
LEFT OUTER JOIN `user` ON `user`.id = news.user_id;

About

HttpServletRequest to MybatisPlus QueryWrapper

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages