|
9 | 9 | [](https://sonarcloud.io/dashboard?id=mybatis_mybatis-dynamic-sql)
|
10 | 10 |
|
11 | 11 | ## What Is This?
|
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. |
| 12 | +This library is a general purpose SQL generator. Think of it as a typesafe and expressive SQL DSL (domain specific |
| 13 | +language), with support for rendering SQL formatted properly for MyBatis3 and Spring's NamedParameterJDBCTemplate. |
14 | 14 |
|
15 | 15 | The library also contains extensions for Kotlin that enable an idiomatic Kotlin DSL for SQL.
|
16 | 16 |
|
17 | 17 | The library will generate full DELETE, INSERT, SELECT, and UPDATE statements. The DSL implemented 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): |
| 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 |
| 20 | +similar): |
20 | 21 |
|
21 | 22 | ```kotlin
|
22 |
| - fun search(id: String?, firstName: String?, lastName: String?) = |
23 |
| - select(Customer.id, Customer.firstName, Customer.lastName) { |
| 23 | + data class SearchParameters(val id: String?, val firstName: String?, val lastName: String?) |
| 24 | + |
| 25 | + fun search(searchParameters: SearchParameters) = |
| 26 | + select(id, firstName, lastName) { |
24 | 27 | from(Customer)
|
25 |
| - where { Customer.active.isTrue() } |
26 |
| - and { Customer.id (isEqualToWhenPresent(id).map{ it?.padStart(5, '0') }) } |
27 |
| - and { Customer.firstName (isLikeCaseInsensitiveWhenPresent(firstName) |
28 |
| - .map{ "%" + it.trim() + "%" }) } |
29 |
| - and { Customer.lastName (isLikeCaseInsensitiveWhenPresent(lastName) |
30 |
| - .map{ "%" + it.trim() + "%" }) } |
31 |
| - orderBy(Customer.lastName, Customer.firstName) |
| 28 | + where { |
| 29 | + active isEqualTo true |
| 30 | + and { id isEqualToWhenPresent searchParameters.id } |
| 31 | + and { |
| 32 | + firstName(isLikeCaseInsensitiveWhenPresent(searchParameters.firstName) |
| 33 | + .map { "%" + it.trim() + "%" }) |
| 34 | + } |
| 35 | + and { |
| 36 | + lastName(isLikeCaseInsensitiveWhenPresent(searchParameters.lastName) |
| 37 | + .map { "%" + it.trim() + "%" }) |
| 38 | + } |
| 39 | + } |
| 40 | + orderBy(lastName, firstName) |
32 | 41 | limit(500)
|
33 | 42 | }
|
34 | 43 | ```
|
35 | 44 |
|
36 | 45 | This query does quite a lot...
|
37 | 46 |
|
38 | 47 | 1. It is a search with three search criteria - any combination of search criteria can be used
|
39 |
| -1. Only records with an active status will be returned |
40 |
| -1. If `id` is specified, it will be padded to length 5 with '0' at the beginning of the string |
41 |
| -1. If `firstName` is specified, it will be used in a case-insensitive search and SQL wildcards will be appended |
42 |
| -1. If `lastName` is specified, it will be used in a case-insensitive search and SQL wildcards will be appended |
43 |
| -1. The query results are limited to 500 rows |
| 48 | +2. Only records with an active status will be returned |
| 49 | +3. If `id` is specified, it will be used as a filter |
| 50 | +4. If `firstName` is specified, it will be used in a case-insensitive search and SQL wildcards will be appended |
| 51 | +5. If `lastName` is specified, it will be used in a case-insensitive search and SQL wildcards will be appended |
| 52 | +6. The query results are limited to 500 rows |
44 | 53 |
|
45 |
| -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. |
| 54 | +Using the dynamic SQL features of the library eliminates a lot of code that would be required for checking nulls, |
| 55 | +adding wild cards, etc. This query clearly expresses the intent of the search in just a few lines. |
46 | 56 |
|
47 | 57 | See the following pages for detailed information:
|
48 | 58 |
|
49 |
| -| Page | Comments| |
50 |
| -|------|---------| |
51 |
| -|[Quick Start](src/site/markdown/docs/quickStart.md) | Shows a complete example of building code for this library | |
52 |
| -|[MyBatis3 Support](src/site/markdown/docs/mybatis3.md) | Information about specialized support for [MyBatis3](https://github.com/mybatis/mybatis-3). The examples on this page are similar to the code generated by [MyBatis Generator](https://github.com/mybatis/generator) | |
53 |
| -|[Kotlin Support with MyBatis3](src/site/markdown/docs/kotlinMyBatis3.md) | Information about the Kotlin extensions and Kotlin DSL when using MyBatis3 as the runtime | |
54 |
| -|[Spring Support](src/site/markdown/docs/spring.md) | Information about specialized support for Spring JDBC Templates | |
55 |
| -|[Kotlin Support with Spring](src/site/markdown/docs/kotlinSpring.md) | Information about the Kotlin extensions and Kotlin DSL when using Spring JDBC Template as the runtime | |
56 |
| -|[Spring Batch Support](src/site/markdown/docs/springBatch.md) | Information about specialized support for Spring Batch using the [MyBatis Spring Integration](https://github.com/mybatis/spring) | |
| 59 | +| Page | Comments | |
| 60 | +|--------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 61 | +| [Quick Start](src/site/markdown/docs/quickStart.md) | Shows a complete example of building code for this library | |
| 62 | +| [MyBatis3 Support](src/site/markdown/docs/mybatis3.md) | Information about specialized support for [MyBatis3](https://github.com/mybatis/mybatis-3). The examples on this page are similar to the code generated by [MyBatis Generator](https://github.com/mybatis/generator) | |
| 63 | +| [Kotlin Support with MyBatis3](src/site/markdown/docs/kotlinMyBatis3.md) | Information about the Kotlin extensions and Kotlin DSL when using MyBatis3 as the runtime | |
| 64 | +| [Spring Support](src/site/markdown/docs/spring.md) | Information about specialized support for Spring JDBC Templates | |
| 65 | +| [Kotlin Support with Spring](src/site/markdown/docs/kotlinSpring.md) | Information about the Kotlin extensions and Kotlin DSL when using Spring JDBC Template as the runtime | |
| 66 | +| [Spring Batch Support](src/site/markdown/docs/springBatch.md) | Information about specialized support for Spring Batch using the [MyBatis Spring Integration](https://github.com/mybatis/spring) | |
57 | 67 |
|
58 | 68 | The library test cases provide several complete examples of using the library in various different styles:
|
59 | 69 |
|
60 |
| -| Language | Runtime | Comments | Code Directory | |
61 |
| -|---|---|---|---| |
62 |
| -| Java | MyBatis3 | Example using Java utility classes for MyBatis in the style of MyBatis Generator | [../examples/simple](src/test/java/examples/simple) | |
63 |
| -| Java | MyBatis3 + MyBatis-Spring | Example using MyBatis-Spring integration | [../examples/column/comparison](src/test/java/examples/column/comparison) | |
64 |
| -| Java | MyBatis3 + MyBatis-Spring (Spring Batch)| Example using Java utility classes for the MyBatis integration with Spring Batch | [../examples/springbatch](src/test/java/examples/springbatch) | |
65 |
| -| Java | Spring JDBC | Example using Java utility classes for Spring JDBC Template | [../examples/spring](src/test/java/examples/spring) | |
66 |
| -| Kotlin | MyBatis3 | Example using Kotlin utility classes for MyBatis in the style of MyBatis Generator | [../examples/kotlin/mybatis3/canonical](src/test/kotlin/examples/kotlin/mybatis3/canonical) | |
67 |
| -| Kotlin | MyBatis3 + MyBatis-Spring | Example using MyBatis-Spring integration in Kotlin | [../examples/kotlin/mybatis3/column/comparison](src/test/kotlin/examples/kotlin/mybatis3/column/comparison) | |
68 |
| -| Kotlin | Spring JDBC | Example using Kotlin utility classes for Spring JDBC Template | [../examples/kotlin/spring/canonical](src/test/kotlin/examples/kotlin/spring/canonical) | |
| 70 | +| Language | Runtime | Comments | Code Directory | |
| 71 | +|----------|------------------------------------------|------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------| |
| 72 | +| Java | MyBatis3 | Example using Java utility classes for MyBatis in the style of MyBatis Generator | [../examples/simple](src/test/java/examples/simple) | |
| 73 | +| Java | MyBatis3 + MyBatis-Spring | Example using MyBatis-Spring integration | [../examples/column/comparison](src/test/java/examples/column/comparison) | |
| 74 | +| Java | MyBatis3 + MyBatis-Spring (Spring Batch) | Example using Java utility classes for the MyBatis integration with Spring Batch | [../examples/springbatch](src/test/java/examples/springbatch) | |
| 75 | +| Java | Spring JDBC | Example using Java utility classes for Spring JDBC Template | [../examples/spring](src/test/java/examples/spring) | |
| 76 | +| Kotlin | MyBatis3 | Example using Kotlin utility classes for MyBatis in the style of MyBatis Generator | [../examples/kotlin/mybatis3/canonical](src/test/kotlin/examples/kotlin/mybatis3/canonical) | |
| 77 | +| Kotlin | MyBatis3 + MyBatis-Spring | Example using MyBatis-Spring integration in Kotlin | [../examples/kotlin/mybatis3/column/comparison](src/test/kotlin/examples/kotlin/mybatis3/column/comparison) | |
| 78 | +| Kotlin | Spring JDBC | Example using Kotlin utility classes for Spring JDBC Template | [../examples/kotlin/spring/canonical](src/test/kotlin/examples/kotlin/spring/canonical) | |
69 | 79 |
|
70 | 80 |
|
71 | 81 | ## Requirements
|
|
0 commit comments