MyBatis Dynamic Query

License Build Status Coverage Status Dependency Status Maven central

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:

Docs

中文文档1.x | 中文文档2.x

Database support

Maven

<dependency>
    <groupId>com.github.wz2cool</groupId>
    <artifactId>mybatis-dynamic-query</artifactId>
    <version>2.0.11</version>
</dependency>

Dynamic Query example

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.
}

Dynamic Query Mapper

DynamicQueryMapper is based on tk.mybatis.mapper.

spring boot configuration

  1. add dependency
    <!-- base -->
    <dependency>
    <groupId>com.github.wz2cool</groupId>
    <artifactId>mybatis-dynamic-query</artifactId>
    <version>2.0.2</version>
    </dependency>
    <!-- register mapper -->
    <dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>1.1.3</version>
    </dependency>
    <!-- mybatis -->
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.4</version>
    </dependency>
    <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.0</version>
    </dependency>
    <!-- spring boot web already has jackson-->
    <!--  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.0</version>
    </dependency>-->
    <!-- spring boot -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.2.0</version>
    </dependency>
  2. register DynamicQueryMapper in application.properties file.
    mapper.mappers[0]=com.github.wz2cool.dynamic.mybatis.mapper.DynamicQueryMapper
  3. scan mappers.
    @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

    public interface ProductDao extends DynamicQueryMapper<Product> {
    }