|
4 | 4 | [](https://coveralls.io/github/mybatis/mybatis-dynamic-sql?branch=master)
|
5 | 5 | [](https://maven-badges.herokuapp.com/maven-central/org.mybatis.dynamic-sql/mybatis-dynamic-sql)
|
6 | 6 | [](https://oss.sonatype.org/content/repositories/snapshots/org/mybatis/dynamic-sql/mybatis-dynamic-sql/)
|
7 |
| -[](http://www.apache.org/licenses/LICENSE-2.0.html) |
| 7 | +[](https://www.apache.org/licenses/LICENSE-2.0.html) |
8 | 8 | [](https://sonarcloud.io/dashboard?id=mybatis_mybatis-dynamic-sql)
|
9 | 9 | [](https://sonarcloud.io/dashboard?id=mybatis_mybatis-dynamic-sql)
|
10 | 10 |
|
11 | 11 | ## What Is This?
|
12 |
| -This library is a framework for generating dynamic SQL statements. Think of it as a typesafe SQL templating library, |
13 |
| -with additional support for MyBatis3 and Spring JDBC Templates. |
14 |
| - |
15 |
| -The library will generate full DELETE, INSERT, SELECT, and UPDATE statements formatted for use by MyBatis or Spring. |
16 |
| -The most common use case is to generate statements, and a matching set of parameters, that can be directly used |
17 |
| -by MyBatis. The library will also generate statements and parameter objects that are compatible with Spring JDBC |
18 |
| -templates. |
19 |
| - |
20 |
| -The library works by implementing an SQL-like DSL that creates an object containing a full SQL statement and any |
21 |
| -parameters required for that statement. The SQL statement object can be used directly by MyBatis as a parameter to a mapper method. |
22 |
| - |
23 |
| -The library also contains extensions for Kotlin that enable an idiomatic Kotlin DSL. |
24 |
| - |
25 |
| -See the following pages for further information: |
| 12 | +This library is a general purpose SQL generator. Think of it as a typesafe and expressive SQL DSL (domain specific language), |
| 13 | +with support for rendering SQL formatted properly for MyBatis3 and Spring's NamedParameterJDBCTemplate. |
| 14 | + |
| 15 | +The library also contains extensions for Kotlin that enable an idiomatic Kotlin DSL for SQL. |
| 16 | + |
| 17 | +The library will generate full DELETE, INSERT, SELECT, and UPDATE statements. The DSL implementd by the |
| 18 | +library is very similar to native SQL but it includes many functions that allow for very dynamic SQL statements. |
| 19 | +For example, a typical search can be coded with a query like this (the following code is Kotlin, but Java code is very similar): |
| 20 | + |
| 21 | +```kotlin |
| 22 | + fun search(id: String?, firstName: String?, lastName: String?) = |
| 23 | + select(Customer.id, Customer.firstName, Customer.lastName).from(Customer) { |
| 24 | + where(Customer.active, isEqualTo(true)) |
| 25 | + and(Customer.id, isEqualToWhenPresent(id).then{ it?.padStart(5, '0') }) |
| 26 | + and(Customer.firstName, isLikeCaseInsensitiveWhenPresent(firstName).then{ "%" + it.trim() + "%" }) |
| 27 | + and(Customer.lastName, isLikeCaseInsensitiveWhenPresent(lastName).then{ "%" + it.trim() + "%" }) |
| 28 | + orderBy(Customer.lastName, Customer.firstName) |
| 29 | + limit(500) |
| 30 | + } |
| 31 | +``` |
| 32 | + |
| 33 | +This query does quite a lot... |
| 34 | + |
| 35 | +1. It is a search with three search criteria - any combination of search criteria can be used |
| 36 | +1. Only records with an active status will be returned |
| 37 | +1. If `id` is specified, it will be padded to length 5 with '0' at the beginning of the string |
| 38 | +1. If `firstName` is specified, it will be used in a case insensitive search and SQL wildcards will be appended |
| 39 | +1. If `lastName` is specified, it will be used in a case insensitive search and SQL wildcards will be appended |
| 40 | +1. The query results are limited to 500 rows |
| 41 | + |
| 42 | +Using the dynamic SQL features of the library eliminates a lot of code that would be required for checking nulls, adding wild cards, etc. This query clearly expresses the intent of the search in just a few lines. |
| 43 | + |
| 44 | +See the following pages for detailed information: |
26 | 45 |
|
27 | 46 | | Page | Comments|
|
28 | 47 | |------|---------|
|
|
0 commit comments