https://github.com/wz2cool/mybatis-dynamic-query
dynamic query for mybatis
https://github.com/wz2cool/mybatis-dynamic-query
dynamic java mybatis sql
Last synced: 5 months ago
JSON representation
dynamic query for mybatis
- Host: GitHub
- URL: https://github.com/wz2cool/mybatis-dynamic-query
- Owner: wz2cool
- License: apache-2.0
- Created: 2017-07-19T07:58:12.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2025-11-17T16:28:24.000Z (7 months ago)
- Last Synced: 2025-11-17T17:29:59.604Z (7 months ago)
- Topics: dynamic, java, mybatis, sql
- Language: Java
- Size: 689 KB
- Stars: 42
- Watchers: 5
- Forks: 25
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
MyBatis Dynamic Query
=====================================
[](http://www.apache.org/licenses/LICENSE-2.0.html)
[](https://github.com/wz2cool/mybatis-dynamic-query/actions/workflows/webpack.yml)
[](https://coveralls.io/github/wz2cool/mybatis-dynamic-query?branch=master)
[](https://www.versioneye.com/user/projects/597283ce368b08005906060c)
[](https://central.sonatype.com/artifact/com.github.wz2cool/mybatis-dynamic-query)
The MyBatis Dynamic Query framework makes it easier to generate "where" and "order" expression dynamically in mapper xml.
mybatis-dynamic-query comes to solve four problem:
- no need write lots of code in xml.
- filtering or sorting maintained by java code.
- hot update "where" and "order" expression.
- save filter or sort descriptor and re-use them.
## Docs
[中文文档1.x](https://wz2cool.gitbooks.io/mybatis-dynamic-query-zh-cn/content/)
|
[中文文档2.x](https://wz2cool.gitbooks.io/mybatis-dynamic-query-2-0-zh-cn/content/)
## Database support
- H2
- MySql
- SqlServer
- Postresql
- Oracle (TODO)
## Maven
```xml
com.github.wz2cool
mybatis-dynamic-query
2.0.11
```
## Dynamic Query example
- create two tables by sql.
```sql
DELETE FROM category;
INSERT INTO category (category_id, category_name, description) VALUES
(1, 'Beverages', 'test'),
(2, 'Condiments', 'test'),
(3, 'Oil', 'test');
DELETE FROM product;
INSERT INTO product (product_id, category_id, product_name, price) VALUES
(1, 1, 'Northwind Traders Chai', 18.0000),
(2, 2, 'Northwind Traders Syrup', 7.5000),
(3, 2, 'Northwind Traders Cajun Seasoning', 16.5000),
(4, 3, 'Northwind Traders Olive Oil', 16.5000);
```
- create a model map to this table.
```java
public class ProductView {
@Column(name = "product_id", table = "product")
private Long productId;
@Column(name = "product_name", table = "product")
private String productName;
@Column(name = "price", table = "product")
private BigDecimal price;
@Column(name = "category_id", table = "category")
private Long categoryId;
@Column(name = "category_name", table = "category")
private String categoryName;
@Column(name = "description", table = "category")
private String description;
// get, set method.
}
```
- create a dynamic select in mapper interface / xml.
```java
List getProductViewsByDynamic(Map params);
```
```xml
SELECT
${columnsExpression}
*
FROM product LEFT JOIN category ON product.category_id = category.category_id
WHERE ${whereExpression}
ORDER BY ${orderByExpression}
```
- generate expression and param map (NOTE: expression string also put into map).
```java
@Test
public void testMultiTablesFilter() throws Exception {
FilterDescriptor priceFilter1 =
new FilterDescriptor(ProductView.class, ProductView::getPrice,
FilterOperator.GREATER_THAN_OR_EQUAL, 6);
FilterDescriptor priceFilter2 =
new FilterDescriptor(ProductView.class, ProductView::getPrice,
FilterOperator.LESS_THAN, 10);
FilterDescriptor categoryNameFilter =
new FilterDescriptor(ProductView.class, ProductView::getCategoryName,
FilterOperator.START_WITH, "Co");
SortDescriptor idDescSort =
new SortDescriptor(ProductView.class, ProductView::getProductID, SortDirection.DESC);
Map params =
// NOTE: we recommend you to set "columnsExpressionPlaceholder"
// in case of duplicated column name in two tables.
// 这里你也可以不给列的站位,但是推荐使用,防止两个表有重复的名字
MybatisQueryProvider
.createInstance(ProductView.class, "columnsExpression")
.addFilters("whereExpression",
priceFilter1, priceFilter2, categoryNameFilter)
.addSorts("orderByExpression", idDescSort)
.toQueryParam();
List result = northwindDao.getProductViewsByDynamic(params);
assertEquals(true, result.size() > 0);
}
```
output result
```bash
==> Preparing: SELECT product.product_id AS product_id, product.price AS price, category.description AS description, category.category_name AS category_name, product.product_name AS product_name, category.category_id AS category_id
FROM product LEFT JOIN category ON product.category_id = category.category_id WHERE (product.price >= ? AND product.price < ? AND category.category_name LIKE ?)
==> Parameters: 6(Integer), 10(Integer), Co%(String)
<== Columns: PRODUCT_ID, PRICE, DESCRIPTION, CATEGORY_NAME, PRODUCT_NAME, CATEGORY_ID
<== Row: 2, 7.5000, test, Condiments, Northwind Traders Syrup, 2
<== Total: 1
```
## Dynamic Query Mapper
DynamicQueryMapper is based on tk.mybatis.mapper.
### spring boot configuration
1. add dependency
```xml
com.github.wz2cool
mybatis-dynamic-query
2.0.2
tk.mybatis
mapper-spring-boot-starter
1.1.3
org.mybatis
mybatis
3.4.4
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.0
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-aop
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.2.0
```
2. register DynamicQueryMapper in `application.properties` file.
```bash
mapper.mappers[0]=com.github.wz2cool.dynamic.mybatis.mapper.DynamicQueryMapper
```
3. scan mappers.
```java
@SpringBootApplication
@MapperScan(basePackages = "com.github.wz2cool.mdqtest.mapper")
@EnableSwagger2
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
### create mapper
```java
public interface ProductDao extends DynamicQueryMapper {
}
```
